-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Allowing generic item type in new experimental SelectControl #34547
Conversation
|
||
export const itemToString = ( item: ItemType | null ) => { | ||
import { getItemLabelType } from './types'; | ||
export const defaultGetItemLabel = < ItemType >( item: ItemType | null ) => { | ||
return item ? item.label : ''; |
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.
Referencing item.label
and item.value
below are causing TS errors, since TS isn't aware that they're only intended to be used along with the default item type. Is there any way to remedy this in a TS supported way?
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.
Yeah, you can make us of the is
type guard -> https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
The easiest way to do this is by creating a isDefaultItemType
function, like so:
function isDefaultItemType(item: ItemType | null): item is ItemType {
return (item as ItemType).label !== undefined;
}
You could include the check for value
here as well if you wanted.
Then in the above code you can do:
if ( isDefaultItemType(item) ) {
return item.label;
} else {
return '';
}
TypeScript should not complain about item.label
within the if statement now.
Test Results SummaryCommit SHA: b8b9ef7
To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
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.
Really nice work on this @joelclimbsthings, this is testing really well, I merged it into my category field branch locally to try the changes out and things tested well out of the box.
I only have one suggestion for the getItemValueType
return value to allow for number
as well, as a lot of id
's are numbers.
And I left a response to your TS question here: https://github.com/woocommerce/woocommerce/pull/34547/files#r960866165
|
||
export const itemToString = ( item: ItemType | null ) => { | ||
import { getItemLabelType } from './types'; | ||
export const defaultGetItemLabel = < ItemType >( item: ItemType | null ) => { | ||
return item ? item.label : ''; |
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.
Yeah, you can make us of the is
type guard -> https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
The easiest way to do this is by creating a isDefaultItemType
function, like so:
function isDefaultItemType(item: ItemType | null): item is ItemType {
return (item as ItemType).label !== undefined;
}
You could include the check for value
here as well if you wanted.
Then in the above code you can do:
if ( isDefaultItemType(item) ) {
return item.label;
} else {
return '';
}
TypeScript should not complain about item.label
within the if statement now.
|
||
export type getItemLabelType< ItemType > = ( item: ItemType | null ) => string; | ||
|
||
export type getItemValueType< ItemType > = ( item: ItemType | null ) => string; |
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.
Could we also allow the return of a number
-> string | number
for the value type? This is not a must if it will require a bunch of checks and conversions everywhere else, but if we can do it somewhat seamless then that would be really nice.
Thanks for the help @louwie17 ! I believe I remedied the above, so let me know what you think. 👂🏻 |
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 for the updates @joelclimbsthings, this tested well and the code looks great, very nice work!!
@joelclimbsthings it does look like your changelog Github action failed with the same error that Josh ran into ( p1663259087459849-slack-C03CPM3UXDJ ), I think it just required a rebase to fix. Let me know if you need a re-approval. |
850f0fc
to
6bc7edb
Compare
…optional getItemLabel and getItemValue props
6bc7edb
to
b8b9ef7
Compare
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.
Re-approving after rebase, LGTM 🚀
Hi @joelclimbsthings, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:
|
Changes proposed in this Pull Request:
Updates to the new experimental SelectControl to support passing a custom type to describe the items, as well as including
getItemValue()
andgetItemLabel()
functions as props to parse those items.Note: This PR was a bit adventurous for me as I wasn't entirely familiar with TS generic types, but it was a fun learning experience. The only issue I haven't been able to resolve is a couple TS errors within the utils for the default
item
andvalue
getters.Closes #34399 .
How to test the changes in this Pull Request:
npm run storybook
Experimental > SelectControl > Custom Item Type
story.Other information:
pnpm changelog add --filter=<project>
?FOR PR REVIEWER ONLY: