import {useHeadCellResize} from "./useHeadCellResize"; import {computed, designComponent, iHTMLElement, iMouseEvent, PropType, useClassCache, useRefs, useStyleCache} from "@peryl/react-compose"; import {toArray} from "@peryl/utils/toArray"; import {Table} from "../Table"; import {tPlcType} from "../../plc/utils/plc.utils"; import {PlainScroll} from "../../../Scroll"; import {Icon} from "../../../Icon"; import {renderHeadCell} from "../../plc/utils/plc.render"; import {useTooltip} from "../../../useTooltip"; import Tooltip from "../../../Tooltip"; import {getFunctionValue} from '@peryl/utils/getFunctionValue'; export const PltHeadCell = designComponent({ name: 'plt-head-cell', props: { table: { type: Object as PropType, required: true }, tablePlc: { type: Object as PropType, required: true }, scroll: { type: Function as PropType<() => PlainScroll>, required: true }, }, setup({ props }) { const { refs, onRef } = useRefs({ el: iHTMLElement }); const { resizeHandler } = useHeadCellResize(props.table, () => props.tablePlc); const sort = computed(() => { const { tablePlc, table } = props; if (!table.props.sort) {return null;} if (tablePlc.group) {return null;} const sortObj = toArray(props.table.props.sort!).find(i => i.field === tablePlc.props.field); if (!sortObj) {return null;} return !!sortObj.desc; }); const handler = { onClick: (e: iMouseEvent) => {props.table.hooks.onClickHeadCell.exec({ e, plc: props.tablePlc });}, onEnter: (e: iMouseEvent) => {props.table.hooks.onEnterHeadCell.exec({ e, plc: props.tablePlc });}, onLeave: (e: iMouseEvent) => {props.table.hooks.onLeaveHeadCell.exec({ e, plc: props.tablePlc });}, }; useTooltip({ overflow: true, tooltip: () => ({ popperSize: props.table.styleComputed.value.size, reference: refs.el, message: props.tablePlc.props.title }), }); const classes = useClassCache(() => { const ret = [ props.tablePlc.classes.head as any, props.tablePlc.props.headCls as any, !!props.table.props.headCellClassFunc ? props.table.props.headCellClassFunc(props.tablePlc) : null ]; return props.table.hooks.onCellClass.exec({ classes: ret, plc: props.tablePlc }).classes; }); const styles = useStyleCache(() => { const style: any = { ...props.tablePlc.styles.head as any, ...(!!props.table.props.headCellStyleFunc ? props.table.props.headCellStyleFunc(props.tablePlc) : {}) }; return props.table.hooks.onCellStyle.exec({ styles: style, plc: props.tablePlc }).styles; }); return { render: () => { const content = renderHeadCell(props.tablePlc); return ( {content} {!!props.tablePlc.props.tip && , popper: () => getFunctionValue(props.tablePlc.props.tip) }}/>} {sort.value != null && (
)} ); } }; }, });