import {iSelectCompositionParams} from "./select.utils"; import {Input} from "../Input"; import {createPublicSelectRender} from "./createPublicSelectRender"; import {reactive, useModel} from "@peryl/react-compose"; import {delay} from "@peryl/utils/delay"; import {createEffects} from "@peryl/utils/createEffects"; /** * 单选组合函数,支持普通单选以及虚拟列表的方式 * @author 韦胜健 * @date 2022/8/8 20:06 */ export function createSingleSelectRender(params: iSelectCompositionParams) { const { props, hooks, editComputed, scopeSlots, classes, isMatch } = params; const state = reactive({ /*input输入绑定值*/ inputValue: null as null | string }); const { effects } = createEffects(); const model = useModel(() => props.modelValue, params.emit.onUpdateModelValue, { onChange: async () => { await delay(); state.inputValue = selector.optionListHandler.getMatchLabel(); } }, effects); const selector = createPublicSelectRender(params, { isActivated: props => isMatch({ option: props.val, value: model.value }), effects, model, ...(() => { /** * 处理选中上一个值或者下一个值得时候的一些公共逻辑 * @author 韦胜健 * @date 2022.7.23 20:08 */ const handlePublic = (e: KeyboardEvent, getValueIndex: (index: number, length: number) => number) => { if (!model.value) { /*如果当前没有值,则直接设置为第一个值*/ model.value = selector.optionListHandler.getOptionPropsList()[0]?.val; } else { const optionPropsList = selector.optionListHandler.getOptionPropsList(); let index = optionPropsList.findIndex(i => i.val === model.value); model.value = optionPropsList[getValueIndex(index, optionPropsList.length)]?.val; state.inputValue = selector.optionListHandler.getMatchLabel(); } }; return { /*否则直接切换选中值*/ handlePrevWhenNotShow: (e: KeyboardEvent) => {handlePublic(e, (index, length) => index === 0 ? length - 1 : (index - 1));}, /*否则直接切换选中值*/ handleNextWhenNotShow: (e: KeyboardEvent) => {handlePublic(e, (index, length) => index === length - 1 ? 0 : index + 1);}, }; })(), /** * 选中某个选项的处理动作 * @author 韦胜健 * @date 2022/8/8 20:15 */ onSelectOption: (optionProps) => { if (optionProps.disabled || !editComputed.value.editable) {return;} /*应用绑定值*/ model.value = optionProps.val; /*立即应用 inputValue,等close的时候再应用有点慢了*/ state.inputValue = selector.optionListHandler.getMatchLabel(); /*关闭 popper*/ selector.popper.methods.hide(); }, defaultCurrent: undefined, }); effects.push(hooks.renderInput.use(() => { selector.optionListHandler.decode(); /*显示文本*/ const label: string = selector.optionListHandler.getMatchLabel(); /*空值占位符,没有focus的时候就显示真实的placeholder,否则显示当前匹配option的label*/ const placeholder = (selector.state.isFocus ? label : props.placeholder) || props.placeholder; /*input绑定值,focus的时候显示具体绑定值,没有focus的时候显示匹配option的label*/ const inputValue = selector.state.isFocus && !props.disableFilter ? state.inputValue : label; return selector.reference.renderReference({ slotExist: scopeSlots.reference.isExist, slotRender: () => scopeSlots.reference({ checked: selector.optionListHandler.getActivated()[0] }), defaultRender: () => ( ) }); })); effects.push(hooks.onPopperClose.use(() => { /*重置input绑定值,防止输入一半取消选择时显示不正确的文本*/ state.inputValue = selector.optionListHandler.getMatchLabel(); })); effects.push(hooks.onPopperShow.use(() => { state.inputValue = null; })); effects.push(hooks.onInputTextChange.use((val) => { /*应用绑定值*/ state.inputValue = val; })); effects.push(hooks.onInputClear.use(() => { /*点击clear的时候,应用输入绑定值*/ state.inputValue = null; })); return effects.clear; }