import {classnames, computed, designComponent, onBeforeUnmount, reactive, toRaw, useStyles} from "@peryl/react-compose";
import {createPlcPropOptions, PlcEmitsOptions} from "../plc/utils/plc.utils";
import {iTableNode} from "../table/utils/table.utils";
import {PlcScopeSlotsOptions} from "../plc/utils/plc.scope-slots";
import {PlainObject} from "@peryl/utils/event";
import {injectPlainTable} from "../table/Table";
import {usePlc} from "../plc/use/usePlc";
import {Dropdown} from "../../Dropdown";
import {Icon} from "../../Icon";
import {DropdownOption} from "../../DropdownOption";
import i18n from "../../i18n";
export const PlcExpand = designComponent({
name: 'plc-expand',
props: {
...createPlcPropOptions({
autoFixedLeft: true,
order: -9997,
width: 45,
align: 'center',
noPadding: true,
hideInForm: true,
noFilter: true,
noSort: true,
}),
toggleOnClickRow: { type: Boolean }, // 点击行的时候展开收起
fixedWidth: { type: Boolean }, // 固定展开内容的宽度
},
emits: {
...PlcEmitsOptions,
onExpand: (node: iTableNode) => true,
},
scopeSlots: {
...PlcScopeSlotsOptions,
expand: (scope: { node: iTableNode, row: PlainObject }) => null,
},
setup({ props, event, slots, scopeSlots }) {
const table = injectPlainTable();
const state = reactive({
expandData: [] as PlainObject[]
});
/*实时计算出来总列数*/
const totalSpan = computed(() => !table.plcData.value ? 1 : table.bodyPlcList.value?.length || 0);
const expandStyles = useStyles((styles) => {
if (!!props.fixedWidth) {
styles.width = `${table.plcData.value!.tableWidth - 20}px`;
}
});
const utils = {
/**
* 判断节点是否已经展开
* @author 韦胜健
* @date 2022/11/17 10:46
*/
isExpand: (data: PlainObject): boolean => {
return !!state.expandData.find(i => toRaw(i) === toRaw(data));
},
/**
* 渲染展开内容
* @author 韦胜健
* @date 2022/11/17 10:46
*/
renderExpand: (node: iTableNode, vIndex: number, vid: string, index: number) => {
return (
|
{scopeSlots.expand({ node, row: node.data })}
|
);
}
};
const methods = {
/**
* 展开节点
* @author 韦胜健
* @date 2022/11/17 10:46
*/
expand: (data: PlainObject) => {
if (utils.isExpand(data)) {return;}
state.expandData.push(data);
},
/**
* 收起节点
* @author 韦胜健
* @date 2022/11/17 10:46
*/
collapse: (data: PlainObject) => {
const index = state.expandData.findIndex(i => toRaw(i) === toRaw(data));
if (index === -1) {return;}
state.expandData.splice(index, 1);
},
/**
* 展开/收起节点
* @author 韦胜健
* @date 2022/11/17 10:46
*/
toggle: (data: PlainObject) => {
if (utils.isExpand(data)) {
methods.collapse(data);
} else {
methods.expand(data);
}
},
/**
* 展开所有节点
* @author 韦胜健
* @date 2022/11/17 10:46
*/
expandAll: () => {
state.expandData = [...table.props.data || []];
},
/**
* 收起所有节点
* @author 韦胜健
* @date 2022/11/17 10:47
*/
collapseAll: () => {
state.expandData = [];
},
};
/**
* 拦截行渲染
* @author 韦胜健
* @date 2022/11/17 14:15
*/
const weakMap = new WeakMap any>();
onBeforeUnmount(table.hooks.onTableMessyData.use(({ data }) => {
const newData = [] as any[];
data.forEach(item => {
newData.push(item);
if (typeof item === "function") {return;}
if (utils.isExpand(item.data)) {
let dataFunc = weakMap.get(item.data);
if (!dataFunc) {
dataFunc = (param: any) => utils.renderExpand(item, param.vIndex, param.vid, param.index);
weakMap.set(item.data, dataFunc);
}
newData.push(dataFunc);
}
});
return { data: newData };
}));
/**
* 点击行的时候展开收起
* @author 韦胜健
* @date 2022/11/17 14:32
*/
onBeforeUnmount(table.hooks.onClickCell.use(({ node }) => {
!!props.toggleOnClickRow && methods.toggle(node.data);
}));
const { render, refer } = usePlc({
props,
scopeSlots,
event,
slots,
defaultScopeSlots: {
head: () => (
{{
reference: () => (
),
popper: () => <>
>
}}
),
normal: ({ node }) => {
const isExpand = utils.isExpand(node.data);
return (
methods[isExpand ? 'collapse' : 'expand'](node.data)}>
);
},
summary: () => '',
},
externalRefer: {
utils,
},
});
return {
refer,
render,
};
},
});