import {iInputNumberPublic} from "./useInputNumber.public"; import {createEffects} from "@peryl/utils/createEffects"; import {useMultipleModel} from "../../uses/useMultipleModel"; import {reactive} from "@peryl/react-compose"; export function useInputNumberMultiple( { props, hooks, event, utils, effectiveInputValue }: iInputNumberPublic ) { const { effects } = createEffects(); const model = useMultipleModel(() => props.modelValue as (string | number)[] | undefined, event.emit.onUpdateModelValue as any, props); const state = reactive({ /*文本框绑定值*/ inputValue: null as string | null | undefined | number }); const handler = { onUpdateModelValue: (vals: any) => { model.value = vals; }, onUpdateMultipleText: (val: string | undefined) => { state.inputValue = val; }, handleAddTag: () => { const { val, unit } = effectiveInputValue.value; if (val == null) { return false; } const checkValue = utils.checkValue({ val, unit }); if (checkValue == state.inputValue && checkValue != null) { const modelValue = [...model.value || []]; modelValue.push(checkValue); model.value = modelValue; state.inputValue = undefined; } return false; }, }; /** * 计算当前有效的输入值 * @author 韦胜健 * @date 2023/2/10 11:31 */ effects.push( hooks.onGetEffectiveInputValue.use(() => { return utils.getEffectiveValue(state.inputValue, undefined); }) ); /** * 绑定输入文本的属性 * @author 韦胜健 * @date 2023/2/10 11:31 */ effects.push( hooks.onGetInputEditAttrs.use(() => ({ handleAddTag: handler.handleAddTag, multiple: true, multipleText: state.inputValue, modelValue: model.value, onUpdateModelValue: handler.onUpdateModelValue, onUpdateMultipleText: handler.onUpdateMultipleText, suggestionDisabledOpenOnEnter: true, })) ); /** * 失去焦点的时候重置输入的本文 * @author 韦胜健 * @date 2023/2/10 11:34 */ effects.push( hooks.onBlur.use(() => { state.inputValue = undefined; }) ); /** * 处理点击加号动作 * @author 韦胜健 * @date 2023/2/10 11:34 */ effects.push( hooks.onAdd.use(val => { state.inputValue = val; }) ); /** * 处理点击减号动作 * @author 韦胜健 * @date 2023/2/10 11:34 */ effects.push( hooks.onSub.use(val => { state.inputValue = val; }) ); /** * 处理拖拽值变化动作 * @author 韦胜健 * @date 2023/2/10 11:35 */ effects.push( hooks.onResize.use(val => { if (!props.availableUnits || !props.availableUnits.length) { state.inputValue = undefined; } const { unit: _unit } = utils.getValueInfo(state.inputValue); const unit = _unit || (state.inputValue == null || !String(state.inputValue).trim().length ? props.defaultUnit : undefined); let inputValue = !unit ? val : `${val}${unit}`; handler.onUpdateMultipleText(typeof inputValue === "number" ? String(inputValue) : inputValue); }) ); return effects.clear; }