import {createPlcPropOptions, PlcEmitsOptions} from "../plc/utils/plc.utils"; import {computed, designComponent, Fragment} from "@peryl/react-compose"; import {PlcScopeSlotsOptions} from "../plc/utils/plc.scope-slots"; import {usePlc} from "../plc/use/usePlc"; import {Select} from "../../Select"; import {createSelectOptionDecoder} from "../../Select/createSelectOptionDecoder"; import {iTableCellRenderScope} from "../table/utils/table.utils"; import {SelectPropOptions} from "../../Select/select.utils"; import {eMultipleValueType} from "../../../uses/useMultipleModel"; import {iFilterConfigSelect} from "../../FilterService/filter/filter.select"; import {injectPlainTable} from "../table/Table"; export const PlcSelect = designComponent({ name: 'plc-select', props: { ...createPlcPropOptions({ filterType: 'select', filterHandler: 'in', }), multiple: SelectPropOptions.multiple, valueType: SelectPropOptions.valueType, useOptionRenderNormal: { type: Boolean }, }, scopeSlots: { ...PlcScopeSlotsOptions, dynamic: (cellRenderScope: iTableCellRenderScope) => {}, }, emits: PlcEmitsOptions, slots: ['default'], setup({ props, scopeSlots, event, slots }) { const table = injectPlainTable(); const selectOptionDecoder = createSelectOptionDecoder(); /** * 当使用dynamic插槽渲染每一个选项时,每一行都应该有自己的decoder,公用一个decoder会导致renderKey一直刷新 * @author 韦胜健 * @date 2023.1.25 16:18 */ const dynamicOptionDecoderList = computed(() => { return (table.dataRef.value.data || []).map(() => { if (!scopeSlots.dynamic.isExist()) { return selectOptionDecoder; } else { return createSelectOptionDecoder(); } }); }); const filterConfig = computed((): iFilterConfigSelect => { return { renderOptions: () => slots.default(), getSelectOptions: () => selectOptionDecoder.options.map((so: any) => ({ label: so.props.label, val: so.props.val })), ...props.filterConfig, }; }); const utils = { /** * 多选时将值转化为数组对象 * @author 韦胜健 * @date 2022.12.4 15:29 */ getArrayValue: (value: any): any[] | null | undefined => { if (value == null) {return value;} if (Array.isArray(value)) { return value; } if (typeof value === "string") { if (props.valueType === eMultipleValueType.arrayjson) { return JSON.parse(value); } else { return value.split(','); } } }, /** * 值格式化为显示值,用于前端静态导出显示内容 * @author 韦胜健 * @date 2022.12.4 11:55 */ labelFormatter: (value: any): string | number | null | undefined => { if (value == null) {return value;} if (!props.multiple) { const item = selectOptionDecoder.options.find(i => i.props.val === value); return !!item ? item.props.label : value; } else { const arrayValue = utils.getArrayValue(value); if (!arrayValue) {return null;} return arrayValue.map(itemValue => { const item = selectOptionDecoder.options.find(i => i.props.val === itemValue); return !!item ? item.props.label : itemValue; }).join(','); } }, }; const { refer, render } = usePlc({ props, slots, scopeSlots, event, proxyPropsConfig: { formatter: () => { if (slots.default.isExist()) { return utils.labelFormatter; } }, filterConfig: () => filterConfig.value, }, defaultScopeSlots: { summary: () => null, normal: (scope) => { const decoder = dynamicOptionDecoderList.value[scope.node.state.index]; /*没有绑定字段,不渲染内容*/ if (!props.field || !decoder || !decoder.isRender()) {return null;} /*原始值*/ const sourceValue = scope.row[props.field]; if (sourceValue == null) {return null;} /*目标值,无论如何格式化为数组展示*/ const valueList = props.multiple ? utils.getArrayValue(sourceValue) : [sourceValue]; if (!valueList) {return null;} /*动态选项,每次render normal cell都decode解析scopeSlots.dynamic一遍,根据每次解析的内容计算显示值*/ /*否贼为静态选项,只有渲染plc的时候,只解析一遍slots.default*/ if (scopeSlots.dynamic.isExist()) { decoder.decode(scopeSlots.dynamic(scope)); } const lastIndex = valueList.length - 1; return valueList.map((val, index) => { const selectedOption = decoder.options.find(i => i.props.val === val)?.node; if (!selectedOption) { /*无匹配选项*/ return val; } if (props.useOptionRenderNormal) { /*渲染option的渲染内容*/ return {selectedOption.props.children || selectedOption.props.label}; } else { /*仅渲染文本,option的渲染内容仅供下拉的时候展示*/ return {selectedOption.props.label}{index !== lastIndex ? ',' : ''}; } }); }, edit: ({ row, plc, node }) => !plc.props.field ? null : (