mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-02-01 12:47:57 +01:00
feat(Input/Textarea): allow null value in model (#3415)
This commit is contained in:
committed by
GitHub
parent
ed7710a890
commit
cfe9b2ecf3
@@ -82,7 +82,7 @@ const props = withDefaults(defineProps<InputProps>(), {
|
|||||||
const emits = defineEmits<InputEmits>()
|
const emits = defineEmits<InputEmits>()
|
||||||
const slots = defineSlots<InputSlots>()
|
const slots = defineSlots<InputSlots>()
|
||||||
|
|
||||||
const [modelValue, modelModifiers] = defineModel<string | number>()
|
const [modelValue, modelModifiers] = defineModel<string | number | null>()
|
||||||
|
|
||||||
const { emitFormBlur, emitFormInput, emitFormChange, size: formGroupSize, color, id, name, highlight, disabled, emitFormFocus, ariaAttrs } = useFormField<InputProps>(props, { deferInputValidation: true })
|
const { emitFormBlur, emitFormInput, emitFormChange, size: formGroupSize, color, id, name, highlight, disabled, emitFormFocus, ariaAttrs } = useFormField<InputProps>(props, { deferInputValidation: true })
|
||||||
const { orientation, size: buttonGroupSize } = useButtonGroup<InputProps>(props)
|
const { orientation, size: buttonGroupSize } = useButtonGroup<InputProps>(props)
|
||||||
@@ -111,15 +111,19 @@ function autoFocus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Custom function to handle the v-model properties
|
// Custom function to handle the v-model properties
|
||||||
function updateInput(value: string) {
|
function updateInput(value: string | null) {
|
||||||
if (modelModifiers.trim) {
|
if (modelModifiers.trim) {
|
||||||
value = value.trim()
|
value = value?.trim() ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modelModifiers.number || props.type === 'number') {
|
if (modelModifiers.number || props.type === 'number') {
|
||||||
value = looseToNumber(value)
|
value = looseToNumber(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (modelModifiers.nullify) {
|
||||||
|
value ||= null
|
||||||
|
}
|
||||||
|
|
||||||
modelValue.value = value
|
modelValue.value = value
|
||||||
emitFormInput()
|
emitFormInput()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ const props = withDefaults(defineProps<TextareaProps>(), {
|
|||||||
defineSlots<TextareaSlots>()
|
defineSlots<TextareaSlots>()
|
||||||
const emits = defineEmits<TextareaEmits>()
|
const emits = defineEmits<TextareaEmits>()
|
||||||
|
|
||||||
const [modelValue, modelModifiers] = defineModel<string | number>()
|
const [modelValue, modelModifiers] = defineModel<string | number | null>()
|
||||||
|
|
||||||
const { emitFormFocus, emitFormBlur, emitFormInput, emitFormChange, size, color, id, name, highlight, disabled, ariaAttrs } = useFormField<TextareaProps>(props, { deferInputValidation: true })
|
const { emitFormFocus, emitFormBlur, emitFormInput, emitFormChange, size, color, id, name, highlight, disabled, ariaAttrs } = useFormField<TextareaProps>(props, { deferInputValidation: true })
|
||||||
|
|
||||||
@@ -94,15 +94,19 @@ function autoFocus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Custom function to handle the v-model properties
|
// Custom function to handle the v-model properties
|
||||||
function updateInput(value: string) {
|
function updateInput(value: string | null) {
|
||||||
if (modelModifiers.trim) {
|
if (modelModifiers.trim) {
|
||||||
value = value.trim()
|
value = value?.trim() ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modelModifiers.number) {
|
if (modelModifiers.number) {
|
||||||
value = looseToNumber(value)
|
value = looseToNumber(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (modelModifiers.nullify) {
|
||||||
|
value ||= null
|
||||||
|
}
|
||||||
|
|
||||||
modelValue.value = value
|
modelValue.value = value
|
||||||
emitFormInput()
|
emitFormInput()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,8 @@ describe('Input', () => {
|
|||||||
it.each([
|
it.each([
|
||||||
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
|
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
|
||||||
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
|
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
|
||||||
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }]
|
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }],
|
||||||
|
['with .nullify modifier', { props: { modelModifiers: { nullify: true } } }, { input: '', expected: null }]
|
||||||
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
|
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
|
||||||
const wrapper = mount(Input, {
|
const wrapper = mount(Input, {
|
||||||
...options
|
...options
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ describe('Textarea', () => {
|
|||||||
it.each([
|
it.each([
|
||||||
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
|
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
|
||||||
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
|
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
|
||||||
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }]
|
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }],
|
||||||
|
['with .nullify modifier', { props: { modelModifiers: { nullify: true } } }, { input: '', expected: null }]
|
||||||
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
|
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
|
||||||
const wrapper = mount(Textarea, {
|
const wrapper = mount(Textarea, {
|
||||||
...options
|
...options
|
||||||
|
|||||||
Reference in New Issue
Block a user