-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextControl: Convert component to TypeScript #40633
Conversation
Size Change: +7 B (0%) Total Size: 1.23 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for helping out on the @wordpress/components
TypeScript refactor, @walbo !
I just left a couple of inline comments, but this PR is in a great shape already.
Finally, would you mind adding an entry to the pachake's CHANGELOG ? You may want to add an ### Internal
section under ## Unreleased
.
/** | ||
* The current value of the input. | ||
*/ | ||
value: string | number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I were to write this component from scratch, I would type value
as a string
.
Since this component is not experimental (and since the purpose of these TypeScript migrations is to introduce the least amount of changes possible), we may want to keep this type as it currently is, but consider updating it in a follow-up (see also this recent related conversation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it should be string
in this case. The old jsdocs said string
but the README file says string | number
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it as string|number
for this PR. It would be great if we could then open a follow-up PR to change it to be only string
— we could then assess in that PR if effectively it's a change that can be applied without introducing any breaking change.
Would you be up for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Lets do that. I will creeate a follow-up PR once this is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful, thank you for the contribution!
Looks great on my end, I think we're good to go once the Action thing is sorted 👍
import type { WordPressComponentProps } from '../ui/context'; | ||
import type { TextControlProps } from './types'; | ||
|
||
function UnforwardedTextControl( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries :) Great that you updated the guide. Did follow the guide and seems to cover most of the stuff you need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh, good to hear! 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback! And of course do let us know if you feel like there's something missing (or something wrong) in the guide 😄
argTypes: { | ||
onChange: { | ||
action: 'onChange', | ||
control: { type: null }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It looks like we could remove the control: { type: null }
in this case. Storybook hides the controls for on*
props by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I didn't know that 👍 Removed in e30a8dd
const DefaultTemplate: ComponentStory< typeof TextControl > = ( { | ||
...args | ||
} ) => { | ||
const [ value, setValue ] = useState( '' ); | ||
|
||
return ( | ||
<TextControl | ||
{ ...args } | ||
value={ value } | ||
onChange={ ( v ) => { | ||
setValue( v ); | ||
} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently, the Action thing (enabled on this line) only works by passing through the onChange
prop.
const DefaultTemplate: ComponentStory< typeof TextControl > = ( {
+ onChange,
...args
} ) => {
const [ value, setValue ] = useState( '' );
return (
<TextControl
{ ...args }
value={ value }
onChange={ ( v ) => {
setValue( v );
+ onChange( v );
CleanShot.2022-04-29.at.01.46.10-converted.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Added in e30a8dd
- Remove control on onChange. Storybook hides on* by default. - Add onChange to trigger action
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thank you! 🚀
What?
Converts the
TextControl
component to TypeScript.Why?
Part of the @wordpress/components's TypeScript migration (#35744).
How?
Refactors the current
TextControl
to TypeScript.Testing Instructions
TextControl
continues to function as expectedcc: @ciampo