-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(core): add support for <optgroup>
tag
#4374
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
e092cab
df18e6a
d22e271
ed880d9
4a29fc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
onFocus, | ||
placeholder, | ||
}: WidgetProps<T, S, F>) { | ||
const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options; | ||
const { enumOptions, enumDisabled, emptyValue: optEmptyVal, optgroups } = options; | ||
const emptyValue = multiple ? [] : ''; | ||
|
||
const handleFocus = useCallback( | ||
|
@@ -67,7 +67,37 @@ | |
); | ||
|
||
const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple); | ||
const showPlaceholderOption = !multiple && schema.default === undefined; | ||
|
||
let opts = null | ||
Check failure on line 71 in packages/core/src/components/widgets/SelectWidget.tsx
|
||
if (optgroups) { | ||
let enumOptionFromValue = new Map() | ||
Check failure on line 73 in packages/core/src/components/widgets/SelectWidget.tsx
|
||
enumOptions.forEach((enumOption, i) => { | ||
enumOptionFromValue.set(enumOption.value, [enumOption, i]) | ||
Check failure on line 75 in packages/core/src/components/widgets/SelectWidget.tsx
|
||
}) | ||
Check failure on line 76 in packages/core/src/components/widgets/SelectWidget.tsx
|
||
opts = Object.keys(optgroups).map(optgroup => { | ||
Check failure on line 77 in packages/core/src/components/widgets/SelectWidget.tsx
|
||
return ( | ||
<optgroup key={optgroup} label={optgroup}>{optgroups[optgroup].map(value => { | ||
if (!enumOptionFromValue.has(value)) return null | ||
const disabled = enumDisabled && enumDisabled.indexOf(value) !== -1; | ||
let [{ label }, i] = enumOptionFromValue.get(value) | ||
return ( | ||
<option key={i} value={String(i)} disabled={disabled}> | ||
{label} | ||
</option> | ||
); | ||
Comment on lines
+81
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate code here, minus the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the suggestion below, this would look like:
|
||
})}</optgroup> | ||
) | ||
}) | ||
} else { | ||
opts = Array.isArray(enumOptions) && enumOptions.map(({ value, label }, i) => { | ||
const disabled = enumDisabled && enumDisabled.indexOf(value) !== -1; | ||
return ( | ||
<option key={i} value={String(i)} disabled={disabled}> | ||
{label} | ||
</option> | ||
); | ||
Comment on lines
+93
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicate code to what is above. Consider creating a new component at the top of the file to render it and use it here and above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like a helper component in the file:
Then this would look like:
|
||
}); | ||
} | ||
|
||
return ( | ||
<select | ||
|
@@ -84,16 +114,8 @@ | |
onChange={handleChange} | ||
aria-describedby={ariaDescribedByIds<T>(id)} | ||
> | ||
{showPlaceholderOption && <option value=''>{placeholder}</option>} | ||
{Array.isArray(enumOptions) && | ||
enumOptions.map(({ value, label }, i) => { | ||
const disabled = enumDisabled && enumDisabled.indexOf(value) !== -1; | ||
return ( | ||
<option key={i} value={String(i)} disabled={disabled}> | ||
{label} | ||
</option> | ||
); | ||
})} | ||
{!multiple && schema.default === undefined && <option value=''>{placeholder}</option>} | ||
{opts} | ||
</select> | ||
); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
This will now be
5.25.0
since we just released5.24.0