Skip to content
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

Merged
merged 9 commits into from
Sep 16, 2022

Conversation

joelclimbsthings
Copy link
Contributor

@joelclimbsthings joelclimbsthings commented Sep 1, 2022

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() and getItemLabel() 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 and value getters.

Closes #34399 .

How to test the changes in this Pull Request:

  1. Checkout branch.
  2. Fire up storybook with npm run storybook
  3. Observe and interact with the new Experimental > SelectControl > Custom Item Type story.
  4. Optionally including the Select control with a custom type (can copy from story) live on any react screen to interact with it there.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you created a changelog file for each project being changed, ie pnpm changelog add --filter=<project>?

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@github-actions github-actions bot added package: @woocommerce/components issues related to @woocommerce/components plugin: woocommerce Issues related to the WooCommerce Core plugin. labels Sep 1, 2022

export const itemToString = ( item: ItemType | null ) => {
import { getItemLabelType } from './types';
export const defaultGetItemLabel = < ItemType >( item: ItemType | null ) => {
return item ? item.label : '';
Copy link
Contributor Author

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?

Copy link
Contributor

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.

@joelclimbsthings joelclimbsthings marked this pull request as ready for review September 1, 2022 17:05
@joelclimbsthings joelclimbsthings requested a review from a team September 1, 2022 17:05
@joelclimbsthings joelclimbsthings self-assigned this Sep 1, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2022

Test Results Summary

Commit SHA: b8b9ef7

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests11800201200m 49s
E2E Tests186001018714m 54s
To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@louwie17 louwie17 self-requested a review September 14, 2022 18:16
Copy link
Contributor

@louwie17 louwie17 left a 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 : '';
Copy link
Contributor

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;
Copy link
Contributor

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.

@joelclimbsthings
Copy link
Contributor Author

Thanks for the help @louwie17 ! I believe I remedied the above, so let me know what you think. 👂🏻

@github-actions github-actions bot added the release: highlight Issues that have a high user impact and need to be discussed/paid attention to. label Sep 15, 2022
louwie17
louwie17 previously approved these changes Sep 16, 2022
Copy link
Contributor

@louwie17 louwie17 left a 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!!

@louwie17
Copy link
Contributor

@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.

@joelclimbsthings joelclimbsthings force-pushed the update/34399-select-control branch from 850f0fc to 6bc7edb Compare September 16, 2022 16:19
@github-actions github-actions bot removed the release: highlight Issues that have a high user impact and need to be discussed/paid attention to. label Sep 16, 2022
@joelclimbsthings joelclimbsthings force-pushed the update/34399-select-control branch from 6bc7edb to b8b9ef7 Compare September 16, 2022 18:25
@octaedro octaedro self-requested a review September 16, 2022 20:18
Copy link
Contributor

@louwie17 louwie17 left a 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 🚀

@joelclimbsthings joelclimbsthings merged commit 09bfa0b into trunk Sep 16, 2022
@joelclimbsthings joelclimbsthings deleted the update/34399-select-control branch September 16, 2022 20:35
@github-actions github-actions bot added this to the 7.1.0 milestone Sep 16, 2022
@github-actions
Copy link
Contributor

Hi @joelclimbsthings, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the release: add testing instructions label

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: @woocommerce/components issues related to @woocommerce/components plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Components] Allow generic item type in new experimental SelectControl
2 participants