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

patch SelectControl with optgroups #28901

Closed

Conversation

JohnRDOrazio
Copy link
Contributor

@JohnRDOrazio JohnRDOrazio commented Feb 9, 2021

Description

Added a conditional check whether the array of objects passed into options contain simple options or contain grouped options, so as to allow for optgroups in the generated SelectControl.

How has this been tested?

I have forked a copy of the Gutenberg repo, applied this patch, and then built the gutenberg plugin zip. I installed the zipped gutenberg plugin into a running WordPress instance, then created a SelectControl component in the InspectorTools for an example block in the Block editor. Here is how I created this SelectControl:

EXAMPLE 1: GROUPED OPTIONS

createElement(SelectControl, {
	value: attributes.FOODPLATTER,
	label: 'Food platter',
	onChange: changeVersion,
	multiple: true,
	options: [ 
		{
			label: 'Fruits',
			value: [
				{ label: 'Apples', value: 'apple' },
				{ label: 'Pears', value: 'pear' },
				{ label: 'Bananas', value: 'banana' }
			]
		},
		{
			label: 'Vegetables',
			value: [
				{ label: 'Celery', value: 'celery' },
				{ label: 'Cucumber', value: 'cucumber' },
				{ label: 'Carrots', value: 'carrots' }
			]
		}
	],
})

I also created a second example, combining top-level options with grouped options, and it is working correctly. Top-level options appear just fine alongside grouped options.

EXAMPLE 2: GROUPED OPTIONS with TOP-LEVEL OPTIONS

createElement(SelectControl, {
	value: attributes.FOODPLATTER,
	label: 'Food platter',
	onChange: changeVersion,
	multiple: true,
	options: [ 
		{
			label: 'Fruits',
			value: [
				{ label: 'Apples', value: 'apple' },
				{ label: 'Pears', value: 'pear' },
				{ label: 'Bananas', value: 'banana' }
			]
		},
		{
			label: 'Cold cuts',
			value: 'coldcuts'
		},
		{
			label: 'Vegetables',
			value: [
				{ label: 'Celery', value: 'celery' },
				{ label: 'Cucumber', value: 'cucumber' },
				{ label: 'Carrots', value: 'carrots' }
			]
		}
	],
})

Screenshots

EXAMPLE 1
food platter

EXAMPLE 2
food platter 2

Types of changes

New feature : this patch enhances the SelectControl component so as to be more compliant with the html select input, allowing for optgroups.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • 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. (I don't believe this is applicable in this case?)

Fixes #26505

@github-actions
Copy link

github-actions bot commented Feb 9, 2021

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @JohnRDOrazio!. 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 Feb 9, 2021
under `Props -> options`, explain that `value` can be either a scalar or an array.
@stokesman
Copy link
Contributor

@JohnRDOrazio, congrats on figuring out the build and everything you need to get this far! As I understand it the SelectControl component is soon to be succeeded by one that already has support for option groups. So I'm guessing❓ that there may be little interest in adding features to the current one.

Base automatically changed from master to trunk March 1, 2021 15:45
@mkaz mkaz added the [Package] Components /packages/components label Oct 16, 2021
@skorasaurus
Copy link
Member

Thanks for your initial Pull request; I believe this has been implemented through #29540 (ref https://github.com/WordPress/gutenberg/tree/trunk/packages/components/src/select-control#development-guidelines)

Please reopen if I am mistaken.

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 [Package] Components /packages/components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OptGroup in SelectControl
4 participants