import {designComponent, fixInputCursor, getComponentCls, iHTMLInputElement, useClasses, useModel, useRefs, useStyles} from "@peryl/react-compose"; import './auto-width-input.scss'; import {unit} from "@peryl/utils/unit"; const FixInput = fixInputCursor('input'); /** * 自适应宽度的输入框 * @author 韦胜健 * @date 2022.5.21 0:25 */ export const AutoWidthInput = designComponent({ props: { modelValue: {}, minWidth: { type: [String, Number] }, maxWidth: { type: [String, Number] }, placeholder: {}, disabled: {}, readonly: {}, nativeAttrs: { type: Object }, }, emits: { onUpdateModelValue: (val?: any) => true, }, setup({ props, event: { emit } }) { const { refs, onRef } = useRefs({ input: iHTMLInputElement }); const model = useModel(() => props.modelValue, emit.onUpdateModelValue); const handler = { onInput: (e: any) => { model.value = e.target.value; }, }; const classes = useClasses(() => [ getComponentCls('auto-width-input'), ]); const spanStyles = useStyles((style) => { if (!!props.maxWidth) {style.maxWidth = unit(props.maxWidth);} if (!!props.minWidth) {style.minWidth = unit(props.minWidth);} }); return { refer: { refs, }, render: () => ( {model.value as string} ) }; }, });