-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Allow empty option for enum fields #451
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
Changes from 3 commits
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 |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ import {asNumber} from "../../utils"; | |
| * always retrieved as strings. | ||
| */ | ||
| function processValue({type, items}, value) { | ||
| if (type === "array" && items && ["number", "integer"].includes(items.type)) { | ||
| if (value === "") { | ||
| return undefined; | ||
|
Collaborator
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. We need to add a warning somewhere in the docs, because if one adds
Contributor
Author
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. I added a note, but I'm not sure how well I explained it or if there's a better spot. |
||
| } else if (type === "array" && items && ["number", "integer"].includes(items.type)) { | ||
| return value.map(asNumber); | ||
| } else if (type === "boolean") { | ||
| return value === "true"; | ||
|
|
@@ -38,15 +40,17 @@ function SelectWidget({ | |
| multiple, | ||
| autofocus, | ||
| onChange, | ||
| onBlur | ||
| onBlur, | ||
| placeholder | ||
| }) { | ||
| const {enumOptions} = options; | ||
| const emptyValue = multiple ? [] : ""; | ||
| return ( | ||
| <select | ||
| id={id} | ||
| multiple={multiple} | ||
| className="form-control" | ||
| value={value} | ||
| value={typeof value === "undefined" ? emptyValue : value} | ||
| required={required} | ||
| disabled={disabled} | ||
| readOnly={readonly} | ||
|
|
@@ -58,11 +62,12 @@ function SelectWidget({ | |
| onChange={(event) => { | ||
| const newValue = getValue(event, multiple); | ||
| onChange(processValue(schema, newValue)); | ||
| }}>{ | ||
| enumOptions.map(({value, label}, i) => { | ||
| }}> | ||
| {!multiple && !schema.default && <option value="">{placeholder}</option>} | ||
| {enumOptions.map(({value, label}, i) => { | ||
| return <option key={i} value={value}>{label}</option>; | ||
| }) | ||
| }</select> | ||
| })} | ||
| </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.
I think I'd move this in new section under Form data validation, eg. The case of empty strings.