import {ComponentEvent, createSyncHooks, ExtractPropTypes, PropType, reactive, RenderNode, tScopeSlotsType, tSlotsType} from "@peryl/react-compose"; import {EditProps, tEditComputed} from "../../uses/useEdit"; import {StyleProps, ThemeSize} from "../../uses/useStyle"; import {Scroll} from "../Scroll"; import {PlainObject} from "@peryl/utils/event"; import {iIsMatch, IsMatchPropsOption} from "../../utils/isObjectCommonMatch"; /** * 是否匹配搜索关键字判断函数类型 * @author 韦胜健 * @date 2022/7/25 10:52 */ export interface iIsMatchSearch { (searchText: string, optionProps: iSelectOptionProps): boolean; } /** * SelectPanel额外属性定义 * @author 韦胜健 * @date 2022/7/25 10:52 */ export const SelectPanelOptionPropOptions = { size: StyleProps.size, // 大小尺寸 optionAlign: { type: String }, // 每个option的对其方式 searchText: { type: String }, // 当前搜索关键字 isMatchSearch: { type: Function as PropType }, // 判断是否匹配搜索结果函数 multiple: { type: Boolean }, // 是否多选 disableAutoSetOptionIndex: { type: Boolean }, // 禁用自动给SelectOption设置props.index索引 noMatchLabel: { type: String }, // 筛选无匹配的时候展示文本 noDataLabel: { type: String }, // 没有选项的时候展示文本 handleCheckAll: { type: Function as PropType<(options: iSelectOptionProps[]) => void> }, // 手动处理全选 handleUncheckAll: { type: Function as PropType<(options: iSelectOptionProps[]) => void> }, // 手动处理清空 handleCheckReverse: { type: Function as PropType<(options: iSelectOptionProps[]) => void> }, // 手动处理反选 } as const; /** * Select属性定义 * @author 韦胜健 * @date 2022/7/25 10:52 */ export const SelectPropOptions = { ...EditProps, ...StyleProps, ...IsMatchPropsOption, modelValue: {}, disableFilter: { type: Boolean }, // 是否可以输入筛选 inputAttrs: { type: Object as PropType },// Input组件额外属性 inputSlots: { type: Object as PropType> },// Input组件的插槽 popperAttrs: {}, // Popper组件额外属性 noClear: { type: Boolean }, // Popper组件额外属性 suffixIcon: { type: String, default: 'pi-down' },// Popper组件额外属性 handleFilterChange: { type: Function as PropType<(text: string) => void> },// 手动处理搜索值变化 scrollNum: { type: Number, default: 6 }, // 最多显示的下拉个数,超过这个数就会滚动展示 scrollHeight: { type: Number, default: null },// 滚动展示下拉选项的时候的高度 virtualData: { type: Array as PropType }, virtualRowSize: { type: Number },// 虚拟滚动的时候的行高 ...SelectPanelOptionPropOptions, } as const; /** * Select属性类型 * @author 韦胜健 * @date 2022/7/25 10:52 */ export type iSelectProps = ExtractPropTypes /** * SelectOption属性定义 * @author 韦胜健 * @date 2022/7/25 10:52 */ export const SelectOptionPropOptions = { label: { type: String }, val: {}, icon: { type: String }, disabled: { type: Boolean }, index: { type: Number }, data: { type: Object as PropType }, } as const; /** * SelectOption属性类型 * @author 韦胜健 * @date 2022/7/25 10:52 */ type _iSelectOptionProps = Readonly> export type iSelectOptionProps = Partial<{ -readonly [k in keyof _iSelectOptionProps]: _iSelectOptionProps[k] }> /** * Select hooks内部公共钩子函数 * @author 韦胜健 * @date 2022/7/25 10:52 */ export function useSelectHook() { return { renderInput: createSyncHooks<(content: RenderNode) => void>(), onPopperClose: createSyncHooks<() => void>(), onPopperShow: createSyncHooks<() => void>(), onPopperHide: createSyncHooks<() => void>(), onInputTextChange: createSyncHooks<(val: any) => void>(), onInputClear: createSyncHooks<() => void>(), onTriggerShow: createSyncHooks<() => void>(), onTriggerHide: createSyncHooks<() => void>(), }; } export const SelectEmitsOption = { onUpdateModelValue: (val?: any) => true, onBlur: (e: FocusEvent) => true, onFocus: (e: FocusEvent) => true, } as const; export const SelectSlotsOption = ['default', 'noData', 'noMatch', 'loadingContent'] as const; export const SelectScopeSlotsOption = { reference: (scope: { checked: iSelectOptionProps | iSelectOptionProps[] | null | undefined }) => null, } as const; /** * Select内部组合函数公共参数类型 * @author 韦胜健 * @date 2022/7/25 10:52 */ export type iSelectCompositionParams = { props: iSelectProps, hooks: ReturnType, isMatch: iIsMatch, emit: ComponentEvent["emit"], getPublicPopperAttrs: () => any, slots: tSlotsType, scopeSlots: tScopeSlotsType, editComputed: tEditComputed, styleComputed: { value: { size: typeof ThemeSize.TYPE } }, classes: { value: any, }, scrollHeight: { value: number } } /** * Select内部工具对象 * @author 韦胜健 * @date 2022/7/25 10:53 */ export const SelectUtils = { isMatchSearch: (searchText: string | null | undefined, optionProps: iSelectOptionProps, props: { isMatchSearch?: iIsMatchSearch }) => { if (searchText == null) { return true; } if (!!props.isMatchSearch) { return props.isMatchSearch(searchText, optionProps); } return !!optionProps.label && optionProps.label.toLowerCase().indexOf(searchText.toLowerCase()) > -1; }, }; export function createSelectHover( { defaultCurrent, refs, scrollHeight, getMatchOptionsLength, }: { defaultCurrent?: number, refs: { scroll: typeof Scroll.use.class | null | undefined }, scrollHeight: () => number, getMatchOptionsLength: () => number, } ) { /*当前hover的index*/ const state = reactive({ current: defaultCurrent as undefined | number, }); const resetCurrent = () => state.current = defaultCurrent; /*上下移动的时候,令滚动到目标位置*/ const scrollToOption = () => { if (!refs.scroll) { return; } /** * 找到目标option的dom元素 * - 如果当前scrollTop已经大于option的offsetTop,则滚动到option的offsetTop * - 如果当前scrollTop以及小于option的bottom,则滚动到option的bottom * @author 韦胜健 * @date 2022/8/8 20:00 */ const optionEl = refs.scroll.refs.wrapper?.querySelector(`[data-sdi="${state.current}"]`) as HTMLElement; if (optionEl != null) { let top: number; top = optionEl.offsetTop; if (refs.scroll.refs.wrapper!.scrollTop > top) { refs.scroll.methods.scrollTop(top); } top = optionEl.offsetTop - (scrollHeight() - optionEl.offsetHeight); if (refs.scroll.refs.wrapper!.scrollTop < top) { refs.scroll.methods.scrollTop(top); } } }; /*hover上一个option*/ const prev = () => { if (state.current == null) { /*如果没有hover的元素,则hover最后一个元素*/ state.current = getMatchOptionsLength() - 1; } else { if (state.current === 0) { /*如果当前hover元素为第一个元素,则hover最后一个元素*/ state.current = getMatchOptionsLength() - 1; } else { /*否则hover上一个元素*/ state.current--; } } scrollToOption(); }; /*hover下一个option*/ const next = () => { /*如果没有hover的元素,则hover第一个元素*/ if (state.current == null) { state.current = 0; } else { if (state.current === getMatchOptionsLength() - 1) { /*如果当前hover为最后一个元素,则hover第一个元素*/ state.current = 0; } else { /*否则hover下一个元素*/ state.current++; } } scrollToOption(); }; /*enter某个option的时候,令这个option为hover的元素*/ const onEnterOption = (props: iSelectOptionProps) => { state.current = props.index!; }; return { state, prev, next, onEnterOption, resetCurrent }; } export const SelectDefaultVirtualRowSize: Record = { [ThemeSize.mini]: 27, [ThemeSize.small]: 31, [ThemeSize.normal]: 34, [ThemeSize.large]: 34, };