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

feat(SelectControl): add children prop #29540

Merged

Conversation

frosso
Copy link
Contributor

@frosso frosso commented Mar 3, 2021

Description

It looks like the select-control component accepts an options prop, which is just an array of options that are mapped to <option /> elements.
<optgroup />s are a native part of JSX. However, they cannot be added through the current options prop.

I understand that options is provided as an abstraction, but I think that rather than changing the options prop, we can leverage the native children prop present on all the JSX elements and obtain the same result.
We could still leverage the options prop to add <optgroup /> elements, but where's the fun in that? It seems to me that by leveraging the children prop we can have a more semantical component. And with that we can just leverage <optgroup />s natively!

I kept the options prop, but maybe it can deprecated in favor of children?

How has this been tested?

I tested this in Storybook. I also added some unit tests for the component (there weren't any before).

Screenshots

https://d.pr/i/JvrB57

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions
Copy link

github-actions bot commented Mar 3, 2021

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @frosso! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 3, 2021
@frosso frosso marked this pull request as ready for review March 3, 2021 22:13
Copy link
Contributor

@talldan talldan 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 your contribution @frosso.

This sounds like a reasonable change, and probably how many would expect the component to work. From memory, I think there are other components that work like this too (maybe DropdownMenu?)

I think as part of this the README.md would need to be updated. Some of the docs may (or may not) need to be generated after modifying that file (npm run docs:build and then also commit and push any generated changes).

@@ -0,0 +1,82 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

A convention in the codebase is that test file name should try to map to the filename that's being tested in the parent folder, so here it should be called index.js (and it doesn't need the test.js postfix).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks - done in 6c4e3a2

Copy link
Contributor Author

@frosso frosso 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 taking the time to review @talldan !
I made an update to the README.md in 18872a4. Running npm run docs:build didn't seem to create additional changes.
Let me know if there's anything else!

@@ -0,0 +1,82 @@
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks - done in 6c4e3a2

@frosso frosso requested a review from talldan April 14, 2021 14:07
Conflicts:
	packages/components/src/select-control/index.tsx
Copy link
Contributor

@ajlende ajlende left a comment

Choose a reason for hiding this comment

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

I resolved the conflicts with trunk and everything works as expected. Looks like @talldan's comments have all been resolved too, so I think this looks good to go 👍

@mkaz mkaz merged commit a106432 into WordPress:trunk Oct 16, 2021
@github-actions github-actions bot added this to the Gutenberg 11.8 milestone Oct 16, 2021
@frosso frosso deleted the frosso/select-control-allow-children-prop branch October 21, 2021 17:32
Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

Hey @frosso , it looks like this change introduced some typescript errors in the package, since the compiler now thinks that the children prop is mandatory on SelectControl (for example, here) — would you be able to work on a fix to mark children as optional?

Something like this should be enough
diff --git a/packages/components/src/select-control/index.tsx b/packages/components/src/select-control/index.tsx
index 7f68f56721..ec2afed60d 100644
--- a/packages/components/src/select-control/index.tsx
+++ b/packages/components/src/select-control/index.tsx
@@ -4,7 +4,7 @@
 import { isEmpty, noop } from 'lodash';
 import classNames from 'classnames';
 // eslint-disable-next-line no-restricted-imports
-import type { ChangeEvent, FocusEvent, Ref } from 'react';
+import type { ChangeEvent, FocusEvent, ReactNode, Ref } from 'react';
 
 /**
  * WordPress dependencies
@@ -31,7 +31,7 @@ function useUniqueId( idProp?: string ) {
 }
 
 export interface SelectControlProps
-	extends Omit< InputBaseProps, 'isFocused' > {
+	extends Omit< InputBaseProps, 'children' | 'isFocused' > {
 	help?: string;
 	hideLabelFromVision?: boolean;
 	multiple?: boolean;
@@ -50,6 +50,7 @@ export interface SelectControlProps
 	size?: Size;
 	value?: string | string[];
 	labelPosition?: LabelPosition;
+	children?: ReactNode;
 }
 
 function SelectControl(

Also, when making changes on the @wordpress/components package, we usually add an entry to the CHANGELOG. You can find more information regarding best practices in the contributing guidelines.

Finally, feel free to ping myself and/or @mirka in any future PR concerting @wordpress/components. Thank you!


An alternative to the `options` prop.
Use the `children` prop to have more control on the style of the items being rendered, like `optgroup`s or `options` and possibly avoid re-rendering due to the reference update on the `options` prop.
- Type: `Element`
Copy link
Contributor

Choose a reason for hiding this comment

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

ReactNode would be a better choice for describing children 's type

@ciampo
Copy link
Contributor

ciampo commented Jan 11, 2022

Opened #37872 with the fix suggested in the previous comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants