File tree Expand file tree Collapse file tree 2 files changed +52
-5
lines changed Expand file tree Collapse file tree 2 files changed +52
-5
lines changed Original file line number Diff line number Diff line change 25
25
26
26
import cloneDeep from 'lodash/cloneDeep' ;
27
27
import setFp from 'lodash/fp/set' ;
28
+ import unsetFp from 'lodash/fp/unset' ;
28
29
import get from 'lodash/get' ;
29
30
import filter from 'lodash/filter' ;
30
31
import isEqual from 'lodash/isEqual' ;
@@ -285,11 +286,19 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
285
286
} else {
286
287
const oldData : any = get ( state . data , action . path ) ;
287
288
const newData = action . updater ( cloneDeep ( oldData ) ) ;
288
- const newState : any = setFp (
289
- action . path ,
290
- newData ,
291
- state . data === undefined ? { } : state . data
292
- ) ;
289
+ let newState : any ;
290
+ if ( newData !== undefined ) {
291
+ newState = setFp (
292
+ action . path ,
293
+ newData ,
294
+ state . data === undefined ? { } : state . data
295
+ ) ;
296
+ } else {
297
+ newState = unsetFp (
298
+ action . path ,
299
+ state . data === undefined ? { } : state . data
300
+ ) ;
301
+ }
293
302
const errors = validate ( state . validator , newState ) ;
294
303
return {
295
304
...state ,
Original file line number Diff line number Diff line change @@ -562,6 +562,44 @@ test('core reducer - update - should update errors', (t) => {
562
562
} ) ;
563
563
} ) ;
564
564
565
+ test ( 'core reducer - update - setting a state slice as undefined should remove the slice' , ( t ) => {
566
+ const schema = {
567
+ type : 'object' ,
568
+ properties : {
569
+ foo : {
570
+ type : 'string' ,
571
+ } ,
572
+ fizz : {
573
+ type : 'string' ,
574
+ } ,
575
+ } ,
576
+ } ;
577
+
578
+ const before : JsonFormsCore = {
579
+ data : {
580
+ foo : 'bar' ,
581
+ fizz : 42 ,
582
+ } ,
583
+ schema : schema ,
584
+ uischema : {
585
+ type : 'Label' ,
586
+ } ,
587
+ errors : [ ] ,
588
+ validator : new Ajv ( ) . compile ( schema ) ,
589
+ } ;
590
+
591
+ const after = coreReducer (
592
+ before ,
593
+ update ( 'foo' , ( _ ) => {
594
+ return undefined ;
595
+ } )
596
+ ) ;
597
+
598
+ t . not ( before , after ) ;
599
+ t . not ( before . data , after . data ) ;
600
+ t . deepEqual ( Object . keys ( after . data ) , [ 'fizz' ] ) ;
601
+ } ) ;
602
+
565
603
test ( 'core reducer - updateErrors - should update errors with empty list' , ( t ) => {
566
604
const before : JsonFormsCore = {
567
605
data : { } ,
You can’t perform that action at this time.
0 commit comments