import {designComponent, getComponentCls, onBeforeUnmount, PropType, useClasses, useRefs} from "@peryl/react-compose"; import {AutoTableScopeSlotsOption, AutoTableSlotsOption, iAutoTableRefer} from "./utils/AutoTable.utils"; import './auto-table.scss'; import {StyleProps, useStyle} from "../../uses/useStyle"; import {TableOptionSpace} from "./utils/TableOption.space"; export const AutoTable = designComponent({ name: 'auto-table', props: { ...StyleProps, option: { type: Object as PropType, required: true }, }, provideRefer: true, slots: AutoTableSlotsOption, scopeSlots: AutoTableScopeSlotsOption, setup({ props, slots, scopeSlots }) { const { refs, onRef } = useRefs({ el: HTMLElement }); const { styleComputed } = useStyle({ adjust: (ret) => {ret.size = props.option.config.size;} }); const classes = useClasses(() => props.option.hooks.onTableClass.exec([ getComponentCls('auto-table'), `auto-table-size-${styleComputed.value.size}` ])); props.option.hooks.onTableSetup.exec(undefined); onBeforeUnmount(() => {props.option.hooks.onTableDestroy.exec(undefined);}); const refer: iAutoTableRefer = { refs, props, slots, scopeSlots }; props.option.state.getAutoTable = () => refer; onBeforeUnmount(() => {props.option.state.getAutoTable = () => null;}); const onRefEl = (() => { let prev: HTMLDivElement | null = null; return (el: HTMLDivElement | null) => { if (el === prev) {return;} prev = el; props.option.hooks.onRefAutoTableEl.exec(el); onRef.el(el); }; })(); return { refer, render: () => (
{props.option.hooks.onTableOuterRender.exec()}
), }; }, });