fix: input date only (birth date for now)

This commit is contained in:
puriphatt 2024-07-16 08:12:01 +00:00
parent 2d8c4d5d6a
commit a92e9ffdc1
2 changed files with 36 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import useOptionStore from 'src/stores/options';
import {
dateFormat,
calculateAge,
parseAndFormatDate,
disabledAfterToday,
} from 'src/utils/datetime';
@ -119,6 +120,7 @@ defineProps<{
v-model="gender"
:options="optionStore.globalOption.gender"
/>
<VueDatePicker
id="input-birth-date"
for="input-birth-date"
@ -143,16 +145,33 @@ defineProps<{
<q-input
id="input-birth-date"
hide-bottom-space
:mask="
birthDate?.toString() === 'Invalid Date' ||
birthDate?.toString() === undefined
? '##/##/####'
: ''
"
placeholder="DD/MM/YYYY"
:label="$t('formDialogInputBirthDate')"
:dense="dense"
outlined
:readonly="readonly"
:model-value="birthDate ? dateFormat(birthDate) : ''"
:rules="[
(val: string) =>
!!val || $t('selectValidate') + $t('formDialogInputBirthDate'),
]"
:model-value="birthDate ? dateFormat(birthDate) : ''"
@update:model-value="
(v) => {
if (v && v.toString().length === 10) {
const date = parseAndFormatDate(v);
birthDate = date;
}
if (v && v.toString().length === 0) birthDate = '';
}
"
>
<!-- v-model="birthDate" -->
<template v-slot:prepend>
<q-icon
size="xs"
@ -171,7 +190,12 @@ defineProps<{
readonly
:label="$t('formDialogInputAge')"
class="col-3"
:model-value="birthDate ? calculateAge(birthDate) : ''"
:model-value="
birthDate?.toString() === 'Invalid Date' ||
birthDate?.toString() === undefined
? ''
: calculateAge(birthDate)
"
/>
<q-select
v-if="employee"

View file

@ -103,3 +103,13 @@ export function disabledAfterToday(date: Date) {
today.setHours(0, 0, 0, 0);
return date > today;
}
export function parseAndFormatDate(value: string | number | undefined) {
if (!value) return;
if (value && value.toString().length === 10) {
const [year, month, day] = value.toString().split('/');
return new Date(`${day}/${month}/${year}`);
}
return;
}