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

chore: input[type=date], the modifier number could converted to a timestamp #12396

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,61 @@ describe('vModel', () => {
expect(data.value).toEqual('使用拼音输入')
})

it('should trasform date to timestamp with numebr modifier', async () => {
const component = defineComponent({
data() {
return { date: null, datetime: null }
},
render() {
return [
withVModel(
h('input', {
class: 'date',
type: 'date',
'onUpdate:modelValue': (val: any) => {
this.date = val
},
}),
this.date,
{
number: true,
},
),
withVModel(
h('input', {
class: 'datetime',
type: 'datetime-local',
'onUpdate:modelValue': (val: any) => {
console.log('zxxx', val)
shengxj1 marked this conversation as resolved.
Show resolved Hide resolved
this.datetime = val
},
}),
this.datetime,
{
number: true,
},
),
]
},
})
render(h(component), root)

const date = root.querySelector('.date')
const datetime = root.querySelector('.datetime')
const data = root._vnode.component.data

date.value = '2024-11-11'
triggerEvent('input', date)
await nextTick()
expect(data.date).toEqual(new Date('2024-11-11').getTime())

datetime.value = '2024-11-11T20:00'
triggerEvent('input', datetime)
await nextTick()
expect(data.datetime).toEqual(new Date('2024-11-11T20:00').getTime())
})

// #10503
it('multiple select (model is number, option value is string)', async () => {
const component = defineComponent({
data() {
Expand Down
16 changes: 13 additions & 3 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@vue/runtime-core'
import { addEventListener } from '../modules/events'
import {
formatDateStamp,
invokeArrayFns,
isArray,
isSet,
Expand Down Expand Up @@ -53,15 +54,19 @@ export const vModelText: ModelDirective<
> = {
created(el, { modifiers: { lazy, trim, number } }, vnode) {
el[assignKey] = getModelAssigner(vnode)
const castToNumber =
number || (vnode.props && vnode.props.type === 'number')
const vnodeType = vnode.props && vnode.props.type
const castToNumber = number || vnodeType === 'number'
const castToTimeStamp =
number && (vnodeType === 'date' || vnodeType === 'datetime-local')
addEventListener(el, lazy ? 'change' : 'input', e => {
if ((e.target as any).composing) return
let domValue: string | number = el.value
if (trim) {
domValue = domValue.trim()
}
if (castToNumber) {
if (castToTimeStamp) {
domValue = formatDateStamp(domValue)
} else if (castToNumber) {
domValue = looseToNumber(domValue)
}
el[assignKey](domValue)
Expand Down Expand Up @@ -111,6 +116,11 @@ export const vModelText: ModelDirective<
if (trim && el.value.trim() === newValue) {
return
}
if (number && (el.type === 'date' || el.type === 'datetime-local')) {
if (formatDateStamp(el.value) === value) {
return
}
}
}

el.value = newValue
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,10 @@ export function genCacheKey(source: string, options: any): string {
)
)
}

export function formatDateStamp(dateStr: string): number {
// Handle iOS compatibility issue by replacing - with /
// 2024-11-15T12:37' => 2024/11/15 12:37'
const normalizedDateStr = dateStr.replace(/-/g, '/').replace('T', ' ')
return new Date(normalizedDateStr).getTime()
}