File tree 3 files changed +160
-1
lines changed
3 files changed +160
-1
lines changed Original file line number Diff line number Diff line change @@ -91,7 +91,9 @@ function SelectWidget(props) {
91
91
const newValue = getValue ( event , multiple ) ;
92
92
onChange ( processValue ( schema , newValue ) ) ;
93
93
} } >
94
- { ! multiple && ! schema . default && < option value = "" > { placeholder } </ option > }
94
+ { ! multiple && schema . default === undefined && (
95
+ < option value = "" > { placeholder } </ option >
96
+ ) }
95
97
{ enumOptions . map ( ( { value, label } , i ) => {
96
98
const disabled = enumDisabled && enumDisabled . indexOf ( value ) != - 1 ;
97
99
return (
Original file line number Diff line number Diff line change @@ -290,5 +290,82 @@ describe("NumberField", () => {
290
290
291
291
expect ( node . querySelector ( "select" ) . id ) . eql ( "root" ) ;
292
292
} ) ;
293
+
294
+ it ( "should render a select element if set the enum." , ( ) => {
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 ) . to . have . length . of ( 1 ) ;
311
+ } ) ;
312
+
313
+ it ( "should render a select element and it's value is empty, if set the enum and the default value is undefined." , ( ) => {
314
+ const schema = {
315
+ type : "object" ,
316
+ properties : {
317
+ foo : {
318
+ type : "number" ,
319
+ enum : [ 0 ] ,
320
+ } ,
321
+ } ,
322
+ } ;
323
+
324
+ const { node } = createFormComponent ( {
325
+ schema,
326
+ } ) ;
327
+
328
+ const selects = node . querySelectorAll ( "select" ) ;
329
+ expect ( selects [ 0 ] . value ) . eql ( "" ) ;
330
+ } ) ;
331
+
332
+ 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." , ( ) => {
333
+ const schema = {
334
+ type : "object" ,
335
+ properties : {
336
+ foo : {
337
+ type : "number" ,
338
+ enum : [ 0 ] ,
339
+ } ,
340
+ } ,
341
+ } ;
342
+
343
+ const { node } = createFormComponent ( {
344
+ schema,
345
+ } ) ;
346
+
347
+ const options = node . querySelectorAll ( "option" ) ;
348
+ expect ( options [ 0 ] . innerHTML ) . eql ( "" ) ;
349
+ } ) ;
350
+
351
+ it ( "should render a select element and it's first option is '0', if set the enum and the default value is 0." , ( ) => {
352
+ const schema = {
353
+ type : "object" ,
354
+ properties : {
355
+ foo : {
356
+ type : "number" ,
357
+ enum : [ 0 ] ,
358
+ default : 0 ,
359
+ } ,
360
+ } ,
361
+ } ;
362
+
363
+ const { node } = createFormComponent ( {
364
+ schema,
365
+ } ) ;
366
+
367
+ const options = node . querySelectorAll ( "option" ) ;
368
+ expect ( options [ 0 ] . innerHTML ) . eql ( "0" ) ;
369
+ } ) ;
293
370
} ) ;
294
371
} ) ;
Original file line number Diff line number Diff line change @@ -350,6 +350,86 @@ describe("StringField", () => {
350
350
351
351
expect ( node . querySelector ( "#custom" ) ) . to . exist ;
352
352
} ) ;
353
+
354
+ it ( "should render a select element and it's first option is 'false', if set the enum and 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
+ } ) ;
373
+
374
+ it ( "should render a select element and the option's length is equal the enum's length, if set the enum." , ( ) => {
375
+ const schema = {
376
+ type : "object" ,
377
+ properties : {
378
+ foo : {
379
+ type : "string" ,
380
+ enum : [ false , true ] ,
381
+ default : false ,
382
+ } ,
383
+ } ,
384
+ } ;
385
+
386
+ const { node } = createFormComponent ( {
387
+ schema,
388
+ } ) ;
389
+
390
+ const options = node . querySelectorAll ( "option" ) ;
391
+ expect ( options . length ) . eql ( 2 ) ;
392
+ } ) ;
393
+
394
+ 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." , ( ) => {
395
+ const schema = {
396
+ type : "object" ,
397
+ properties : {
398
+ foo : {
399
+ type : "string" ,
400
+ enum : [ "" , "1" ] ,
401
+ default : "" ,
402
+ } ,
403
+ } ,
404
+ } ;
405
+
406
+ const { node } = createFormComponent ( {
407
+ schema,
408
+ } ) ;
409
+
410
+ const options = node . querySelectorAll ( "option" ) ;
411
+ expect ( options . length ) . eql ( 2 ) ;
412
+ } ) ;
413
+
414
+ it ( "shouldn't render two empty options, when the default value is empty." , ( ) => {
415
+ const schema = {
416
+ type : "object" ,
417
+ properties : {
418
+ foo : {
419
+ type : "string" ,
420
+ enum : [ "" ] ,
421
+ default : "" ,
422
+ } ,
423
+ } ,
424
+ } ;
425
+
426
+ const { node } = createFormComponent ( {
427
+ schema,
428
+ } ) ;
429
+
430
+ const options = node . querySelectorAll ( "option" ) ;
431
+ expect ( options . length ) . eql ( 1 ) ;
432
+ } ) ;
353
433
} ) ;
354
434
355
435
describe ( "TextareaWidget" , ( ) => {
You can’t perform that action at this time.
0 commit comments