import * as React from "react"; import { RefObject } from "react"; import { Button, Checkbox as AntdCheckbox, Popconfirm, Popover, Tooltip, Space } from "antd"; import "./index.css"; import { ActionConfig, DtmplData, DtmplConfig, DoEditParam } from "../../tmpl/interface"; import { FormInstance } from "antd/lib/form/hooks/useForm"; import FieldsEditCard from "../../detail/edit/fields-edit-card"; import { SizeType } from "antd/lib/config-provider/SizeContext"; import ActionUtils from "./utils"; import Icon from "../../icon/aliIcon"; import { ButtonType } from "antd/lib/button"; import { LocaleContext } from "../../locale/LocaleProvider"; import ProgramConfig from "../../units"; export type ActionButtonIconType = "icon" | "icon-title" | "title"; export type ActionButtonType = "primary-block" | "dashed" | "primary"; export type ActionButtonHiddenType = "hidden" | "disabled"; interface ActionProps { serverKey?: string; disabled?: boolean; actionConfig: ActionConfig; // ltmplConfig:LtmplConfig, data?: DtmplData[] | object[]; doAction: (string, Array, object) => void; serial?: number; buttonType?: ActionButtonType; hiddenType?: ActionButtonHiddenType; buttonSize?: SizeType; iconType: ActionButtonIconType; formRef?: RefObject; loading?: boolean; doEdit?: (doEditParam: DoEditParam) => void; } interface ActionState { popoverOpen?: boolean; appDtmplConfig?: DtmplConfig, formRef?: RefObject } export default class Action extends React.PureComponent< ActionProps, ActionState > { static defaultProps = { hiddenType: "hidden", buttonSize: "middle", iconType: "icon-title", loading: false, //formRef:React.createRef(), }; static contextType = LocaleContext; context: React.ContextType; state = { popoverOpen: false, appDtmplConfig: null, formRef: React.createRef() }; componentDidMount() { const { data, actionConfig } = this.props; let d = data && data[0] ? data[0] : null let appDtmplConfig = ProgramConfig.getAppDtmplConfig( { id: actionConfig.id, "groups": [{ "fields": actionConfig.writes }] }, d && data[0]['fieldMap'] ? { ...d } : d ? { fieldMap: { ...d } } : {}, null ); this.setState({ appDtmplConfig }); } componentDidUpdate(prevProps) { const { } = this.props; if (this.props != prevProps) { // this.setState({collapse:!this.state.collapse}) } } //formRef = React.createRef(); isShow = () => { const { actionConfig, data } = this.props; return ActionUtils.isShow(actionConfig?.preposes, data, this.getFormRef()); }; getFormRef = () => { return this.state.formRef; } getValue = () => { const { data, actionConfig, formRef } = this.props; let value = {}; //从 dtmplData 与 ref //dtmplData取第一个 let dtmplValue = data ? data[0] : undefined; let upperFormValue = formRef ? formRef.current : this.getFormRef().current; for (let fieldConfig of actionConfig.writes) { if (!fieldConfig) { continue; } if (upperFormValue && upperFormValue.getFieldValue(fieldConfig.id)) { value[fieldConfig.id] = ActionUtils.getFormRefValue( upperFormValue, fieldConfig.id ); } else { if (dtmplValue) { const { requiredRuleCriteria, usableRuleCriteria, relValueField, relValueField3, relMinValueField, relMaxValueField } = fieldConfig; const temDtmplValue = dtmplValue["fieldMap"] ? dtmplValue["fieldMap"] : dtmplValue; if (temDtmplValue) { value[fieldConfig.id] = temDtmplValue[fieldConfig.id]; // 当前表单项 requiredRuleCriteria && requiredRuleCriteria.forEach(item => { // 处理必填关联数据 value[item.id] = temDtmplValue[item.id]; }); usableRuleCriteria && usableRuleCriteria.forEach(item => { // 处理可用关联数据 value[item.id] = temDtmplValue[item.id]; }); if (relValueField) value[relValueField.id] = temDtmplValue[relValueField.id]; // 次条件 if (relValueField3) value[relValueField3.id] = temDtmplValue[relValueField3.id]; // 三条件 if (relMinValueField) value[relMinValueField.id] = temDtmplValue[relMinValueField.id]; // 最小值 if (relMaxValueField) value[relMaxValueField.id] = temDtmplValue[relMaxValueField.id]; // 最大值 } } } } console.log("actionValue:", value); return value; }; onOk = (value: object) => { console.log("action form value:", value); const { actionConfig, doAction } = this.props; this.setState({ popoverOpen: false, }); doAction(actionConfig.id, this.getCodes(), value); }; onCancel = () => { this.setState({ popoverOpen: false, }); }; onValuesChange = (value: object, allValues) => { const { data, formRef } = this.props; const { appDtmplConfig } = this.state; //debugger if (formRef && formRef.current) { formRef.current.setFieldsValue(value); } if (appDtmplConfig?.onValuesChange) { //自定义值修改逻辑 appDtmplConfig.onValuesChange( value, { code: data ? data[0]['code'] : null, fieldMap: allValues, }, this.getFormRef().current ); } }; getButtonType = () => { const { buttonType, actionConfig } = this.props; let bType: ButtonType = !buttonType ? "dashed" : buttonType.split("-")[0] == "dashed" ? "dashed" : "primary"; return buttonType ? bType : actionConfig.buttonType ? actionConfig.buttonType : "dashed"; }; blockButton = (): boolean => { const { buttonType } = this.props; let block = buttonType && buttonType.includes("block"); return block; }; formButton = () => { const { popoverOpen } = this.state; const { actionConfig, disabled, hiddenType, buttonSize, serverKey, loading } = this.props; let disabled_ = disabled; if (!this.isShow()) { if (!hiddenType || hiddenType == "hidden") { return null; } else { disabled_ = true; } } return ( this.onCancel()} fields={actionConfig.writes} value={this.getValue()} onOk={this.onOk} onValuesChange={this.onValuesChange} /> } > ); }; getTitle = () => { const { actionConfig, iconType } = this.props; const { translate } = this.context; let title = null; if (actionConfig.icon) { if (iconType == "icon-title") { title = ( {translate("${" + actionConfig.title + "}")} ); } else if (iconType == "icon") { title = ( ); } else { title = translate("${" + actionConfig.title + "}"); } } else { title = translate("${" + actionConfig.title + "}"); } if (actionConfig.tip && iconType != "icon") { title = ( <> {translate("${" + actionConfig.title + "}")} ); } return title; }; getCodes = () => { const { data } = this.props; let codes = []; if (data) { if (data instanceof Array) { for (let d of data) { if (d) { codes.push(d["code"]); } } } } return codes; }; withoutFormButton = () => { const { actionConfig, doAction, data, loading, disabled, buttonType, hiddenType, buttonSize, } = this.props; const { translate } = this.context; let disabled_ = disabled; if (!this.isShow()) { if (!hiddenType || hiddenType == "hidden") { return null; } else { disabled_ = true; } } let codes = this.getCodes(); let block = this.blockButton(); let bType = this.getButtonType(); return !actionConfig.beforeExeConfirm || actionConfig.beforeExeConfirm == "default" ? ( { doAction(actionConfig.id, codes, {}); }} disabled={disabled_} okText={translate("${确定}")} cancelText={translate("${取消}")} > ) : actionConfig.beforeExeConfirm == "danger" ? ( { doAction(actionConfig.id, codes, {}); }} disabled={disabled_} okText={translate("${确定}")} cancelText={translate("${取消}")} okType={"danger"} > ) : ( ); }; // 操作编辑模板 renderEditButton = () => { const { actionConfig, disabled, hiddenType, buttonSize, loading, data, doEdit } = this.props; let disabled_ = disabled; if (!this.isShow()) { if (!hiddenType || hiddenType === "hidden") { return null; } else { disabled_ = true; } } return ; } render() { const { actionConfig } = this.props; if (actionConfig.dtmplSourceId) { // 操作按钮存在关联编辑模板 return this.renderEditButton(); } return ( <> {actionConfig.writes && actionConfig.writes.length > 0 ? this.formButton() : this.withoutFormButton()} ); } }