import {computed, ExtractPropTypes, reactive, useNumber, useRefs} from "@peryl/react-compose"; import {PlcCollector} from "../group/PlcGroup"; import {usePlcPropsState} from "./usePlcPropsState"; import {nextPlcKey, PlcGroupPropsOptions, PlcPublicAttrs, tPlcGroup} from "../utils/plc.utils"; import {tPlcGroupScopeSlots, tPlcSlotsType} from "../utils/plc.scope-slots"; export function useBasePlcGroup( { props, scopeSlots, slots, }: { props: ExtractPropTypes, scopeSlots: tPlcGroupScopeSlots, slots: tPlcSlotsType, } ) { const { refs, onRef } = useRefs({ el: HTMLDivElement }); /*collector收集列信息*/ PlcCollector.child({ sort: () => refs.el!, injectDefaultValue: null }); const items = PlcCollector.parent(true); /*格式化props*/ const { numberState } = useNumber(props, ['order']); const { propsState, state } = usePlcPropsState(computed(() => ({ ...props, ...numberState, }) as Omit & typeof numberState)); /*核心暴露对象*/ const group: tPlcGroup = reactive({ refs, /*PlcPublicAttrs 在 copyPlc中会深度复制一遍,这里适配类型即可*/ ...PlcPublicAttrs, plcKey: `${props.title || ''}_${nextPlcKey()}`, group: true, children: items, /** * 这里这个props是可修改的对象,别名叫做propsState,来源是props,当props * 中的值发生变化时,自动修正使用props中的值 * @author 韦胜健 * @date 2021/6/2 15:57 */ props: propsState, slots: scopeSlots, refer: () => group, /*分组表头宽度调整时,将放大/缩小的列宽分配给每一个子列*/ setDurWidth: (durWidth: number) => { const itemDurWidth = Math.floor(durWidth / (items.value.length)); items.value.forEach(item => item.setDurWidth(itemDurWidth)); }, /*对propsState的state的修改,最好都通过setPropsState来进行,这样可以出发table的onConfigPlc这个hooks*/ setPropsState: (data: any) => { Object.entries(data).forEach(([key, val]) => {(propsState as any)[key] = val;}); // todo // table.emitConfigPlc(); }, /*有时候希望直接修改props的state,可以通过这个方法获取修改的对象,修改这个对象不会触发table的onConfigPlc这个hook*/ getState: () => state as any, getWidth: () => items.value.reduce((prev, child) => prev + child.getWidth(), 0), }); return { refer: group, render: () => (
{slots.default()}
) }; }