Skip to content

Commit

Permalink
Merge branch 'feature/fix-enum-empty-bug' of https://github.com/huhua…
Browse files Browse the repository at this point in the history
…aa/react-jsonschema-form into huhuaaa-feature/fix-enum-empty-bug
  • Loading branch information
epicfaace committed Feb 23, 2019
2 parents f9d4c63 + 4d17bd8 commit 9153444
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/widgets/SelectWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function SelectWidget(props) {
const newValue = getValue(event, multiple);
onChange(processValue(schema, newValue));
}}>
{!multiple && !schema.default && <option value="">{placeholder}</option>}
{!multiple && schema.default === undefined && (
<option value="">{placeholder}</option>
)}
{enumOptions.map(({ value, label }, i) => {
const disabled = enumDisabled && enumDisabled.indexOf(value) != -1;
return (
Expand Down
77 changes: 77 additions & 0 deletions test/NumberField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,82 @@ describe("NumberField", () => {

expect(node.querySelector("select").id).eql("root");
});

it("should render a select element if set the enum.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "number",
enum: [0],
},
},
};

const { node } = createFormComponent({
schema,
});

const selects = node.querySelectorAll("select");
expect(selects).to.have.length.of(1);
});

it("should render a select element and it's value is empty, if set the enum and the default value is undefined.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "number",
enum: [0],
},
},
};

const { node } = createFormComponent({
schema,
});

const selects = node.querySelectorAll("select");
expect(selects[0].value).eql("");
});

it("should render a select element and it's first option has an empty innerHTML, if set the enum and the default value is undefined.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "number",
enum: [0],
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options[0].innerHTML).eql("");
});

it("should render a select element and it's first option is '0', if set the enum and the default value is 0.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "number",
enum: [0],
default: 0,
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options[0].innerHTML).eql("0");
});
});
});
80 changes: 80 additions & 0 deletions test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,86 @@ describe("StringField", () => {

expect(node.querySelector("#custom")).to.exist;
});

it("should render a select element and it's first option is 'false', if set the enum and the default value is false.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "string",
enum: [false, true],
default: false,
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options[0].innerHTML).eql("false");
});

it("should render a select element and the option's length is equal the enum's length, if set the enum.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "string",
enum: [false, true],
default: false,
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options.length).eql(2);
});

it("should render a select element and the option's length is equal the enum's length, if set the enum and the default value is empty.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "string",
enum: ["", "1"],
default: "",
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options.length).eql(2);
});

it("shouldn't render two empty options, when the default value is empty.", () => {
const schema = {
type: "object",
properties: {
foo: {
type: "string",
enum: [""],
default: "",
},
},
};

const { node } = createFormComponent({
schema,
});

const options = node.querySelectorAll("option");
expect(options.length).eql(1);
});
});

describe("TextareaWidget", () => {
Expand Down

0 comments on commit 9153444

Please sign in to comment.