Skip to content

Commit 146415b

Browse files
committed
React Vanilla enum renderer removes empty data
Instead of the empty string the React Vanilla enum renderer will now hand over 'undefined' to handleChange and therefore delete the corresponding attribute.
1 parent b3e6d89 commit 146415b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/vanilla/src/cells/EnumCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const EnumCell = (props: EnumCellProps & VanillaRendererProps) => {
4343
disabled={!enabled}
4444
autoFocus={uischema.options && uischema.options.focus}
4545
value={data || ''}
46-
onChange={ev => handleChange(path, ev.target.value)}
46+
onChange={ev => handleChange(path, ev.target.selectedIndex === 0 ? undefined : ev.target.value)}
4747
>
4848
{
4949
[<option value='' key={'empty'} />]

packages/vanilla/test/renderers/EnumCell.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ describe('Enum cell', () => {
188188
expect(onChangeData.data.foo).toBe('b');
189189
});
190190

191+
test('empty selection should lead to data deletion', () => {
192+
const onChangeData: any = {
193+
data: undefined
194+
};
195+
const core = initCore(fixture.schema, fixture.uischema, fixture.data);
196+
wrapper = mount(
197+
<JsonFormsStateProvider initState={{ core }}>
198+
<TestEmitter
199+
onChange={({ data }) => {
200+
onChangeData.data = data;
201+
}}
202+
/>
203+
<EnumCell schema={fixture.schema} uischema={fixture.uischema} path='foo' />
204+
</JsonFormsStateProvider>
205+
);
206+
expect(onChangeData.data.foo).toBe('a');
207+
const select = wrapper.find('select');
208+
select.simulate('change', { target: { selectedIndex: 0 } });
209+
expect(onChangeData.data.foo).toBe(undefined);
210+
});
211+
191212
test('update via action', () => {
192213
const data = { 'foo': 'b' };
193214
const core = initCore(fixture.schema, fixture.uischema, data);

0 commit comments

Comments
 (0)