Skip to content
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

Let VaDateInput accept a null value #4169

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/ui/src/components/va-date-input/VaDateInput.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ export const AsyncDate = () => ({
<VaDateInput v-model="stringValueUTC" />
`,
})

export const NullValue = () => ({
components: { VaDateInput },
data () {
return {
value: null,
}
},
template: `
[[value]]: {{ value === null ? 'null' : value }}
<VaDateInput v-model="value" clearable manual-input clear-value="null" />
`,
})
8 changes: 4 additions & 4 deletions packages/ui/src/components/va-date-input/VaDateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const props = defineProps({
...useValidationProps as ValidationProps<DateInputModelValue>,
...useComponentPresetProp,

clearValue: { type: Date as PropType<DateInputModelValue>, default: undefined },
clearValue: { type: undefined as unknown as PropType<any>, default: undefined },
modelValue: { type: [Date, Array, Object, String, Number] as PropType<DateInputModelValue> },

resetOnClose: { type: Boolean, default: true },
Expand Down Expand Up @@ -239,7 +239,7 @@ const modelValueToString = (value: DateInputModelValue): string => {
return ''
}

throw new Error('VaDatePicker: Invalid model value. Value should be Date, Date[] or { start: Date, end: Date | null }, got ' + typeof value)
Copy link
Contributor

@m0ksem m0ksem May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an error, because by default clearValue is empty string. ''

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay gotcha. Thanks for the explanation and the fix!

return ''
}

const {
Expand All @@ -263,10 +263,10 @@ const valueText = computed(() => {
const onInputTextChanged = ({ target }: Event) => {
if (props.disabled) { return }

const parsedValue = parseDateInputValue((target as HTMLInputElement).value)
const inputValue = (target as HTMLInputElement).value

if (isValid.value) {
valueComputed.value = parsedValue
valueComputed.value = inputValue === '' ? props.clearValue : parseDateInputValue(inputValue)
}
}

Expand Down