import {ExtractPropTypes} from "@peryl/react-compose"; import {createProxyProps, iPlcEventType, iPlcPropsProxyConfig, PlcPropsOptions, tPlc, tPlcType} from "../utils/plc.utils"; import {PlcScopeSlotsOptions, tPlcScopeSlots, tPlcSlots} from "../utils/plc.scope-slots"; import {useBasePlc} from "./useBasePlc"; import {iTableCellRenderScope} from "../../table/utils/table.utils"; type tPlcDefaultScopeSlotsOptions = Partial export function usePlc>({ props: _props, slots, scopeSlots, event, defaultScopeSlots, externalRefer, proxyPropsConfig }: { event: iPlcEventType, slots: tPlcSlots, props: ExtractPropTypes, scopeSlots: tPlcScopeSlots, defaultScopeSlots?: tPlcDefaultScopeSlotsOptions, externalRefer?: iExternalRefer, proxyPropsConfig?: iPlcPropsProxyConfig, }) { const props = createProxyProps(_props, proxyPropsConfig); const formatScopeSlots: tPlcScopeSlots = { ...scopeSlots, head: Object.assign((scope: { plc: tPlcType }) => { if (scopeSlots.head.isExist()) { return scopeSlots.head(scope); } if (!!defaultScopeSlots && defaultScopeSlots.head) { return defaultScopeSlots.head(scope); } return scope.plc.props.title; }, { isExist: () => { if (scopeSlots.head.isExist()) { return true; } if (!!defaultScopeSlots && !!defaultScopeSlots.head) { return true; } return false; }, }), normal: Object.assign((scope: iTableCellRenderScope) => { if (scopeSlots.normal.isExist()) { return scopeSlots.normal(scope); } if (!!defaultScopeSlots && defaultScopeSlots.normal) { return defaultScopeSlots.normal(scope); } return !scope.plc.props.field ? '' : scope.row[scope.plc.props.field]; }, { isExist: () => { if (scopeSlots.normal.isExist()) { return true; } if (!!defaultScopeSlots && !!defaultScopeSlots.normal) { return true; } return false; }, }), edit: Object.assign((scope: iTableCellRenderScope) => { if (scopeSlots.edit.isExist()) { return scopeSlots.edit(scope); } if (!!defaultScopeSlots && defaultScopeSlots.edit) { return defaultScopeSlots.edit(scope); } return formatScopeSlots.normal(scope); }, { isExist: () => { if (scopeSlots.edit.isExist()) { return true; } if (!!defaultScopeSlots && !!defaultScopeSlots.edit) { return true; } return false; }, }), form: Object.assign((scope: iTableCellRenderScope) => { if (scopeSlots.form.isExist()) { return scopeSlots.form(scope); } if (!!defaultScopeSlots && defaultScopeSlots.form) { return defaultScopeSlots.form(scope); } return formatScopeSlots.edit(scope); }, { isExist: () => { if (scopeSlots.form.isExist() || scopeSlots.edit.isExist()) { return true; } if (!!defaultScopeSlots && (!!defaultScopeSlots.form || !!defaultScopeSlots.edit)) { return true; } return false; }, }), summary: Object.assign((scope: iTableCellRenderScope) => { if (scopeSlots.summary.isExist()) { return scopeSlots.summary(scope); } if (!!defaultScopeSlots && defaultScopeSlots.summary) { return defaultScopeSlots.summary(scope); } return formatScopeSlots.normal(scope); }, { isExist: () => { if (scopeSlots.summary.isExist()) { return true; } if (!!defaultScopeSlots && !!defaultScopeSlots.summary) { return true; } return false; }, }), }; const { render, refer } = useBasePlc({ props, event, slots, scopeSlots: formatScopeSlots, }); if (!!externalRefer) { const plcKeys = Object.keys(refer); const externalKeys = Object.keys(externalRefer); const conflictKey = externalKeys.find(i => plcKeys.indexOf(i) > -1); if (!!conflictKey) { throw new Error(`usePlc externalRefer错误,出现冲突的key:${conflictKey}`); } } return { render, refer: Object.assign(refer, externalRefer) as iExternalRefer & tPlc }; } export type iPlcRefer = ReturnType['refer']