-
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
Allow empty option for enum fields #451
Allow empty option for enum fields #451
Conversation
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.
Two nits and we're good to go, thank you!
@@ -181,6 +206,19 @@ describe("StringField", () => { | |||
expect(comp.state.formData).eql("foo"); | |||
}); | |||
|
|||
it("should reflect undefined into form state is empty option selected", () => { |
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.
Nit: s/is/if
@@ -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; |
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.
We need to add a warning somewhere in the docs, because if one adds ""
as an enum choice in their schema, it will be automatically converted to undefined
and drop the property from any parent object - which is now consistent with our text inputs but may be surprising to users.
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 added a note, but I'm not sure how well I explained it or if there's a better spot.
@@ -681,6 +682,10 @@ Form component supports the following html attributes: | |||
schema={} /> | |||
``` | |||
|
|||
### Enum fields | |||
|
|||
String fields that use `enum` and a `select` widget will have an empty option in the options list. When a user selects that option, the field will be set to `undefined` (similar to how regular `string` fields work if the field is empty). This also means that if you have an empty string in your `enum` array, selecting that option will cause the field to be set to `undefined`. |
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.
Perfect, thank you! |
Breaking changes When a text input is emptied by the user, it's value is now reset to `undefined` instead of being set to `""` (empty string) as previously. This better matches traditional HTML forms behavior. New features * Add an array field template component (#437) * Wrap radio and checkbox labels into span to enable styling. (#428) * Reset text inputs value when emptied (#442) * Add transform hook for changing errors from json schema validation (#432) * Add a noHtml5Validate prop (#448) * Add support for a onBlur event handler (#431) * Allow empty option for enum fields (#451) Bugfixes * Fix #452: Support recursively referenced definitions. (#453) * Fix #454: Document what master actually is, suffix its version with -dev.
Released in v0.42.0. |
Reasons for making this change
This removes the logic that defaults enum fields to the first item and updates the date and select widgets to use an empty default option when a field is undefined.
This ended up being more difficult to get right at the widget level than I thought and I've essentially skipped making this change affect
multiple
arrays. This needs some careful checking for the cases that aren't on the simpler string field using a select widget path.Closes #449.
Checklist