Skip to content

Commit

Permalink
Fix React Vanilla time renderer format
Browse files Browse the repository at this point in the history
JSON Schema's time format is specified as 'HH:mm:ss'. The React Vanilla time renderer
is adjusted to store in this format by appending ':00' when necessary.
  • Loading branch information
sdirix committed Jun 26, 2022
1 parent afff4da commit b3e6d89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/vanilla/src/cells/TimeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,37 @@ import { withJsonFormsCellProps } from '@jsonforms/react';
import { VanillaRendererProps } from '../index';
import { withVanillaCellProps } from '../util/index';

/**
* AJV 'time' format expects HH:mm:ss while <input type='time'> only returns HH:mm.
* Therefore we append ':00' when the seconds are missing.
*/
const appendSecondsIfNecessary = (value: unknown) => {
if (typeof value === 'string') {
const splitValue = value.split(':');
if (splitValue.length === 2) {
splitValue.push('00');
}
return splitValue.join(':');
}
return value;
}

export const TimeCell = (props: CellProps & VanillaRendererProps) => {
const { data, className, id, enabled, uischema, path, handleChange } = props;

return (
<input
type='time'
value={data || ''}
onChange={ev => handleChange(path, ev.target.value)}
onChange={ev => handleChange(path, appendSecondsIfNecessary(ev.target.value))}
className={className}
id={id}
disabled={!enabled}
autoFocus={uischema.options && uischema.options.focus}
/>
);
};

/**
* Default tester for date controls.
* @type {RankedTester}
Expand Down
2 changes: 1 addition & 1 deletion packages/vanilla/test/renderers/TimeCell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('Time cell', () => {
const input = wrapper.find('input');
input.simulate('change', { target: { value: '20:15' } });
wrapper.update();
expect(onChangeData.data.foo).toBe('20:15');
expect(onChangeData.data.foo).toBe('20:15:00');
});

test('update via action', () => {
Expand Down

0 comments on commit b3e6d89

Please sign in to comment.