Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,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.

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

### Autogenerated widget ids
Expand Down
13 changes: 9 additions & 4 deletions src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ class ArrayField extends Component {
onDropIndexClick = (index) => {
return (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
};
};
Expand Down Expand Up @@ -175,12 +179,12 @@ class ArrayField extends Component {
autofocus,
} = this.props;
const title = (schema.title === undefined) ? name : schema.title;
const {items} = this.state;
const {definitions, fields} = this.props.registry;
const {TitleField, DescriptionField} = fields;
const itemsSchema = retrieveSchema(schema.items, definitions);
const {addable=true} = getUiOptions(uiSchema);

var {items} = this.state;
items = items || [];
return (
<fieldset
className={`field field-array field-array-of-${itemsSchema.type}`}>
Expand Down Expand Up @@ -220,7 +224,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 @@ -231,6 +235,7 @@ class ArrayField extends Component {
<Widget
id={idSchema && idSchema.$id}
multiple
required={required}
onChange={this.onSelectChange}
options={options}
schema={schema}
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;
}
}

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;
const disabledCls = disabled ? "disabled" : "";
const checkbox = (
<span>
Expand Down