import {AutoWidthInput} from "../AutoWidthInput"; import {iInputCompositionData, inputScrollEnd} from "./input.utils"; import Icon from "../Icon"; import {iHTMLElement, useClasses, useModel, useRefs} from "@peryl/react-compose"; import {eKeyboardKeys, getKeyNameByKeyboardEvent} from "@peryl/utils/keyboard"; import {delay} from "@peryl/utils/delay"; import {createEffectiveHandler} from "../../utils/createEffectiveHandler"; import {createEffects} from "@peryl/utils/createEffects"; import {useDragHorizontalScroll} from "../../uses/useDragHorizontalScroll"; /** * 渲染多值输入框 * @author 韦胜健 * @date 2022.5.22 10:06 */ export const useMultipleInput = createEffectiveHandler(({ hooks, props, model, refs, editComputed, emit, styleComputed }: iInputCompositionData) => { const { effects } = createEffects(); const text = useModel(() => props.multipleText as any, emit.onUpdateMultipleText, undefined, effects); const { refs: innerRefs, onRef: innerOnRef } = useRefs({ container: iHTMLElement }, { effects }); const handler = { onRef: (input: typeof AutoWidthInput.use.class | null) => hooks.onRefInput.exec(input?.refs.input || null), onMousedownContainer: () => {delay(23).then(() => refs.input?.focus());}, onRemoveTag: async (index: number) => { if (!editComputed.value.editable) {return;} if (!model.value || !model.value.length) {return; } if (!!props.handleRemoveTag) { const flag = await props.handleRemoveTag({ index, val: model.value[index] }); if (flag === false) {return;} } else { const val = model.value as any[]; val.splice(index, 1); model.value = [...val]; } hooks.onTagChange.exec(undefined); }, onAddTag: async () => { if (!text.value) {return;} if (!!props.maxLength && !!model.value && model.value.length >= props.maxLength) { return; } if (!!props.handleAddTag) { const flag = await props.handleAddTag(text.value); if (flag === false) {return;} } else { const texts = !!props.multipleSeparator ? text.value.split(props.multipleSeparator) : text.value; model.value = [...model.value || [], ...texts]; } text.value = null; if (!props.tagsWrap) { delay(23).then(() => {inputScrollEnd(() => refs.el);}); } hooks.onTagChange.exec(undefined); }, onChange: (val: any) => { text.value = val; hooks.onInput.exec(val); }, onKeydown: async (e: KeyboardEvent) => { switch (getKeyNameByKeyboardEvent(e)) { case eKeyboardKeys.enter: await handler.onAddTag(); return; case eKeyboardKeys.backspace: !(e as any).target.value && model.value && handler.onRemoveTag((model.value as any[]).length - 1); return; } }, }; effects.push(hooks.onClasses.use(prev => [ ...prev, { 'input-multiple': props.multiple, } ])); effects.push(hooks.onRefInput.use((() => { let input = null as null | HTMLInputElement; return (el) => { if (!!input) { input.removeEventListener('keydown', handler.onKeydown); } input = el; if (!input) {return;} input.addEventListener('keydown', handler.onKeydown); }; })())); const classes = useClasses(() => [ 'input-box input-fill input-tag-container', { 'input-tag-container-wrap': props.tagsWrap, } ], effects); effects.push(hooks.onRenderDefault.use(() => (   {(model.value as any)?.map((item: string, index: number) => ( {item} {!!editComputed.value.editable && handler.onRemoveTag(index)}/>} ))} {styleComputed.value.inputMode !== 'text' && ( )} ))); effects.push(useDragHorizontalScroll(() => innerRefs.container)); return effects.clear; });