import type { PropType } from 'vue' import { computed, defineComponent, ref } from 'vue' import { getPrefixCls } from '../_utils/getPrefixCls' import Icon from '../icon' export default defineComponent({ name: 'Input', props: { modelValue: String, size: { type: String as PropType<'small' | 'medium' | 'large'>, default: 'medium', }, align: { type: String as PropType<'left' | 'center' | 'right'>, default: 'left', }, placeholder: String, disabled: Boolean, readonly: Boolean, clearable: Boolean, prefixIcon: String, suffixIcon: String, showCount: Boolean, maxlength: Number, password: Boolean, prepend: String, append: String, }, emits: ['update:modelValue'], setup(props, { slots, emit }) { const prefixCls = getPrefixCls('input') const inputValue = ref(props.modelValue || '') // 控制密码显示/隐藏 const showPassword = ref(false) const updateValue = (val: string) => { if (props.maxlength) val = val.slice(0, props.maxlength) inputValue.value = val emit('update:modelValue', val) } const countText = computed(() => { const len = inputValue.value.length return props.maxlength ? `${len} / ${props.maxlength}` : `${len}` }) const inputType = computed(() => { if (!props.password) return 'text' return showPassword.value ? 'text' : 'password' }) const inputWrapper = () => { return (
{(slots.prefix || props.prefixIcon) && (
{slots.prefix?.() ?? }
)} updateValue((e.target as HTMLInputElement).value)} /> {props.clearable && inputValue.value && !props.disabled && !props.readonly && ( updateValue('')} > )} {/* 密码切换图标 */} {props.password && ( (showPassword.value = !showPassword.value)} > )} {(slots.suffix || props.suffixIcon) && !props.password && (
{slots.suffix?.() ?? }
)} {props.showCount && ( {countText.value} )}
) } return () => { return (
{props.prepend || slots.prepend ? (
{slots.prepend?.() ?? (props.prepend && {props.prepend})}
) : null} {inputWrapper()} {props.append || slots.append ? (
{slots.append?.() ?? (props.append && {props.append})}
) : null}
) } }, })