-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ColorListPicker: refactor to TypeScript #46358
Conversation
HALP!Spot where I'm currently stuck: When typing the
Potential solution: This also feels like the right move to me, because the first argument isn't actually optional - there's a second argument ( I began working on a PR to demo this change, but it had a bit of a domino effect that spills over to other components, so I put a pin in that for now. I'm happy to chase it further if this feels like the right road to travel! |
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.
Since this component is not exported publicly and is apparently only used by DuotonePicker, it might be simpler to just move this into the duotone-picker/
folder and type it as part of DuotonePicker. We can always extract it back out when necessary.
Btw I don't mind moving forward with this PR as is (without moving it into DuotonePicker) as an intermediary step. I just wanted to say that it's probably not worth the effort to write new stories/tests/docs for this internal-only component.
value?: Array< string >; | ||
disableCustomColors?: boolean; | ||
enableAlpha?: boolean; | ||
onChange: ( newValue: Array< string > ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. | ||
}; | ||
|
||
export type ColorOptionProps = Pick< | ||
ColorListPickerProps, | ||
'colors' | 'disableCustomColors' | 'enableAlpha' | ||
> & { | ||
label: ColorListPickerProps[ 'labels' ][ number ]; | ||
value: string; // TODO: can we extract this from ColorListPickerProps['value']? That prop being optional is a little tricky | ||
onChange: ( newValue: string ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. |
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'm not sure if I fully understand the potential solution you described, but I'm assuming it's something along these lines? If so, yes I think it makes sense!
value?: Array< string >; | |
disableCustomColors?: boolean; | |
enableAlpha?: boolean; | |
onChange: ( newValue: Array< string > ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. | |
}; | |
export type ColorOptionProps = Pick< | |
ColorListPickerProps, | |
'colors' | 'disableCustomColors' | 'enableAlpha' | |
> & { | |
label: ColorListPickerProps[ 'labels' ][ number ]; | |
value: string; // TODO: can we extract this from ColorListPickerProps['value']? That prop being optional is a little tricky | |
onChange: ( newValue: string ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. | |
value?: Array< string | undefined >; | |
disableCustomColors?: boolean; | |
enableAlpha?: boolean; | |
onChange: ( newValue: Array< string | undefined > ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. | |
}; | |
export type ColorOptionProps = Pick< | |
ColorListPickerProps, | |
'colors' | 'disableCustomColors' | 'enableAlpha' | |
> & { | |
label: ColorListPickerProps[ 'labels' ][ number ]; | |
value: string | undefined; // TODO: can we extract this from ColorListPickerProps['value']? That prop being optional is a little tricky | |
onChange: ( newValue: string | undefined ) => void; // TODO: resolve conflict with ColorPalette's onChange prop, which expects an optional value arg. |
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 @mirka! I'm getting back into this after being out for the holidays. I did consider the | undefined
approach, but also (as I tend to do) dove down another rabbit hole at the same time.
The tl;dr of my previously mentioned solution was that the whole reason the undefined
is needed is that another componenet (ColorPalette
) explicitly passes an undefined
value` when de-selecting a color. I think we can use an empty sting instead to both simplify the typing, and prevent a potential edge case bug that TypeScript might not currently see for us, involving an optional parameter that may not be safe to actually omit in all scenarios). More thorough description and a potential change in #46890 😄
If that's not a good direction though, I'm perfectly happy with your proposed solution of adding | undefined
as needed!
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.
Based on the feedback from @jorgefilipecosta on #46890, I've scrapped that approached and gone with string | undefined
here.
I've also added some JSDoc descriptions to types. Probably not mission-critical on an internal component like this one but I figured I may as well include them while I was updating things.
Thanks for that, I'll skip those TODOs and we can plan on rolling this into DuotonePicker in a follow up! |
5e82439
to
f5ae77a
Compare
Size Change: 0 B Total Size: 1.33 MB ℹ️ View Unchanged
|
Flaky tests detected in 8a6f754. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3962383833
|
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.
Looks good 🚀
What?
Refactor
ColorListPicker
component to TypeScriptPart of #35744
Why?
The refactor to TypeScript has many benefits (auto-generated docs, static linting and error prevention, better IDE experience). See #35744 for more details
How?
Followed the steps in the TypeScript migration guide
Testing Instructions
Testing Instructions for Keyboard
TBD