-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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(core): add support for <optgroup>
tag
#4374
base: main
Are you sure you want to change the base?
Changes from all 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 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend | |
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 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend | |
); | ||
|
||
const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple); | ||
const showPlaceholderOption = !multiple && schema.default === undefined; | ||
|
||
let opts = null | ||
if (optgroups) { | ||
let enumOptionFromValue = new Map() | ||
enumOptions.forEach((enumOption, i) => { | ||
enumOptionFromValue.set(enumOption.value, [enumOption, i]) | ||
}) | ||
opts = Object.keys(optgroups).map(optgroup => { | ||
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 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend | |
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> | ||
); | ||
} | ||
|
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.24.0
since we just released5.23.0