แก้ไข date picker error บน ios (เดือน/ปี)

This commit is contained in:
Warunee Tamkoo 2025-07-16 10:51:04 +07:00
parent b5f4209b35
commit dfa4a8284f
3 changed files with 46 additions and 21 deletions

View file

@ -32,7 +32,8 @@ const filterYear = ref<number>(
const titleName = ref<string>('เพิ่มรายการลงเวลากรณีพิเศษ') // popup const titleName = ref<string>('เพิ่มรายการลงเวลากรณีพิเศษ') // popup
const dateMonth = ref<DataDateMonthObject>({ const dateMonth = ref<DataDateMonthObject>({
month: new Date().getMonth(), month: new Date().getMonth(),
year: stores.year ? stores.year : new Date().getFullYear(), year:
stores.year && !isNaN(stores.year) ? stores.year : new Date().getFullYear(),
}) })
const modalPopup = ref<boolean>(false) // modal const modalPopup = ref<boolean>(false) // modal
@ -42,8 +43,16 @@ const modalPopup = ref<boolean>(false) // modal เพิ่มรายกา
*/ */
function filterYearFn(type: string) { function filterYearFn(type: string) {
const year = type === 'year' ? filterYear.value : dateMonth.value.year const year = type === 'year' ? filterYear.value : dateMonth.value.year
const month = dateMonth.value.month
//
if (isNaN(Number(year)) || isNaN(Number(month))) {
console.warn('Invalid year or month value:', { year, month })
return
}
// //
emit('update:year', year, dateMonth.value.month) emit('update:year', Number(year), Number(month))
} }
/** /**
@ -66,8 +75,19 @@ function onClickClose() {
* @returns เดอนและปในภาษาไทย * @returns เดอนและปในภาษาไทย
*/ */
const monthYearThai = (val: DataDateMonthObject) => { const monthYearThai = (val: DataDateMonthObject) => {
if (val == null) return '' if (!val || !val.year || val.month === undefined || val.month === null) {
else return monthYear2Thai(val.month, val.year) return ''
}
// number
const year = Number(val.year)
const month = Number(val.month)
if (isNaN(year) || isNaN(month) || year < 1900 || month < 0 || month > 11) {
return ''
}
return monthYear2Thai(month, year)
} }
/** /**
@ -130,7 +150,7 @@ watch(
month-picker month-picker
:transitions="false" :transitions="false"
:enableTimePicker="false" :enableTimePicker="false"
@update:modelValue="filterYearFn('mount')" @update:modelValue="filterYearFn('month')"
> >
<template #year="{ year }">{{ <template #year="{ year }">{{
Number.isFinite(year) ? year + 543 : '' Number.isFinite(year) ? year + 543 : ''
@ -143,8 +163,8 @@ watch(
dense dense
lazy-rules lazy-rules
outlined outlined
:model-value="monthYearThai(dateMonth)" :model-value="monthYearThai(dateMonth) || 'เลือกเดือน/ปี'"
:label="`${'ปีงบประมาณ'}`" :label="`${'เดือน/ปีงบประมาณ'}`"
> >
<template v-slot:prepend> <template v-slot:prepend>
<q-icon <q-icon

View file

@ -315,7 +315,13 @@ export const useCounterMixin = defineStore('mixin', () => {
} }
function monthYear2Thai(month: number, year: number, isFullMonth = false) { function monthYear2Thai(month: number, year: number, isFullMonth = false) {
const date = new Date(`${year}-${month + 1}-1`) if (
month < 0 ||
month > 11 ||
!Number.isFinite(month) ||
!Number.isFinite(year)
)
return ''
const fullMonthThai = [ const fullMonthThai = [
'มกราคม', 'มกราคม',
'กุมภาพันธ์', 'กุมภาพันธ์',
@ -344,19 +350,12 @@ export const useCounterMixin = defineStore('mixin', () => {
'พ.ย.', 'พ.ย.',
'ธ.ค.', 'ธ.ค.',
] ]
let dstYear = 0
if (date.getFullYear() > 2500) { // assume year is in BE if > 2500
dstYear = date.getFullYear() let dstYear = year > 2500 ? year : year + 543
} else { // month is already 0-based
dstYear = date.getFullYear() + 543 let dstMonth = isFullMonth ? fullMonthThai[month] : abbrMonthThai[month]
} return `${dstMonth} ${dstYear}`
let dstMonth = ''
if (isFullMonth) {
dstMonth = fullMonthThai[date.getMonth()]
} else {
dstMonth = abbrMonthThai[date.getMonth()]
}
return dstMonth + ' ' + dstYear
} }
// กรณีมีเฉพาะ date // กรณีมีเฉพาะ date

View file

@ -219,3 +219,9 @@ onMounted(() => {
</div> </div>
</div> </div>
</template> </template>
<style scoped>
.q-tab-panel {
min-height: 400px;
}
</style>