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

Enable Arrays to be marked as required, and fixes for strings marked as required #442

Merged
merged 15 commits into from
Jan 20, 2017
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ const uiSchema = {
};
```

In addition it is valid to maked an array as "required". This means the user must select at least one items to add to the array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to maked? Looks like a typo. Maybe "to mark"?


See the "Arrays" section of the playground for cool demos.

### Autogenerated widget ids
Expand Down
9 changes: 7 additions & 2 deletions src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

};
};
Expand Down Expand Up @@ -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);
Expand All @@ -338,6 +342,7 @@ class ArrayField extends Component {
<Widget
id={idSchema && idSchema.$id}
multiple
required={required}
onChange={this.onSelectChange}
options={options}
schema={schema}
Expand Down
8 changes: 6 additions & 2 deletions src/components/widgets/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}}}/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const _onChange = ({target: {value}}) => {
  return props.onChange(value === "" ? undefined : value);
};
// ...
onChange={_onChange} />

);
}

Expand Down
9 changes: 7 additions & 2 deletions src/components/widgets/CheckboxesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Unnecessary wrapping parenthesis.

const disabledCls = disabled ? "disabled" : "";
const checkbox = (
<span>
Expand Down
7 changes: 6 additions & 1 deletion src/components/widgets/TextareaWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function TextareaWidget({
autofocus,
onChange
}) {
const _onChange = onChange;
return (
<textarea
id={id}
Expand All @@ -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);
}}}/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous impl. suggestion above.

);
}

Expand Down