评价任务 改为dva数据流转模式
This commit is contained in:
@ -1,27 +1,37 @@
|
||||
import React, { forwardRef, useImperativeHandle, useEffect, useState } from 'react';
|
||||
import { Card, Form } from 'antd';
|
||||
import { Card } from 'antd';
|
||||
import styles from '../supplierTaskManageAdd.less';
|
||||
import SupplierSelector from '@/components/SupplierSelector';
|
||||
import type { TaskAddRequest,SupplierItem } from '@/servers/types/supplierEvaluateTask';
|
||||
import type { SupplierItem } from '@/servers/dao/supplierEvaluateTask';
|
||||
import type { Dispatch } from 'umi';
|
||||
import { connect } from 'umi';
|
||||
import type { SupplierTaskModelState } from '@/models/supplierTaskManage';
|
||||
|
||||
interface SupplierSelectStepProps {
|
||||
formData: Partial<TaskAddRequest>; // 从父组件传递的表单数据
|
||||
onFormDataChange: (data: Partial<TaskAddRequest>) => void; // 表单数据变更的回调函数
|
||||
supplierTaskManage: SupplierTaskModelState;
|
||||
dispatch: Dispatch;
|
||||
innerRef?: any; // 使用 innerRef 作为属性名
|
||||
}
|
||||
|
||||
const SupplierSelectStep = forwardRef<any, SupplierSelectStepProps>(({ formData, onFormDataChange }, ref) => {
|
||||
// 定义组件,使用 innerRef 代替直接的 ref
|
||||
const SupplierSelectStepComponent = (props: SupplierSelectStepProps) => {
|
||||
const { supplierTaskManage, dispatch, innerRef } = props;
|
||||
|
||||
// 从model获取数据
|
||||
const { taskFormData } = supplierTaskManage;
|
||||
|
||||
// 内部状态,避免直接操作formData导致循环更新
|
||||
const [selectedSuppliers, setSelectedSuppliers] = useState<SupplierItem[]>([]);
|
||||
|
||||
// 当formData.selectedSuppliers更新时,同步到本地状态
|
||||
// 当taskFormData.selectedSuppliers更新时,同步到本地状态
|
||||
useEffect(() => {
|
||||
if (formData.selectedSuppliers && formData.selectedSuppliers.length > 0) {
|
||||
setSelectedSuppliers(formData.selectedSuppliers);
|
||||
if (taskFormData.selectedSuppliers && taskFormData.selectedSuppliers.length > 0) {
|
||||
setSelectedSuppliers(taskFormData.selectedSuppliers);
|
||||
}
|
||||
}, [formData.selectedSuppliers]); // 只在表单ID变化时更新(编辑模式加载时)
|
||||
}, [taskFormData.selectedSuppliers]); // 只在表单ID变化时更新(编辑模式加载时)
|
||||
|
||||
// 暴露表单方法给父组件
|
||||
useImperativeHandle(ref, () => ({
|
||||
// 暴露表单方法给父组件,使用 innerRef
|
||||
useImperativeHandle(innerRef, () => ({
|
||||
validateFields: () => {
|
||||
// 这里可以添加自定义验证逻辑
|
||||
return Promise.resolve();
|
||||
@ -51,10 +61,13 @@ const SupplierSelectStep = forwardRef<any, SupplierSelectStepProps>(({ formData,
|
||||
// 更新本地状态
|
||||
setSelectedSuppliers(suppliersWithEvaluators);
|
||||
|
||||
// 通知父组件
|
||||
onFormDataChange({
|
||||
selectedSuppliers: suppliersWithEvaluators,
|
||||
supplierIds: suppliersWithEvaluators.map(supplier => ({ id: supplier.id }))
|
||||
// 通过dispatch更新model数据
|
||||
dispatch({
|
||||
type: 'supplierTaskManage/updateFormData',
|
||||
payload: {
|
||||
selectedSuppliers: suppliersWithEvaluators,
|
||||
supplierIds: suppliersWithEvaluators.map(supplier => ({ id: supplier.id }))
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -68,6 +81,18 @@ const SupplierSelectStep = forwardRef<any, SupplierSelectStepProps>(({ formData,
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// 连接 Dva model
|
||||
const ConnectedComponent = connect(
|
||||
({ supplierTaskManage }: { supplierTaskManage: SupplierTaskModelState }) => ({
|
||||
supplierTaskManage,
|
||||
})
|
||||
)(SupplierSelectStepComponent);
|
||||
|
||||
// 外层转发 ref 到 innerRef
|
||||
const SupplierSelectStep = forwardRef((props: any, ref) => (
|
||||
<ConnectedComponent {...props} innerRef={ref} />
|
||||
));
|
||||
|
||||
export default SupplierSelectStep;
|
||||
|
Reference in New Issue
Block a user