-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Enable Arrays to be marked as required, and fixes for strings marked as required #442
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 4 commits
c30f5b4
cc91fa2
8ecafc2
5091525
8e2ecf1
e0fe72e
b826fc8
2e3c712
477d7de
ea934ed
fe28e93
5f15775
7e7a925
4026366
0e05f7b
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 |
|---|---|---|
|
|
@@ -216,8 +216,12 @@ class ArrayField extends Component { | |
| if (event) { | ||
| event.preventDefault(); | ||
| } | ||
| var newitems = this.state.items.filter((_, i) => i !== index); | ||
| if (newitems.length == 0) { | ||
| newitems = undefined; | ||
| } | ||
| this.asyncSetState({ | ||
| items: this.state.items.filter((_, i) => i !== index) | ||
| items: newitems | ||
| }, {validate: true}); // refs #195 | ||
|
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. const items = this.state.items.filter((_, i) => i !== index);
this.asyncSetState({
items: items.length > 0 ? items : undefined,
}, {validate: true}); // refs #195 |
||
| }; | ||
| }; | ||
|
|
@@ -327,7 +331,7 @@ class ArrayField extends Component { | |
| } | ||
|
|
||
| renderMultiSelect() { | ||
| const {schema, idSchema, uiSchema, disabled, readonly, autofocus} = this.props; | ||
| const {schema, idSchema, uiSchema, disabled, required, readonly, autofocus} = this.props; | ||
| const {items} = this.state; | ||
| const {widgets, definitions} = this.props.registry; | ||
| const itemsSchema = retrieveSchema(schema.items, definitions); | ||
|
|
@@ -338,6 +342,7 @@ class ArrayField extends Component { | |
| <Widget | ||
| id={idSchema && idSchema.$id} | ||
| multiple | ||
| required={required} | ||
| onChange={this.onSelectChange} | ||
| options={options} | ||
| schema={schema} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,21 +8,25 @@ function BaseInput(props) { | |
| value, | ||
| readonly, | ||
| autofocus, | ||
| onChange, | ||
| options, // eslint-disable-line | ||
| schema, // eslint-disable-line | ||
| formContext, // eslint-disable-line | ||
| registry, // eslint-disable-line | ||
| ...inputProps | ||
| } = props; | ||
| const _onChange = props.onChange; | ||
| return ( | ||
| <input | ||
| {...inputProps} | ||
| className="form-control" | ||
| readOnly={readonly} | ||
| autoFocus={autofocus} | ||
| value={typeof value === "undefined" ? "" : value} | ||
| onChange={(event) => onChange(event.target.value)}/> | ||
| onChange={(event) => {if (event.target.value == "") { | ||
| return _onChange(undefined); | ||
| } else { | ||
| return _onChange(event.target.value); | ||
| }}}/> | ||
|
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. const _onChange = ({target: {value}}) => {
return props.onChange(value === "" ? undefined : value);
};
// ...
onChange={_onChange} /> |
||
| ); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ function selectValue(value, selected, all) { | |
| } | ||
|
|
||
| function deselectValue(value, selected) { | ||
| return selected.filter(v => v !== value); | ||
| var ret = selected.filter(v => v !== value); | ||
| if (ret.length == 0) { | ||
| return undefined; | ||
| } else { | ||
| return ret; | ||
| } | ||
|
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. const ret = selected.filter(v => v !== value);
return ret.length === 0 ? undefined : ret; |
||
| } | ||
|
|
||
| function CheckboxesWidget(props) { | ||
|
|
@@ -19,7 +24,7 @@ function CheckboxesWidget(props) { | |
| return ( | ||
| <div className="checkboxes" id={id}>{ | ||
| enumOptions.map((option, index) => { | ||
| const checked = value.indexOf(option.value) !== -1; | ||
| const checked = (value === undefined) ? false : value.indexOf(option.value) !== -1; | ||
|
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. Nit: Unnecessary wrapping parenthesis. |
||
| const disabledCls = disabled ? "disabled" : ""; | ||
| const checkbox = ( | ||
| <span> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ function TextareaWidget({ | |
| autofocus, | ||
| onChange | ||
| }) { | ||
| const _onChange = onChange; | ||
| return ( | ||
| <textarea | ||
| id={id} | ||
|
|
@@ -22,7 +23,11 @@ function TextareaWidget({ | |
| disabled={disabled} | ||
| readOnly={readonly} | ||
| autoFocus={autofocus} | ||
| onChange={(event) => onChange(event.target.value)}/> | ||
| onChange={(event) => {if (event.target.value == "") { | ||
| return _onChange(undefined); | ||
| } else { | ||
| return _onChange(event.target.value); | ||
| }}}/> | ||
|
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. See previous impl. suggestion above. |
||
| ); | ||
| } | ||
|
|
||
|
|
||
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'm realizing slightly late that this is overlapping with the
minItemsjsonschema directive. I think the jsonschema spec wants arequiredobject array property to be an array, possibly an empty one. With jsonschema, if we wanted at least one item in our array, we'd setminItemsto1instead of relying on therequireddirective.So that's a strong direction we're taking here, we're officially diverging from the jsonschema spec to fit Web forms use cases & habits. Is everybody okay with this?
Would appreciate inputs from @MoOx @maartenth @olzraiti @leplatrem @Natim @dehli
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.
Fair call. I wasn't aware of that directive. If we can achieve a similar effect with existing code that is standards compliant I would certainly support that.
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.
Indeed that's a serious diff. Not sure I am comfortable with that but I guess we can't have the silver bullet.
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.
The SelectWidget is an interesting case. In truth I don't use it much because I've written my own custom widget based on react-select which better suited my needs for a nullable and multiselect-able drop down widget. (Happy to share if anyone wants it, but I thought since it was an extra dependency it was better off outside the core project.)
I could be convinced to do it here as well, since of course the "required" tag won't work, and while I prefer react-select for this case, perhaps like dates we possibly should cater for the most general case.
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 wouldn't affect my code base, but I'm not sure if we would benefit enough from adding this considering the consequences.
We aren't extending the json schema spec in a way that would make something possible that was impossible before (using
minItemprovides the functionality that is wanted here). We are merely making something more convenient than before, with the cost of breaking compatibility with json schema spec.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.
The issue is that people import existing schemas and expect them to work with forms with no need to tweak their schemas (uiSchemas have been introduced for that specific purpose).
That could be addressed with an appropriate FAQ/Caveats section though. The same way we could document the
minItemsjsonschema directive.That's indeed a fair point. Another approach would be to make the difference actually visible, but that's rather tricky to get right, and would probably clutter UX a lot. Also this would have to work with bootstrap and other styles.
So my current thinking is:
minItemsjsonschema directive.SelectWidgetas it is, users should provide enum values matching their expectations.What do people think?
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 agree with you @n1k0 . I'm currently using minItems in my application b/c that's the
json-schemaway of doing it. If we added documentation describing it, then it'll be more obvious that it's the way to do it.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.
Sounds good to me! Empty string as undefined seems well-grounded.
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'm OK with your proposed solution, @n1k0. I think it is an improvement to the current situation either way.
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.
Good, @crumblix would you mind updating the patch accordingly if you agree with this decision? :)
Thanks a bunch 👍