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: vaInput component max Length issue(#4052) #4110

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
352 changes: 181 additions & 171 deletions packages/ui/src/components/va-input/VaInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</template>

<script lang="ts">
import { computed, InputHTMLAttributes, nextTick, shallowRef, toRefs, useAttrs, useSlots, watch } from 'vue'
import { computed, defineComponent, InputHTMLAttributes, nextTick, shallowRef, toRefs, watch } from 'vue'
import omit from 'lodash/omit.js'
import pick from 'lodash/pick.js'

Expand Down Expand Up @@ -84,186 +84,196 @@ const { createEmits: createFieldEmits, createListeners: createFieldListeners } =
'click-prepend-inner',
'click-append-inner',
])
</script>

<script lang="ts" setup>

defineOptions({
export default defineComponent({
name: 'VaInput',
inheritAttrs: false,
})

const props = defineProps({
...VaInputWrapperProps,
...useFormFieldProps,
...useFocusableProps,
...useValidationProps as ValidationProps<string>,
...useClearableProps,
...useCleaveProps,
...useComponentPresetProp,
...useStatefulProps,
components: { VaInputWrapper, VaIcon },

props: {
...VaInputWrapperProps,
...useFormFieldProps,
...useFocusableProps,
...useValidationProps as ValidationProps<string>,
...useClearableProps,
...useCleaveProps,
...useComponentPresetProp,
...useStatefulProps,

// input
placeholder: { type: String, default: '' },
tabindex: { type: [String, Number], default: 0 },
modelValue: { type: [Number, String], default: '' },
type: { type: String as AnyStringPropType<'text' | 'password'>, default: 'text' },
inputClass: { type: String, default: '' },
pattern: { type: String },
inputmode: { type: String, default: 'text' },
counter: { type: Boolean, default: false },
placeholder: { type: String, default: '' },
tabindex: { type: [String, Number], default: 0 },
modelValue: { type: [Number, String], default: '' },
type: { type: String as AnyStringPropType<'text' | 'password'>, default: 'text' },
inputClass: { type: String, default: '' },
pattern: { type: String },
inputmode: { type: String, default: 'text' },
counter: { type: Boolean, default: false },

// style
ariaResetLabel: { type: String, default: '$t:reset' },
ariaResetLabel: { type: String, default: '$t:reset' },

/** Set value to input when model value is updated */
strictBindInputValue: { type: Boolean, default: false },
})

const emit = defineEmits([
'update:modelValue',
...useValidationEmits,
...useClearableEmits,
...createInputEmits(),
...createFieldEmits(),
...useStatefulEmits,
])

useDeprecatedCondition([
() => props.type !== 'textarea' || 'Use VaTextarea component instead of VaInput with type="textarea"',
])

const input = shallowRef<HTMLInputElement>()
strictBindInputValue: { type: Boolean, default: false },
},

emits: [
'update:modelValue',
...useValidationEmits,
...useClearableEmits,
...createInputEmits(),
...createFieldEmits(),
...useStatefulEmits,
],

const { valueComputed } = useStateful(props, emit, 'modelValue')

const reset = () => withoutValidation(() => {
emit('update:modelValue', props.clearValue)
emit('clear')
resetValidation()
})

const { focus, blur } = useFocusable(input, props)

const slots = useSlots()

const filterSlots = computed(() => {
const iconSlot = ['icon']
return Object.keys(slots).filter(slot => !iconSlot.includes(slot))
})

const { tp } = useTranslation()

const {
isDirty,
computedError,
computedErrorMessages,
listeners: { onBlur, onFocus },
validationAriaAttributes,
isLoading,
withoutValidation,
resetValidation,
} = useValidation(props, emit, { reset, focus, value: valueComputed })

const { modelValue } = toRefs(props)
const {
canBeCleared,
clearIconProps,
} = useClearable(props, modelValue, input, computedError)

const { computedValue, onInput } = useCleave(input, props, valueComputed)

const inputListeners = createInputListeners(emit)

const inputEvents = {
...inputListeners,
onFocus: combineFunctions(onFocus, inputListeners.onFocus),
onBlur: combineFunctions(onBlur, inputListeners.onBlur),
onInput: combineFunctions(onInput, inputListeners.onInput),
}

const setInputValue = (newValue: string) => {
if (!props.strictBindInputValue) {
return
}

const target = input.value

if (!target) {
return
}

// Similar to cleave solution
// When user types, we update input value according to computedValue, if value is different
// This causes cursor to move to the end of the input
// To prevent this, we save cursor position and restore it after value is updated
const selectionStart = target.selectionStart || 0
const selectionEnd = target.selectionEnd || 0

if (target.value !== newValue) {
target.value = String(newValue)
}
target.setSelectionRange(selectionStart, selectionEnd)
}

watch(computedValue, (newValue) => {
setInputValue(String(newValue))
})

useEvent('input', () => {
setInputValue(String(valueComputed.value))
}, input)

const tabIndexComputed = computed(() => props.disabled ? -1 : props.tabindex)

const attrs = useAttrs()

const computedChildAttributes = computed(() => (({
'aria-label': props.inputAriaLabel || props.label,
'aria-labelledby': props.inputAriaLabelledby,
'aria-required': props.requiredMark,
tabindex: tabIndexComputed.value,
class: props.inputClass,
'aria-disabled': props.disabled,
'aria-readonly': props.readonly,
...validationAriaAttributes.value,
...omit(attrs, ['class', 'style']),
}) as InputHTMLAttributes))

const computedInputAttributes = computed(() => (({
...computedChildAttributes.value,
...pick(props, ['type', 'disabled', 'readonly', 'placeholder', 'pattern', 'inputmode', 'minlength', 'maxlength']),
}) as InputHTMLAttributes))

const valueLengthComputed = computed(() =>
props.counter && typeof computedValue.value === 'string' ? computedValue.value.length : undefined,
)
inheritAttrs: false,

const onFieldClick = (e: MouseEvent) => {
if (!e.target || !('tagName' in e.target)) {
return
}

if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
return
}

focus()
}

const wrapperProps = filterComponentProps(VaInputWrapperProps)
const fieldListeners = createFieldListeners(emit)

defineExpose({
isDirty,
isLoading,
computedError,
computedErrorMessages,
reset,
focus,
blur,
value: valueComputed,
withoutValidation,
resetValidation,
setup (props, { emit, attrs, slots }) {
useDeprecatedCondition([
() => props.type !== 'textarea' || 'Use VaTextarea component instead of VaInput with type="textarea"',
])

const input = shallowRef<HTMLInputElement>()

const { valueComputed } = useStateful(props, emit, 'modelValue')

const reset = () => withoutValidation(() => {
emit('update:modelValue', props.clearValue)
emit('clear')
resetValidation()
})

const { focus, blur } = useFocusable(input, props)

const filterSlots = computed(() => {
const iconSlot = ['icon']
return Object.keys(slots).filter(slot => !iconSlot.includes(slot))
})

const {
isDirty,
computedError,
computedErrorMessages,
listeners: { onBlur, onFocus },
validationAriaAttributes,
isLoading,
withoutValidation,
resetValidation,
} = useValidation(props, emit, { reset, focus, value: valueComputed })

const { modelValue } = toRefs(props)
const {
canBeCleared,
clearIconProps,
} = useClearable(props, modelValue, input, computedError)

const { computedValue, onInput } = useCleave(input, props, valueComputed)

const inputListeners = createInputListeners(emit)

const inputEvents = {
...inputListeners,
onFocus: combineFunctions(onFocus, inputListeners.onFocus),
onBlur: combineFunctions(onBlur, inputListeners.onBlur),
onInput: combineFunctions(onInput, inputListeners.onInput),
}

const setInputValue = (newValue: string) => {
if (!props.strictBindInputValue) {
return
}

const target = input.value

if (!target) {
return
}

// Similar to cleave solution
// When user types, we update input value according to computedValue, if value is different
// This causes cursor to move to the end of the input
// To prevent this, we save cursor position and restore it after value is updated
const selectionStart = target.selectionStart || 0
const selectionEnd = target.selectionEnd || 0

if (target.value !== newValue) {
target.value = String(newValue)
}
target.setSelectionRange(selectionStart, selectionEnd)
}

watch(computedValue, (newValue) => {
setInputValue(String(newValue))
})

useEvent('input', () => {
setInputValue(String(valueComputed.value))
}, input)

const tabIndexComputed = computed(() => props.disabled ? -1 : props.tabindex)

const computedChildAttributes = computed(() => ({
'aria-label': props.inputAriaLabel || props.label,
'aria-labelledby': props.inputAriaLabelledby,
'aria-required': props.requiredMark,
tabindex: tabIndexComputed.value,
class: props.inputClass,
'aria-disabled': props.disabled,
'aria-readonly': props.readonly,
...validationAriaAttributes.value,
...omit(attrs, ['class', 'style']),
}) as InputHTMLAttributes)

const computedInputAttributes = computed(() => ({
...computedChildAttributes.value,
...pick(props, ['type', 'disabled', 'readonly', 'placeholder', 'pattern', 'inputmode', 'minLength', 'maxLength']),
}) as InputHTMLAttributes)

const valueLengthComputed = computed(() =>
props.counter && typeof computedValue.value === 'string' ? computedValue.value.length : undefined,
)

const onFieldClick = (e: MouseEvent) => {
if (!e.target || !('tagName' in e.target)) {
return
}

if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
return
}

focus()
}

return {
...useTranslation(),
onFieldClick,
input,
inputEvents,
isLoading,

valueLengthComputed,
computedChildAttributes,
computedInputAttributes,
wrapperProps: filterComponentProps(VaInputWrapperProps),
computedValue,
tabIndexComputed,

// Validations
computedError,
computedErrorMessages,

// Icon
canBeCleared,
clearIconProps,

fieldListeners: createFieldListeners(emit),
filterSlots,
isDirty,
reset,
focus,
blur,
}
},
})
</script>
Loading