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

Allow empty option for enum fields #451

Merged

Conversation

jbalboni
Copy link
Contributor

@jbalboni jbalboni commented Jan 24, 2017

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

  • I'm updating documentation
    • I've checked the rendering of the Markdown text I've added
    • If I'm adding a new section, I've updated the Table of Content
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

Copy link
Collaborator

@n1k0 n1k0 left a 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", () => {
Copy link
Collaborator

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;
Copy link
Collaborator

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.

Copy link
Contributor Author

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`.
Copy link
Collaborator

@n1k0 n1k0 Jan 27, 2017

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.

@n1k0 n1k0 merged commit a0ffadf into rjsf-team:master Jan 27, 2017
@n1k0
Copy link
Collaborator

n1k0 commented Jan 27, 2017

Perfect, thank you!

n1k0 added a commit that referenced this pull request Jan 28, 2017
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.
@n1k0
Copy link
Collaborator

n1k0 commented Jan 28, 2017

Released in v0.42.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants