Repository navigation
jsonforms-renderers: DateControl time picker stores HH:MM:SS #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sthanikan2000
merged 10 commits into
OpenNSW:main
from
WebCooper:fix/time-picker-seconds
Oct 10, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ba65070
fix(jsonforms-renderers): store time picker values as HH:MM:SS
WebCooper 106d73b
fix(jsonforms-renderers): move time helpers out of DateControl
WebCooper 8570125
test(jsonforms-renderers): colocate time helper tests in utils
WebCooper 67bea1e
test(jsonforms-renderers): cover clearing time in DateControl
WebCooper 9930566
docs(jsonforms-renderers): note date-time layout and showSeconds
WebCooper 1643e41
test(jsonforms-renderers): cover showSeconds on date-time controls
WebCooper c7f00a4
docs(jsonforms-renderers): document time formatting helpers
WebCooper 08bbe48
merge origin/main: keep jsonforms-renderers README from #103
WebCooper c2cec5e
docs(jsonforms-renderers): add DateControl guide under docs/
WebCooper 200473e
style(jsonforms-renderers): prettier-format date-control.md
WebCooper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Date and time (`DateControl`) | ||
|
|
||
| `DateControl` renders JSON Schema `string` fields with `format` of `date`, `time`, or `date-time`, using native browser pickers. | ||
|
|
||
| See the `date` fixture in `dev/fixtures.ts` for a runnable example (`eventDate`, `appointment`, `openingTime`, `closingTime`). | ||
|
|
||
| ## Formats | ||
|
|
||
| | Schema `format` | Stored value | UI | | ||
| | --------------- | ------------------------------------------- | -------------------------------------------------------- | | ||
| | `date` | `YYYY-MM-DD` | Native date input | | ||
| | `time` | RFC 3339 `HH:MM:SS` | Native time input | | ||
| | `date-time` | RFC 3339 date-time (seconds + local offset) | Date and time inputs side by side, sharing width equally | | ||
|
|
||
| `time` and `date-time` share the same native time input. With `date` alone the date input expands to the full control width. | ||
|
|
||
| ## Stored time is always `HH:MM:SS` | ||
|
|
||
| AJV's `format: "time"` is RFC 3339, which requires seconds. Native `<input type="time">` with minute precision (the default) emits `HH:MM`. `DateControl` pads that to `HH:MM:SS` before writing form data (`14:30` → `14:30:00`) via [`utils/time.ts`](../src/utils/time.ts). Clearing the picker still clears the field. | ||
|
|
||
| Artifact schemas that declare `format: "time"` do not need to change. | ||
|
|
||
| ## Seconds spinner (`options.showSeconds`) | ||
|
|
||
| The seconds spinner is **opt-in**. Set `options.showSeconds: true` on the **Control** in the uischema — it applies to both `time` and `date-time`: | ||
|
|
||
| ```json | ||
| { | ||
| "type": "Control", | ||
| "scope": "#/properties/proposed_inspection_time", | ||
| "options": { "showSeconds": true } | ||
| } | ||
| ``` | ||
|
|
||
| | `showSeconds` | Picker | Stored value | | ||
| | ----------------- | ------------------------------------ | ------------------------------------------------------------------- | | ||
| | omitted / `false` | Hour and minute only (`step=60`) | Seconds default to `00` | | ||
| | `true` | Hour, minute, and seconds (`step=1`) | Seconds as picked; still `00` if the user only sets hour and minute | | ||
|
|
||
| A form that omits `showSeconds` keeps the hour-and-minute picker and still submits a valid `HH:MM:SS` string. |
143 changes: 143 additions & 0 deletions
143
packages/jsonforms-renderers/src/renderers/DateControl.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // @vitest-environment jsdom | ||
| import { afterEach, describe, expect, it } from 'vitest' | ||
| import { useState } from 'react' | ||
| import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' | ||
| import { JsonForms } from '@jsonforms/react' | ||
| import { Theme } from '@radix-ui/themes' | ||
| import type { JsonSchema, UISchemaElement } from '@jsonforms/core' | ||
| import { radixRenderers } from './index' | ||
|
|
||
| type Data = Record<string, unknown> | ||
|
|
||
| const finishers: (() => void)[] = [] | ||
|
|
||
| afterEach(() => { | ||
| for (const stop of finishers) stop() | ||
| finishers.length = 0 | ||
| cleanup() | ||
| }) | ||
|
|
||
| const openingTimeSchema: JsonSchema = { | ||
| type: 'object', | ||
| properties: { | ||
| openingTime: { type: 'string', format: 'time', title: 'Opening Time' }, | ||
| }, | ||
| } | ||
|
|
||
| const appointmentSchema: JsonSchema = { | ||
| type: 'object', | ||
| properties: { | ||
| appointment: { type: 'string', format: 'date-time', title: 'Appointment' }, | ||
| }, | ||
| } | ||
|
|
||
| function renderForm(schema: JsonSchema, uischema: UISchemaElement, seed: Data) { | ||
| const writes: Data[] = [] | ||
| let live = true | ||
| finishers.push(() => { | ||
| live = false | ||
| }) | ||
|
|
||
| function Harness() { | ||
| const [initial] = useState(seed) | ||
| return ( | ||
| <Theme> | ||
| <JsonForms | ||
| schema={schema} | ||
| uischema={uischema} | ||
| data={initial} | ||
| renderers={radixRenderers} | ||
| onChange={({ data }) => { | ||
| if (live) writes.push(data as Data) | ||
| }} | ||
| /> | ||
| </Theme> | ||
| ) | ||
| } | ||
|
|
||
| render(<Harness />) | ||
| return { writes } | ||
| } | ||
|
|
||
| const timeControl = { | ||
| type: 'VerticalLayout', | ||
| elements: [{ type: 'Control', scope: '#/properties/openingTime' }], | ||
| } as UISchemaElement | ||
|
|
||
| const secondsControl = { | ||
| type: 'VerticalLayout', | ||
| elements: [{ type: 'Control', scope: '#/properties/openingTime', options: { showSeconds: true } }], | ||
| } as UISchemaElement | ||
|
|
||
| const appointmentControl = { | ||
| type: 'VerticalLayout', | ||
| elements: [{ type: 'Control', scope: '#/properties/appointment' }], | ||
| } as UISchemaElement | ||
|
|
||
| const appointmentSecondsControl = { | ||
| type: 'VerticalLayout', | ||
| elements: [{ type: 'Control', scope: '#/properties/appointment', options: { showSeconds: true } }], | ||
| } as UISchemaElement | ||
|
|
||
| describe('DateControl time', () => { | ||
| it('stores HH:MM:SS when the picker emits HH:MM', async () => { | ||
| const { writes } = renderForm(openingTimeSchema, timeControl, {}) | ||
| const input = screen.getByLabelText(/Opening Time/) | ||
|
|
||
| fireEvent.change(input, { target: { value: '14:30' } }) | ||
|
|
||
| await waitFor(() => { | ||
| expect(writes.at(-1)).toEqual({ openingTime: '14:30:00' }) | ||
| }) | ||
| }) | ||
|
|
||
| it('clears the time when the picker value is empty', async () => { | ||
| const { writes } = renderForm(openingTimeSchema, timeControl, { openingTime: '14:30:00' }) | ||
| const input = screen.getByLabelText(/Opening Time/) | ||
|
|
||
| fireEvent.change(input, { target: { value: '' } }) | ||
|
|
||
| await waitFor(() => { | ||
| expect(writes.at(-1)?.openingTime).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| it('uses minute step unless showSeconds is set', () => { | ||
| renderForm(openingTimeSchema, timeControl, { openingTime: '14:30:00' }) | ||
| expect(screen.getByLabelText(/Opening Time/).getAttribute('step')).toBe('60') | ||
| expect((screen.getByLabelText(/Opening Time/) as HTMLInputElement).value).toBe('14:30') | ||
| }) | ||
|
|
||
| it('shows a seconds spinner when showSeconds is true', async () => { | ||
| const { writes } = renderForm(openingTimeSchema, secondsControl, {}) | ||
| const input = screen.getByLabelText(/Opening Time/) | ||
|
|
||
| expect(input.getAttribute('step')).toBe('1') | ||
|
|
||
| fireEvent.change(input, { target: { value: '14:30' } }) | ||
| await waitFor(() => { | ||
| expect(writes.at(-1)).toEqual({ openingTime: '14:30:00' }) | ||
| }) | ||
|
|
||
| fireEvent.change(input, { target: { value: '14:30:45' } }) | ||
| await waitFor(() => { | ||
| expect(writes.at(-1)).toEqual({ openingTime: '14:30:45' }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('DateControl date-time', () => { | ||
| it('uses minute step on the time input unless showSeconds is set', () => { | ||
| renderForm(appointmentSchema, appointmentControl, { appointment: '2026-10-09T14:30:00' }) | ||
| const timeInput = document.querySelector('input[type="time"]') | ||
| expect(timeInput?.getAttribute('step')).toBe('60') | ||
| expect((timeInput as HTMLInputElement).value).toBe('14:30') | ||
| }) | ||
|
|
||
| it('shows a seconds spinner on date-time when showSeconds is true', () => { | ||
| renderForm(appointmentSchema, appointmentSecondsControl, { appointment: '2026-10-09T14:30:45' }) | ||
| const timeInput = document.querySelector('input[type="time"]') | ||
| expect(timeInput?.getAttribute('step')).toBe('1') | ||
| expect((timeInput as HTMLInputElement).value).toBe('14:30:45') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { timeInputValue, toRfc3339Time } from './time' | ||
|
|
||
| describe('toRfc3339Time', () => { | ||
| it('pads HH:MM with zero seconds', () => { | ||
| expect(toRfc3339Time('14:30')).toBe('14:30:00') | ||
| }) | ||
|
|
||
| it('leaves HH:MM:SS unchanged', () => { | ||
| expect(toRfc3339Time('14:30:45')).toBe('14:30:45') | ||
| }) | ||
|
|
||
| it('clears an empty picker value', () => { | ||
| expect(toRfc3339Time('')).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('timeInputValue', () => { | ||
| it('drops seconds when the seconds spinner is off', () => { | ||
| expect(timeInputValue('14:30:00', false)).toBe('14:30') | ||
| }) | ||
|
|
||
| it('keeps seconds when the seconds spinner is on', () => { | ||
| expect(timeInputValue('14:30:45', true)).toBe('14:30:45') | ||
| }) | ||
|
|
||
| it('fills missing seconds for the seconds spinner', () => { | ||
| expect(timeInputValue('14:30', true)).toBe('14:30:00') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** Normalize a native time input value for AJV `format: "time"` (RFC 3339 `HH:MM:SS`). */ | ||
| export const toRfc3339Time = (raw: string): string | undefined => { | ||
| if (!raw) return undefined | ||
| if (/^\d{2}:\d{2}$/.test(raw)) return `${raw}:00` | ||
| return raw | ||
| } | ||
|
|
||
| /** Format a stored time for a native `<input type="time">`, optionally hiding seconds. */ | ||
| export const timeInputValue = (stored: string, showSeconds: boolean): string => { | ||
| if (!stored) return '' | ||
| const match = stored.match(/^(\d{2}:\d{2})(:\d{2})?/) | ||
| if (!match) return stored | ||
| return showSeconds ? `${match[1]}${match[2] ?? ':00'}` : match[1] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.