Skip to content

Commit c355ddb

Browse files
committedFeb 23, 2019
Merge branch 'huhuaaa-feature/fix-enum-empty-bug'
2 parents f9d4c63 + 5aaf863 commit c355ddb

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed
 

‎src/components/widgets/SelectWidget.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ function SelectWidget(props) {
9191
const newValue = getValue(event, multiple);
9292
onChange(processValue(schema, newValue));
9393
}}>
94-
{!multiple && !schema.default && <option value="">{placeholder}</option>}
94+
{!multiple && schema.default === undefined && (
95+
<option value="">{placeholder}</option>
96+
)}
9597
{enumOptions.map(({ value, label }, i) => {
9698
const disabled = enumDisabled && enumDisabled.indexOf(value) != -1;
9799
return (

‎test/NumberField_test.js

+71
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,76 @@ describe("NumberField", () => {
290290

291291
expect(node.querySelector("select").id).eql("root");
292292
});
293+
294+
it("should render a select element with a blank option, when default value is not set.", () => {
295+
const schema = {
296+
type: "object",
297+
properties: {
298+
foo: {
299+
type: "number",
300+
enum: [0],
301+
},
302+
},
303+
};
304+
305+
const { node } = createFormComponent({
306+
schema,
307+
});
308+
309+
const selects = node.querySelectorAll("select");
310+
expect(selects[0].value).eql("");
311+
312+
const options = node.querySelectorAll("option");
313+
expect(options.length).eql(2);
314+
expect(options[0].innerHTML).eql("");
315+
});
316+
317+
it("should render a select element without a blank option, if a default value is set.", () => {
318+
const schema = {
319+
type: "object",
320+
properties: {
321+
foo: {
322+
type: "number",
323+
enum: [2],
324+
default: 2,
325+
},
326+
},
327+
};
328+
329+
const { node } = createFormComponent({
330+
schema,
331+
});
332+
333+
const selects = node.querySelectorAll("select");
334+
expect(selects[0].value).eql("2");
335+
336+
const options = node.querySelectorAll("option");
337+
expect(options.length).eql(1);
338+
expect(options[0].innerHTML).eql("2");
339+
});
340+
341+
it("should render a select element without a blank option, if the default value is 0.", () => {
342+
const schema = {
343+
type: "object",
344+
properties: {
345+
foo: {
346+
type: "number",
347+
enum: [0],
348+
default: 0,
349+
},
350+
},
351+
};
352+
353+
const { node } = createFormComponent({
354+
schema,
355+
});
356+
357+
const selects = node.querySelectorAll("select");
358+
expect(selects[0].value).eql("0");
359+
360+
const options = node.querySelectorAll("option");
361+
expect(options.length).eql(1);
362+
expect(options[0].innerHTML).eql("0");
363+
});
293364
});
294365
});

‎test/StringField_test.js

+63
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,69 @@ describe("StringField", () => {
350350

351351
expect(node.querySelector("#custom")).to.exist;
352352
});
353+
354+
it("should render a select element with first option 'false' if the default value is false", () => {
355+
const schema = {
356+
type: "object",
357+
properties: {
358+
foo: {
359+
type: "string",
360+
enum: [false, true],
361+
default: false,
362+
},
363+
},
364+
};
365+
366+
const { node } = createFormComponent({
367+
schema,
368+
});
369+
370+
const options = node.querySelectorAll("option");
371+
expect(options[0].innerHTML).eql("false");
372+
expect(options.length).eql(2);
373+
});
374+
375+
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.", () => {
376+
const schema = {
377+
type: "object",
378+
properties: {
379+
foo: {
380+
type: "string",
381+
enum: ["", "1"],
382+
default: "",
383+
},
384+
},
385+
};
386+
387+
const { node } = createFormComponent({
388+
schema,
389+
});
390+
391+
const options = node.querySelectorAll("option");
392+
expect(options[0].innerHTML).eql("");
393+
expect(options.length).eql(2);
394+
});
395+
396+
it("should render only one empty option when the default value is empty.", () => {
397+
const schema = {
398+
type: "object",
399+
properties: {
400+
foo: {
401+
type: "string",
402+
enum: [""],
403+
default: "",
404+
},
405+
},
406+
};
407+
408+
const { node } = createFormComponent({
409+
schema,
410+
});
411+
412+
const options = node.querySelectorAll("option");
413+
expect(options[0].innerHTML).eql("");
414+
expect(options.length).eql(1);
415+
});
353416
});
354417

355418
describe("TextareaWidget", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.