Skip to content

Convert SearchControl component to TypeScript - #42647

Closed
kienstra wants to merge 7 commits into
WordPress:trunkfrom
kienstra:update/search-control-typescript
Closed

kienstra wants to merge 7 commits into
WordPress:trunkfrom
kienstra:update/search-control-typescript

Conversation

@kienstra

@kienstra kienstra commented Jul 22, 2022 •

Copy link
Copy Markdown
Contributor

What?

Converts the SearchControl component to TypeScript

Why?

As part of an effort to convert @wordpress/components to TypeScript

How?

Mainly by adding types to the SearchControl component

Testing Instructions

  1. Create a new post
  2. Click the block inserter
  3. Type 'Columns' in the SearchControl component
  4. Expected: The 'Columns' block appeasr

Screenshots or screencast

conver

Add a types.ts file.
Though doesn't convert index.native.js to TS.
@kienstra kienstra changed the title Convert SearchControl to TypeScript Convert SearchControl component to TypeScript Jul 22, 2022
* Internal dependencies
*/
import { Button } from '../';
import { Button } from '../button';

@kienstra kienstra Jul 22, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, there's an error, probably from Button not being in TypeScript:

File 'plugins/gutenberg/packages/components/src/index.js' is not listed within the file list of project 'plugins/gutenberg/packages/components/tsconfig.json'. Projects must list all files or use an 'include' pattern.
The file is in the program because:
Imported via '../' from file 'plugins/gutenberg/packages/components/src/search-control/index.tsx'

onClick={ () => {
onChange( '' );
searchRef.current.focus();
if ( searchRef.current ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this if conditional, there's a TypeScript error:

(property) React.MutableRefObject<undefined>.current: undefined
Conversion of type 'undefined' to type 'HTMLInputElement' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need it if you do this #42647 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Removed this conditional in 366b312

onChange( '' );
searchRef.current.focus();
if ( searchRef.current ) {
( searchRef.current as HTMLInputElement )?.focus();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ref is passed to an <input> below, so searchRef.current should usually be HTMLInputElement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No casting needed. See #42647 (comment)

@kienstra kienstra Jul 25, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Removed this casting in 366b312

@kienstra
kienstra marked this pull request as ready for review July 22, 2022 20:12
@kienstra
kienstra requested a review from ajitbohra as a code owner July 22, 2022 20:12
kienstra and others added 4 commits July 25, 2022 10:54
…trol/index.tsx

Co-authored-by: Manzoor Wani <manzoorwani.jk@gmail.com>
…trol/types.ts

Co-authored-by: Manzoor Wani <manzoorwani.jk@gmail.com>
…trol/types.ts

Co-authored-by: Manzoor Wani <manzoorwani.jk@gmail.com>
@kienstra

kienstra commented Oct 2, 2022

Copy link
Copy Markdown
Contributor Author

Closing in favor of #43871, which handles this.

I should have followed up on this PR.

@kienstra kienstra closed this Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants