Skip to content

Commit

Permalink
fix(input-hms): emit padded value
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Dec 26, 2024
1 parent 35856fb commit 379eed3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
16 changes: 4 additions & 12 deletions lib/components/SInputHMS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ const _value = computed(() => {
: props.value !== undefined ? props.value : null
})
const padValue = computed(() => {
return {
hour: _value.value?.hour?.padStart(2, '0') ?? null,
minute: _value.value?.minute?.padStart(2, '0') ?? null,
second: _value.value?.second?.padStart(2, '0') ?? null
}
})
const padPlaceholder = computed(() => {
return {
hour: props.placeholder?.hour?.toString().padStart(2, '0') ?? '00',
Expand Down Expand Up @@ -102,7 +94,7 @@ function update(type: ValueType, value: string | null) {
const newValue = {
..._value.value,
[type]: value ?? null
[type]: value?.padStart(2, '0') ?? null
}
emit('update:model-value', newValue)
Expand Down Expand Up @@ -166,7 +158,7 @@ function createRequiredTouched(): boolean[] {
<input
v-if="!noHour"
class="input hour"
:value="padValue?.hour"
:value="_value?.hour"
:placeholder="padPlaceholder.hour"
:maxlength="2"
:disabled="disabled"
Expand All @@ -177,7 +169,7 @@ function createRequiredTouched(): boolean[] {
<input
v-if="!noMinute"
class="input minute"
:value="padValue?.minute"
:value="_value?.minute"
:placeholder="padPlaceholder.minute"
:maxlength="2"
:disabled="disabled"
Expand All @@ -188,7 +180,7 @@ function createRequiredTouched(): boolean[] {
<input
v-if="!noSecond"
class="input second"
:value="padValue?.second"
:value="_value?.second"
:placeholder="padPlaceholder.second"
:maxlength="2"
:disabled="disabled"
Expand Down
18 changes: 9 additions & 9 deletions tests/components/SInputHMS.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('components/SInputHMS', async () => {
test('accepts `:value`', async () => {
const wrapper = mount(SInputHMS, {
props: {
value: { hour: '1', minute: '2', second: '3' }
value: { hour: '01', minute: '02', second: '03' }
}
})

Expand All @@ -24,7 +24,7 @@ describe('components/SInputHMS', async () => {
test('accepts `:model-value`', async () => {
const wrapper = mount(SInputHMS, {
props: {
modelValue: { hour: '1', minute: '2', second: '3' }
modelValue: { hour: '01', minute: '02', second: '03' }
}
})

Expand Down Expand Up @@ -73,24 +73,24 @@ describe('components/SInputHMS', async () => {
test('emits `@update:model-value` and `@change` on blur', async () => {
const wrapper = mount(SInputHMS, {
props: {
modelValue: { hour: '1', minute: '2', second: '3' }
modelValue: { hour: '01', minute: '02', second: '03' }
}
})

await wrapper.find('.SInputHMS .input.hour').setValue('4')
await wrapper.find('.SInputHMS .input.hour').trigger('blur')
assertEmitted(wrapper, 'update:model-value', 1, { hour: '4', minute: '2', second: '3' })
assertEmitted(wrapper, 'change', 1, { hour: '4', minute: '2', second: '3' })
assertEmitted(wrapper, 'update:model-value', 1, { hour: '04', minute: '02', second: '03' })
assertEmitted(wrapper, 'change', 1, { hour: '04', minute: '02', second: '03' })

await wrapper.find('.SInputHMS .input.minute').setValue('5')
await wrapper.find('.SInputHMS .input.minute').trigger('blur')
assertEmitted(wrapper, 'update:model-value', 2, { hour: '1', minute: '5', second: '3' })
assertEmitted(wrapper, 'change', 2, { hour: '1', minute: '5', second: '3' })
assertEmitted(wrapper, 'update:model-value', 2, { hour: '01', minute: '05', second: '03' })
assertEmitted(wrapper, 'change', 2, { hour: '01', minute: '05', second: '03' })

await wrapper.find('.SInputHMS .input.second').setValue('6')
await wrapper.find('.SInputHMS .input.second').trigger('blur')
assertEmitted(wrapper, 'update:model-value', 3, { hour: '1', minute: '2', second: '6' })
assertEmitted(wrapper, 'change', 3, { hour: '1', minute: '2', second: '6' })
assertEmitted(wrapper, 'update:model-value', 3, { hour: '01', minute: '02', second: '06' })
assertEmitted(wrapper, 'change', 3, { hour: '01', minute: '02', second: '06' })
})

test('emits events with `null` when the input is not number', async () => {
Expand Down

0 comments on commit 379eed3

Please sign in to comment.