Skip to content

Commit b3e6d89

Browse files
committed
Fix React Vanilla time renderer format
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.
1 parent afff4da commit b3e6d89

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/vanilla/src/cells/TimeCell.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,37 @@ import { withJsonFormsCellProps } from '@jsonforms/react';
3333
import { VanillaRendererProps } from '../index';
3434
import { withVanillaCellProps } from '../util/index';
3535

36+
/**
37+
* AJV 'time' format expects HH:mm:ss while <input type='time'> only returns HH:mm.
38+
* Therefore we append ':00' when the seconds are missing.
39+
*/
40+
const appendSecondsIfNecessary = (value: unknown) => {
41+
if (typeof value === 'string') {
42+
const splitValue = value.split(':');
43+
if (splitValue.length === 2) {
44+
splitValue.push('00');
45+
}
46+
return splitValue.join(':');
47+
}
48+
return value;
49+
}
50+
3651
export const TimeCell = (props: CellProps & VanillaRendererProps) => {
3752
const { data, className, id, enabled, uischema, path, handleChange } = props;
3853

3954
return (
4055
<input
4156
type='time'
4257
value={data || ''}
43-
onChange={ev => handleChange(path, ev.target.value)}
58+
onChange={ev => handleChange(path, appendSecondsIfNecessary(ev.target.value))}
4459
className={className}
4560
id={id}
4661
disabled={!enabled}
4762
autoFocus={uischema.options && uischema.options.focus}
4863
/>
4964
);
5065
};
66+
5167
/**
5268
* Default tester for date controls.
5369
* @type {RankedTester}

packages/vanilla/test/renderers/TimeCell.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe('Time cell', () => {
259259
const input = wrapper.find('input');
260260
input.simulate('change', { target: { value: '20:15' } });
261261
wrapper.update();
262-
expect(onChangeData.data.foo).toBe('20:15');
262+
expect(onChangeData.data.foo).toBe('20:15:00');
263263
});
264264

265265
test('update via action', () => {

0 commit comments

Comments
 (0)