ImportInfoCard.tsx
6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { useEffect, useMemo } from 'react';
import { InfoCircleFilled } from '@ant-design/icons';
import { App } from 'antd';
import { UCard, UForm, UFormItem, USelect, UInput } from '@uj/ui';
import type { USelectProps } from '@uj/ui';
import type { StoreImportCheckVO } from '@/api/modules/store';
export interface ImportInfoValues {
org?: string;
scene?: string;
categories?: string[];
remark?: string;
}
const formItemLayout = {
style: { marginBottom: 18 },
};
export interface ImportInfoCardProps {
values?: Partial<ImportInfoValues>;
/** check 接口返回结果(用于动态生成 3 个下拉的 options) */
checkResult?: StoreImportCheckVO | null;
onChange?: (values: ImportInfoValues) => void;
}
export function ImportInfoCard({ values, checkResult, onChange }: ImportInfoCardProps) {
const { message } = App.useApp();
const [form] = UForm.useForm<ImportInfoValues>();
/**
* 3 个下拉 options 完全由接口返回决定(不再硬编码):
* - orgPath / businessScene 都是单值(名字里可能带 "/",是层级/组合名,不拆分)
* - productCategories 是多值,用 "/" 分隔成多选数组
*/
const orgOptions: USelectProps['options'] = useMemo(() => {
if (!checkResult?.orgPath) return [];
return [{ label: checkResult.orgPath, value: checkResult.orgPath }];
}, [checkResult?.orgPath]);
const sceneOptions: USelectProps['options'] = useMemo(() => {
if (!checkResult?.businessScene) return [];
return [{ label: checkResult.businessScene, value: checkResult.businessScene }];
}, [checkResult?.businessScene]);
const categoryOptions: USelectProps['options'] = useMemo(() => {
if (!checkResult?.productCategories) return [];
return checkResult.productCategories
.split('/')
.map((s) => s.trim())
.filter(Boolean)
.map((v) => ({ label: v, value: v }));
}, [checkResult?.productCategories]);
// 「只读」标记:只要任一3个下拉从接口回填过,即被锁定,禁止改动
const readOnly = useMemo(
() =>
Boolean(
values &&
(values.org || values.scene || (values.categories && values.categories.length > 0)),
),
[values],
);
// 受控回填:外部 values 变化时,把导入结果同步到表单
useEffect(() => {
if (values) {
form.setFieldsValue(values);
}
}, [values, form]);
const handleValuesChange = (changed: Partial<ImportInfoValues>, all: ImportInfoValues) => {
// 只读模式下:所属组织/业务场景/产品分类一旦由接口回填确定,不允许再修改
if (readOnly) {
const lockedKeys: (keyof ImportInfoValues)[] = ['org', 'scene', 'categories'];
const touched = lockedKeys.filter((k) => Object.prototype.hasOwnProperty.call(changed, k));
if (touched.length > 0) {
message.warning('该字段由模板解析结果自动回填,不可手动修改');
// 立刻恢复原值(只读语义:展示原值恢复不改)
form.setFieldsValue({
org: values?.org ?? all.org,
scene: values?.scene ?? all.scene,
categories: values?.categories ?? all.categories,
});
// 回退 touched keys 恢复后,不向上游广播 change(只传 remark 的变化允许)
const next: ImportInfoValues = { ...all };
if (values?.org != null) next.org = values.org;
if (values?.scene != null) next.scene = values.scene;
if (values?.categories != null) next.categories = values.categories;
onChange?.(next);
return;
}
}
onChange?.(all);
};
return (
<UCard
styles={{ body: { padding: 20 } }}
style={{ marginBottom: 20, border: '1px solid #E5E7EB' }}
title={
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<InfoCircleFilled style={{ color: '#2B5CE6', fontSize: 14 }} />
<span style={{ fontSize: 15, fontWeight: 600 }}>导入信息</span>
</div>
}
>
<UForm
form={form}
layout="vertical"
onValuesChange={handleValuesChange}
onFinish={() => {
onChange?.(form.getFieldsValue());
}}
>
<div
style={{
display: 'grid',
gridTemplateColumns: '1fr 1fr 1fr',
gap: 20,
}}
>
<UFormItem
label={
<span>
所属组织<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
</span>
}
name="org"
{...formItemLayout}
>
<USelect options={orgOptions} placeholder="请选择所属组织" />
</UFormItem>
<UFormItem
label={
<span>
业务场景<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
</span>
}
name="scene"
{...formItemLayout}
>
<USelect options={sceneOptions} placeholder="请选择业务场景" />
</UFormItem>
<UFormItem
label={
<span>
产品分类<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
</span>
}
name="categories"
{...formItemLayout}
>
<USelect
mode="multiple"
options={categoryOptions}
placeholder="请选择产品分类"
maxTagCount="responsive"
/>
</UFormItem>
</div>
<div
style={{
display: 'grid',
gridTemplateColumns: '1fr 1fr 1fr',
gap: 20,
marginTop: 4,
}}
>
<div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
决定本次导入数据的数据可见范围
</div>
<div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
同一门店有多个业务场景时需拆分为独立行导入
</div>
<div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
决定模板中「产品分类」列的枚举范围
</div>
</div>
<UFormItem
label={<span style={{ marginTop: 18, display: 'inline-block' }}>备注</span>}
name="remark"
style={{ marginBottom: 8 }}
>
<UInput.TextArea
rows={2}
maxLength={200}
showCount
placeholder="选填,≤ 200 字,将在审批详情中展示给审批人"
/>
</UFormItem>
</UForm>
</UCard>
);
}