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

OptGroup in SelectControl #26505

Closed
raghavnarang opened this issue Oct 27, 2020 · 5 comments
Closed

OptGroup in SelectControl #26505

raghavnarang opened this issue Oct 27, 2020 · 5 comments
Assignees
Labels
[Package] Components /packages/components [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@raghavnarang
Copy link

raghavnarang commented Oct 27, 2020

Description of feature request
By checking the code of the SelectControl component. It seems that it doesn't support Optgroup tag to group the options of the select HTML element.

OptGroup Reference Link
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup

Proposed Solution
It would be nice if we have OptGroup support in SelectControl component, by passing an array of groups and fields as a nested array in their respective Group.

{ options.map( ( option, index ) => {
const key =
option.id ||
`${ option.label }-${ option.value }-${ index }`;
return (
<option
key={ key }
value={ option.value }
disabled={ option.disabled }
>
{ option.label }
</option>
);
} ) }

@talldan talldan added [Package] Components /packages/components [Status] Needs More Info Follow-up required in order to be actionable. labels Oct 28, 2020
@talldan
Copy link
Contributor

talldan commented Oct 28, 2020

@raghavnarang The issue description is a bit brief, would you mind updating it with more information? Thanks.

@raghavnarang
Copy link
Author

@raghavnarang The issue description is a bit brief, would you mind updating it with more information? Thanks.

@talldan Issue description updated

@talldan talldan added [Type] Enhancement A suggestion for improvement. and removed [Status] Needs More Info Follow-up required in order to be actionable. labels Oct 28, 2020
@talldan
Copy link
Contributor

talldan commented Oct 28, 2020

Thanks!

@JohnRDOrazio
Copy link
Contributor

JohnRDOrazio commented Feb 7, 2021

I created and tested successfully a patch that introduces this functionality. Now both top-level options and optgroups are supported. Here is the result:

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' }
			]
		}
	],
})

food platter

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' }
			]
		}
	],
})

food platter 2

@skorasaurus
Copy link
Member

This has been fixed in #29540 with an example at https://github.com/WordPress/gutenberg/tree/trunk/packages/components/src/select-control#development-guidelines

Please reopen and comment if this incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants