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

fix(Input/Textarea): add v-model modifiers #856

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 38 additions & 3 deletions src/runtime/components/forms/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
v-bind="attrs"
@input="onInput"
@blur="onBlur"
@change="onChange"
>
<slot />

Expand All @@ -38,7 +39,7 @@ import { twMerge, twJoin } from 'tailwind-merge'
import UIcon from '../elements/Icon.vue'
import { useUI } from '../../composables/useUI'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig } from '../../utils'
import { mergeConfig, looseToNumber } from '../../utils'
import type { NestedKeyOf, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
Expand Down Expand Up @@ -156,6 +157,10 @@ export default defineComponent({
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
},
modelModifiers: {
type: Object as PropType<{ trim?: boolean, lazy?: boolean, number?: boolean }>,
default: () => ({})
}
},
emits: ['update:modelValue', 'blur'],
Expand All @@ -172,11 +177,40 @@ export default defineComponent({
}
}

const onInput = (event: InputEvent) => {
emit('update:modelValue', (event.target as HTMLInputElement).value)
// Custom function to handle the v-model properties
const updateInput = (value: string) => {

if (props.modelModifiers.trim) {
value = value.trim()
}

if (props.modelModifiers.number || props.type === 'number') {
value = looseToNumber(value)
}
romhml marked this conversation as resolved.
Show resolved Hide resolved

emit('update:modelValue', value)
emitFormInput()
}

const onInput = (event: InputEvent) => {
if (!props.modelModifiers.lazy) {
updateInput((event.target as HTMLInputElement).value)
}
}

const onChange = (event: InputEvent) => {
const value = (event.target as HTMLInputElement).value

if (props.modelModifiers.lazy) {
updateInput(value)
}

// Update trimmed input so that it has same behaviour as native input https://github.com/vuejs/core/blob/5ea8a8a4fab4e19a71e123e4d27d051f5e927172/packages/runtime-dom/src/directives/vModel.ts#L63
if (props.modelModifiers.trim) {
(event.target as HTMLInputElement).value = value.trim()
}
romhml marked this conversation as resolved.
Show resolved Hide resolved
}

const onBlur = (event: FocusEvent) => {
emitFormBlur()
emit('blur', event)
Expand Down Expand Up @@ -280,6 +314,7 @@ export default defineComponent({
trailingIconClass,
trailingWrapperIconClass,
onInput,
onChange,
onBlur
}
}
Expand Down
39 changes: 36 additions & 3 deletions src/runtime/components/forms/Textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
v-bind="attrs"
@input="onInput"
@blur="onBlur"
@change="onChange"
/>
</div>
</template>
Expand All @@ -24,7 +25,7 @@ import type { PropType } from 'vue'
import { twMerge, twJoin } from 'tailwind-merge'
import { useUI } from '../../composables/useUI'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig } from '../../utils'
import { mergeConfig, looseToNumber } from '../../utils'
import type { NestedKeyOf, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
Expand Down Expand Up @@ -119,6 +120,10 @@ export default defineComponent({
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
},
modelModifiers: {
type: Object as PropType<{ trim?: boolean, lazy?: boolean, number?: boolean }>,
default: () => ({})
}
},
emits: ['update:modelValue', 'blur'],
Expand Down Expand Up @@ -157,11 +162,38 @@ export default defineComponent({
}
}

// Custom function to handle the v-model properties
const updateInput = (value: string) => {
if (props.modelModifiers.trim) {
value = value.trim()
}

if (props.modelModifiers.number) {
value = looseToNumber(value)
}

emit('update:modelValue', value)
emitFormInput()
}

const onInput = (event: InputEvent) => {
autoResize()
if (!props.modelModifiers.lazy) {
updateInput((event.target as HTMLInputElement).value)
}
}

emit('update:modelValue', (event.target as HTMLInputElement).value)
emitFormInput()
const onChange = (event: InputEvent) => {
const value = (event.target as HTMLInputElement).value

if (props.modelModifiers.lazy) {
updateInput(value)
}

// Update trimmed input so that it has same behaviour as native input
if (props.modelModifiers.trim) {
(event.target as HTMLInputElement).value = value.trim()
}
}

const onBlur = (event: FocusEvent) => {
Expand Down Expand Up @@ -211,6 +243,7 @@ export default defineComponent({
// eslint-disable-next-line vue/no-dupe-keys
textareaClass,
onInput,
onChange,
onBlur
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ export function getSlotsChildren (slots: any) {
return children
}

/**
* "123-foo" will be parsed to 123
* This is used for the .number modifier in v-model
*/
export function looseToNumber (val: any): any {
const n = parseFloat(val)
return isNaN(n) ? val : n
}

export * from './lodash'
Loading