Skip to content
Merged
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
1 change: 1 addition & 0 deletions playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class App extends Component {
uiSchema={uiSchema}
formData={formData}
onChange={this.onFormDataChange}
onSubmit={({formData}) => console.log("submitted formData", formData)}
fields={{geo: GeoPosition}}
validate={validate}
onBlur={(id, value) => console.log(`Touched ${id} with value ${value}`)}
Expand Down
53 changes: 28 additions & 25 deletions src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ class ArrayField extends Component {
return schema.items.title || schema.items.description || "Item";
}

isItemRequired(itemsSchema) {
return itemsSchema.type === "string" && itemsSchema.minLength > 0;
isItemRequired(itemSchema) {
if (Array.isArray(itemSchema.type)) {
// While we don't yet support composite/nullable jsonschema types, it's
// future-proof to check for requirement against these.
return !itemSchema.type.includes("null");
}
// All non-null array item types are inherently required by design
return itemSchema.type !== "null";
}

onAddClick = (event) => {
Expand All @@ -191,10 +197,9 @@ class ArrayField extends Component {
if (event) {
event.preventDefault();
}
this.props.onChange(
this.props.formData.filter((_, i) => i !== index),
{validate: true} // refs #195
);
const {formData, onChange} = this.props;
// refs #195: revalidate to ensure properly reindexing errors
onChange(formData.filter((_, i) => i !== index), {validate: true});
};
};

Expand All @@ -204,30 +209,28 @@ class ArrayField extends Component {
event.preventDefault();
event.target.blur();
}
const items = this.props.formData;
this.props.onChange(
items.map((item, i) => {
if (i === newIndex) {
return items[index];
} else if (i === index) {
return items[newIndex];
} else {
return item;
}
}),
{validate: true}
);
const {formData, onChange} = this.props;
onChange(formData.map((item, i) => {
if (i === newIndex) {
return formData[index];
} else if (i === index) {
return formData[newIndex];
} else {
return item;
}
}), {validate: true});
};
};

onChangeForIndex = (index) => {
return (value) => {
this.props.onChange(
this.props.formData.map((item, i) => {
return index === i ? value : item;
}),
{validate: false}
);
const {formData, onChange} = this.props;
onChange(formData.map((item, i) => {
// We need to treat undefined items as nulls to have validation.
// See https://github.com/tdegrunt/jsonschema/issues/206
const jsonValue = typeof value === "undefined" ? null : value;
return index === i ? jsonValue : item;
}), {validate: false});
};
};

Expand Down
3 changes: 1 addition & 2 deletions src/components/fields/BooleanField.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {PropTypes} from "react";

import {
defaultFieldValue,
getWidget,
getUiOptions,
optionsList,
Expand Down Expand Up @@ -36,7 +35,7 @@ function BooleanField(props) {
id={idSchema && idSchema.$id}
onChange={onChange}
label={title === undefined ? name : title}
value={defaultFieldValue(formData, schema)}
value={formData}
required={required}
disabled={disabled}
readonly={readonly}
Expand Down
3 changes: 1 addition & 2 deletions src/components/fields/StringField.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {PropTypes} from "react";

import {
defaultFieldValue,
getWidget,
getUiOptions,
optionsList,
Expand Down Expand Up @@ -36,7 +35,7 @@ function StringField(props) {
schema={schema}
id={idSchema && idSchema.$id}
label={title === undefined ? name : title}
value={defaultFieldValue(formData, schema)}
value={formData}
onChange={onChange}
onBlur={onBlur}
required={required}
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function BaseInput(props) {
className="form-control"
readOnly={readonly}
autoFocus={autofocus}
value={typeof value === "undefined" ? "" : value}
value={value == null ? "" : value}
onChange={_onChange}
onBlur={onBlur && (event => onBlur(inputProps.id, event.target.value))}/>
);
Expand Down
4 changes: 0 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export function getDefaultRegistry() {
return defaultRegistry;
}

export function defaultFieldValue(formData, schema) {
return typeof formData === "undefined" ? schema.default : formData;
}

export function getWidget(schema, widget, registeredWidgets={}) {
const {type} = schema;

Expand Down
39 changes: 39 additions & 0 deletions test/ArrayField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ describe("ArrayField", () => {
.to.have.length.of(1);
});

it("should mark a non-null array item widget as required", () => {
const {node} = createFormComponent({schema});

Simulate.click(node.querySelector(".array-item-add button"));

expect(node.querySelector(".field-string input[type=text]").required)
.eql(true);
});

it("should fill an array field with data", () => {
const {node} = createFormComponent({schema, formData: ["foo", "bar"]});
const inputs = node.querySelectorAll(".field-string input[type=text]");
Expand Down Expand Up @@ -229,6 +238,26 @@ describe("ArrayField", () => {
.to.have.length.of(0);
});

it("should handle cleared field values in the array", () => {
const schema = {
type: "array",
items: {type: "integer"},
};
const formData = [1, 2, 3];
const {comp, node} = createFormComponent({
liveValidate: true,
schema,
formData,
});

Simulate.change(node.querySelector("#root_1"), {
target: {value: ""}
});

expect(comp.state.formData).eql([1, null, 3]);
expect(comp.state.errors).to.have.length.of(1);
});

it("should render the input widgets with the expected ids", () => {
const {node} = createFormComponent({schema, formData: ["foo", "bar"]});

Expand Down Expand Up @@ -590,6 +619,16 @@ describe("ArrayField", () => {
expect(numInput.id).eql("root_1");
});

it("should mark non-null item widgets as required", () => {
const {node} = createFormComponent({schema});
const strInput =
node.querySelector("fieldset .field-string input[type=text]");
const numInput =
node.querySelector("fieldset .field-number input[type=text]");
expect(strInput.required).eql(true);
expect(numInput.required).eql(true);
});

it("should fill fields with data", () => {
const {node} = createFormComponent({schema, formData: ["foo", 42]});
const strInput =
Expand Down
17 changes: 17 additions & 0 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,23 @@ describe("Form", () => {
});
});

describe("Default value handling on clear", () => {
const schema = {
type: "string",
default: "foo",
};

it("should not set default when a text field is cleared", () => {
const {node} = createFormComponent({schema, formData: "bar"});

Simulate.change(node.querySelector("input"), {
target: {value: ""}
});

expect(node.querySelector("input").value).eql("");
});
});

describe("Defaults array items default propagation", () => {
const schema = {
type: "object",
Expand Down