Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions src/runtime/components/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ function updateInput(value: string | null | undefined) {
value = looseToNumber(value)
}

if (props.modelModifiers?.nullable) {
value ||= null
// Only empty values are mapped, `0` is a value on its own with the `number` modifier
const isEmpty = value === '' || value === null || value === undefined

if (props.modelModifiers?.nullable && isEmpty) {
value = null
}

if (props.modelModifiers?.optional && !props.modelModifiers?.nullable && value !== null) {
value ||= undefined
if (props.modelModifiers?.optional && !props.modelModifiers?.nullable && value !== null && isEmpty) {
value = undefined
}

modelValue.value = value as ApplyModifiers<T, Mod>
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/components/Textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ function updateInput(value: string | null | undefined) {
value = looseToNumber(value)
}

if (props.modelModifiers?.nullable) {
value ||= null
// Only empty values are mapped, `0` is a value on its own with the `number` modifier
const isEmpty = value === '' || value === null || value === undefined

if (props.modelModifiers?.nullable && isEmpty) {
value = null
}

if (props.modelModifiers?.optional && !props.modelModifiers?.nullable && value !== null) {
value ||= undefined
if (props.modelModifiers?.optional && !props.modelModifiers?.nullable && value !== null && isEmpty) {
value = undefined
}

modelValue.value = value as ApplyModifiers<T, Mod>
Expand Down
7 changes: 6 additions & 1 deletion test/components/Input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ describe('Input', () => {
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }],
['with .nullable modifier', { props: { modelModifiers: { nullable: true } } }, { input: '', expected: null }],
['with .optional modifier', { props: { modelModifiers: { optional: true } } }, { input: '', expected: undefined }]
['with .optional modifier', { props: { modelModifiers: { optional: true } } }, { input: '', expected: undefined }],
['with .number and .nullable modifiers', { props: { modelModifiers: { number: true, nullable: true } } }, { input: '0', expected: 0 }],
['with .number and .optional modifiers', { props: { modelModifiers: { number: true, optional: true } } }, { input: '0', expected: 0 }],
['with .number and .nullable modifiers on an empty value', { props: { modelModifiers: { number: true, nullable: true } } }, { input: '', expected: null }],
['with .number and .optional modifiers on an empty value', { props: { modelModifiers: { number: true, optional: true } } }, { input: '', expected: undefined }],
['with number type and .nullable modifier', { props: { type: 'number', modelModifiers: { nullable: true } } }, { input: '0', expected: 0 }]
],
'%s works',
async (_nameOrHtml, options, spec) => {
Expand Down
6 changes: 5 additions & 1 deletion test/components/Textarea.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ describe('Textarea', () => {
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }],
['with .nullable modifier', { props: { modelModifiers: { nullable: true } } }, { input: '', expected: null }],
['with .optional modifier', { props: { modelModifiers: { optional: true } } }, { input: '', expected: undefined }]
['with .optional modifier', { props: { modelModifiers: { optional: true } } }, { input: '', expected: undefined }],
['with .number and .nullable modifiers', { props: { modelModifiers: { number: true, nullable: true } } }, { input: '0', expected: 0 }],
['with .number and .optional modifiers', { props: { modelModifiers: { number: true, optional: true } } }, { input: '0', expected: 0 }],
['with .number and .nullable modifiers on an empty value', { props: { modelModifiers: { number: true, nullable: true } } }, { input: '', expected: null }],
['with .number and .optional modifiers on an empty value', { props: { modelModifiers: { number: true, optional: true } } }, { input: '', expected: undefined }]
],
'%s works',
async (_, options, spec) => {
Expand Down
Loading