Skip to content

Commit 427ade4

Browse files
sdirixlucas-koehler
authored andcommitted
fix: append seconds in Vue Vanilla time
Appends seconds if necessary in Vue Vanilla time renderer. This makes sure that we do not produce validation errors. Also allows to configure the "step" attribute via UI Schema options. Adds test cases.
1 parent e9bd6a9 commit 427ade4

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

packages/vue-vanilla/src/controls/TimeControlRenderer.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
:disabled="!control.enabled"
1414
:autofocus="appliedOptions.focus"
1515
:placeholder="appliedOptions.placeholder"
16+
:step="typeof appliedOptions.step === 'number' ? appliedOptions.step : 0"
1617
@change="onChange"
1718
@focus="isFocused = true"
1819
@blur="isFocused = false"
@@ -45,10 +46,14 @@ const controlRenderer = defineComponent({
4546
...rendererProps<ControlElement>(),
4647
},
4748
setup(props: RendererProps<ControlElement>) {
48-
return useVanillaControl(
49-
useJsonFormsControl(props),
50-
(target) => target.value || undefined
51-
);
49+
return useVanillaControl(useJsonFormsControl(props), (target) => {
50+
const value = target.value || undefined;
51+
// Append '00' seconds if the value is in HH:MM format
52+
if (value && /^\d{2}:\d{2}$/.test(value)) {
53+
return `${value}:00`;
54+
}
55+
return value;
56+
});
5257
},
5358
});
5459

packages/vue-vanilla/tests/unit/controls/TimeControlRenderer.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('TimeControlRenderer.vue', () => {
2929
const wrapper = mountJsonForms('00:20', schema, uischema);
3030
const input = wrapper.find('input');
3131
await input.setValue('01:51');
32-
expect(wrapper.vm.data).to.equal('01:51');
32+
expect(wrapper.vm.data).to.equal('01:51:00');
3333
});
3434

3535
it('should have a placeholder', async () => {
@@ -38,4 +38,18 @@ describe('TimeControlRenderer.vue', () => {
3838
const placeholder = input.attributes('placeholder');
3939
expect(placeholder).to.equal('time placeholder');
4040
});
41+
42+
it('appends seconds when time value has HH:MM format', async () => {
43+
const wrapper = mountJsonForms('00:20:00', schema, uischema);
44+
const input = wrapper.find('input');
45+
await input.setValue('01:30');
46+
expect(wrapper.vm.data).to.equal('01:30:00');
47+
});
48+
49+
it('preserves seconds when time value already has HH:MM:SS format', async () => {
50+
const wrapper = mountJsonForms('00:20:00', schema, uischema);
51+
const input = wrapper.find('input');
52+
await input.setValue('01:30:45');
53+
expect(wrapper.vm.data).to.equal('01:30:45');
54+
});
4155
});

0 commit comments

Comments
 (0)