Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 14 additions & 5 deletions packages/core/src/reducers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import cloneDeep from 'lodash/cloneDeep';
import setFp from 'lodash/fp/set';
import unsetFp from 'lodash/fp/unset';
import get from 'lodash/get';
import filter from 'lodash/filter';
import isEqual from 'lodash/isEqual';
Expand Down Expand Up @@ -285,11 +286,19 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
} else {
const oldData: any = get(state.data, action.path);
const newData = action.updater(cloneDeep(oldData));
const newState: any = setFp(
action.path,
newData,
state.data === undefined ? {} : state.data
);
let newState: any;
if (newData !== undefined) {
newState = setFp(
action.path,
newData,
state.data === undefined ? {} : state.data
);
} else {
newState = unsetFp(
action.path,
state.data === undefined ? {} : state.data
);
}
const errors = validate(state.validator, newState);
return {
...state,
Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/reducers/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,44 @@ test('core reducer - update - should update errors', (t) => {
});
});

test('core reducer - update - setting a state slice as undefined should remove the slice', (t) => {
const schema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
fizz: {
type: 'string',
},
},
};

const before: JsonFormsCore = {
data: {
foo: 'bar',
fizz: 42,
},
schema: schema,
uischema: {
type: 'Label',
},
errors: [],
validator: new Ajv().compile(schema),
};

const after = coreReducer(
before,
update('foo', (_) => {
return undefined;
})
);

t.not(before, after);
t.not(before.data, after.data);
t.like(Object.keys(after.data), ['fizz']);
});

test('core reducer - updateErrors - should update errors with empty list', (t) => {
const before: JsonFormsCore = {
data: {},
Expand Down