import {StyleProps, ThemeSize} from "../../../../uses/useStyle"; import {ComponentEvent, ExtractPropTypes, iMouseEvent, MultipleClass, PropType, reactive, RenderNode, StyleProperties, tSlotsType} from "@peryl/react-compose"; import {PlainObject} from "@peryl/utils/event"; import {eFormValidateTrigger, iFormValidatePropsType, iValidateResult} from "../../../Form/validate/validate.utils"; import {tPlc, tPlcType} from "../../plc/utils/plc.utils"; import {PlainScroll} from "../../../Scroll"; import {tPlcCacheStateData} from "../../plc/use/usePlcPropsState"; import {tPlcData} from "../../plc/formatPlcList"; import {deepcopy} from "@peryl/utils/deepcopy"; import {iFormValidator} from "../../../Form/validate/createValidation"; /** * Table属性配置 * @author 韦胜健 * @date 2022.10.29 12:20 */ export const TablePropsOptions = { ...StyleProps, /*---------------------------------------basic-------------------------------------------*/ data: { type: Array as PropType }, // 数据 summaryData: { type: Array as PropType }, // 合计行数据 config: { type: Function as PropType }, // 配置列信息函数 defaultEditable: { type: Boolean }, // 是否默认开启编辑状态 editSourceData: { type: Boolean }, // 是否编辑原始数据 showRows: { type: [Number, String] as PropType<'auto' | number>, default: 'auto' }, // 表格显示高度,数字为显示固定行数高度,auto为显示所有行数据的高度,也就是自动高度 minShowRows: { type: Number, default: 3 }, // showRows为auto的时候,最小显示行数 border: { type: Boolean }, // 边框表格 headRowHeight: { type: [String, Number] }, // 表头行高 bodyRowHeight: { type: [String, Number] }, // 表体行高 virtual: { type: Boolean }, // 开启虚拟行滚动 virtualRowType: { type: String as PropType<'virtualIndex' | 'realIndex'>, }, // 虚拟滚动的行渲染类型,virtualIndex意思为使用虚拟的行索引作为key渲染行,好处是虚拟滚动时完全复用行组件实例;realIndex意味着虚拟滚动时新的行会重新初始化 virtualColumn: { type: Boolean }, // 开启虚拟列滚动 dynamicSize: { type: Boolean }, // 行虚拟滚动时启用动态高度 colDraggable: { type: Boolean }, // 列是否可拖拽移动 rowDraggable: { type: Boolean }, // 行是否可以拖拽移动 sort: { type: [Object, Array] as PropType }, // 当前排序字段 rules: { type: Object as PropType }, // 校验规则 anotherRules: { type: Object as PropType }, // 校验规则 spanMethod: { type: Function as PropType }, // 合并表体单元格的方法 size: { ...StyleProps["size"] }, // 表格尺寸 loading: { type: Boolean }, // 开启加载状态 formEditorFootRender: { type: Function as PropType<(formData: PlainObject) => any> },// 表单编辑器的foot渲染内容 rowClassFunc: { type: Function as PropType },// 行 className 的计算函数 cellClassFunc: { type: Function as PropType },// 单元格 className 的计算函数 cellStyleFunc: { type: Function as PropType },// 单元格 style 内联样式的计算函数 headCellClassFunc: { type: Function as PropType },// 表头单元格的 className 的计算函数 headCellStyleFunc: { type: Function as PropType },// 表头单元格 style 内联样式计算函数 }; export type TablePropsRowClassFunc = (node: iTableNode) => MultipleClass export type TablePropsCellClassFunc = (node: iTableNode, plc: tPlc) => MultipleClass export type TablePropsCellStyleFunc = (node: iTableNode, plc: tPlc) => StyleProperties export type TablePropsHeadCellClassFunc = (plc: tPlcType) => MultipleClass export type TablePropsHeadCellStyleFunc = (plc: tPlcType) => StyleProperties export type TablePropsSpanMethod = (scope: { node: iTableNode, plc: tPlc }) => { rowspan: number, colspan: number } export interface iTableSortData { field: string, desc: boolean } export type TablePropsSort = iTableSortData | iTableSortData[] /** * Table插槽配置 * @author 韦胜健 * @date 2022.10.29 12:20 */ export const TableSlotsOptions = ['default', 'noData'] as const; /** * Table属性类型 * @author 韦胜健 * @date 2022.10.29 12:21 */ export type iTablePropsType = ExtractPropTypes /** * 表格插槽类型 * @author 韦胜健 * @date 2022.10.29 12:21 */ export type iTableSlotsType = tSlotsType /** * table派发事件配置 * @author 韦胜健 * @date 2022.10.29 23:40 */ export const TableEmitsOptions = { onScrollLeft: (scrollLeft: number, part: TableHoverPart) => true, onVirtualMounted: (data: { scroll: PlainScroll }) => true, onClickRow: (node: iTableNode, e: iMouseEvent) => true, onDblclickRow: (node: iTableNode, e: iMouseEvent) => true, onClickCell: (node: iTableNode, e: iMouseEvent, plc: tPlc) => true, onDblclickCell: (node: iTableNode, e: iMouseEvent, plc: tPlc) => true, onContextmenuCell: (node: iTableNode, e: iMouseEvent, plc: tPlc) => true, onClickHead: (plc: tPlcType, e: iMouseEvent) => true, onConfigPlc: (data: { plcList: tPlcType[], stateData: tPlcCacheStateData }) => true, onCollectPlcData: (plcData: tPlcData) => true, onSetup: (refer: any) => true, onCurrentChange: (data: PlainObject | null) => true, onUpdateData: (data: PlainObject[] | null | undefined) => true, onOrderChange: (broList: tPlcType[]) => true, onClickRowDraggier: (data: { e: iMouseEvent, node: iTableNode }) => true, onClickColDraggier: (data: { e: iMouseEvent, plc: tPlcType }) => true, } as const; export type iTableEventType = ComponentEvent /** * table节点信息 * @author 韦胜健 * @date 2022.10.29 18:54 */ export interface iTableNode { data: PlainObject, // 原始数据 isSummary: boolean, // 是否为合计行数据 state: { index: number, // 节点索引 edit: boolean, // 行是否处于编辑状态 editRow: PlainObject, // 编辑的行对象 validateResultData: iValidateResult[] | null, // 校验信息 }, openEdit: () => void, // 开启编辑状态(不做任何处理) closeEdit: () => void, // 关闭编辑状态(不做任何处理) enableEdit: () => void, // 开启编辑状态(先判断当前是否可编辑,深度赋值一份data赋值给editRow,并且立即执行校验) cancelEdit: () => void, // 取消编辑状态 validate: () => Promise,// 校验数据 validateItem: (param: { label?: string, field?: string, trigger: typeof eFormValidateTrigger.TYPE }) => Promise,// 校验单个字段 clearValidate: () => void, // 清空校验信息 saveEdit: (newRow?: PlainObject) => Promise, // 保存编辑,将editRow以及newRow(请求得到的新对象)覆盖data } export type iTableMessyRender = (data: { vIndex: number, vid: string }) => RenderNode export type iTableMessyData = iTableNode | iTableMessyRender /** * 创建table node节点数据 * @author 韦胜健 * @date 2022.11.6 15:27 */ export function createTableNode( { formValidator, data, isSummary, editSourceData, defaultEditable, index, }: { formValidator: iFormValidator,// 校验器 data: PlainObject, // 行数据 isSummary?: boolean, // 是否为合计行 defaultEditable?: boolean, // 默认是否可编辑(合计行不可编辑) editSourceData?: boolean, // 是否编辑原始数据 index: number, }): iTableNode { return new TableNodeConstructor({ formValidator, data, isSummary: !!isSummary, state: { index, edit: !!editSourceData || !!defaultEditable, editRow: editSourceData ? data : deepcopy(data), validateResultData: null, }, }); } class TableNodeConstructor implements iTableNode { data: iTableNode["data"]; isSummary: iTableNode["isSummary"]; state: iTableNode["state"]; constructor(private param: Pick & { formValidator: iFormValidator }) { this.data = param.data; this.isSummary = param.isSummary; this.state = reactive(param.state); } openEdit = () => { if (!this.state.edit) { this.state.edit = true; } this.clearValidate(); }; closeEdit = () => { if (this.state.edit) { this.state.edit = false; } }; enableEdit = () => { if (this.state.edit) {return;} this.state.editRow = deepcopy(this.data); this.openEdit(); }; cancelEdit = () => { this.state.edit && this.closeEdit(); }; validate = async () => { await this.param.formValidator.methods.validate({ state: this.state, formData: this.state.editRow }); return !this.state.validateResultData || this.state.validateResultData.length == 0 ? null : { validateResultData: this.state.validateResultData, node: this }; }; validateItem = async (param: { label?: string, field?: string, trigger: typeof eFormValidateTrigger.TYPE }) => { await this.param.formValidator.methods.validateItem({ ...param, state: this.state, formData: this.state.editRow, }); }; clearValidate = () => {!!this.state.validateResultData && (this.state.validateResultData = null);}; saveEdit = async (newRow?: PlainObject) => { let { data, state: { editRow } } = this; newRow = newRow || editRow; Object.keys({ ...data, ...editRow, ...newRow }).forEach(k => this.data[k] = newRow![k]); }; } /** * 渲染单元格的作用域插槽参数类型 * @author 韦胜健 * @date 2022.10.29 19:00 */ export type iTableCellRenderScope = { plc: tPlc, node: iTableNode, row: PlainObject } /** * 渲染行的参数类型 * @author 韦胜健 * @date 2022/11/23 16:06 */ export type iTableRowRenderScope = Omit /** * table props.config的类型,用来配置plc * @author 韦胜健 * @date 2022.10.29 23:31 */ export type TablePropsConfig = (plcList: tPlcType[], flatList: tPlc[]) => void /** * table中的自定义校验函数参数类型 * @author 韦胜健 * @date 2022/11/7 19:42 */ // export type iTableValidatorParam = Omit & { formData: Exclude } /** * 目前鼠标处于table的位置 * @author 韦胜健 * @date 2022.10.29 23:37 */ export enum TableHoverPart { body = 'body', head = 'head' } export const TableSizeVariables = { headRowHeight: { large: 56, normal: 56, small: 40, mini: 36, }, bodyRowHeight: { large: 56, normal: 56, small: 40, mini: 36, }, }; export function getTableRowHeight(size: typeof ThemeSize.TYPE, props?: { headRowHeight?: number, bodyRowHeight?: number }) { return { headRowHeight: props?.headRowHeight || TableSizeVariables.headRowHeight[size], bodyRowHeight: props?.bodyRowHeight || TableSizeVariables.bodyRowHeight[size], }; }