WordBankForm.tsx
3.49 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
import { useEffect, useRef, useState, useCallback } from 'react';
import { Modal, Form, Input, Select } from 'antd';
import { wordBankService } from '../../services/wordBankService';
const { TextArea } = Input;
const CATEGORY_OPTIONS = [
{ label: '个人身份信息(PII)', value: 'PII' },
{ label: '政治敏感', value: 'politics' },
{ label: '暴力内容', value: 'violence' },
{ label: '广告推广', value: 'ad' },
{ label: '自定义', value: 'custom' },
];
interface WordItem {
id: string;
category: string;
word: string;
replacement: string;
enabled: boolean;
created_at: string;
updated_at: string;
}
interface Props {
open: boolean;
word: WordItem | null;
onClose: () => void;
}
export default function WordBankForm({ open, word, onClose }: Props) {
const [form] = Form.useForm();
const isEdit = !!word;
const [submitting, setSubmitting] = useState(false);
const [dirty, setDirty] = useState(false);
const firstFieldRef = useRef<HTMLInputElement>(null);
const isInitialMount = useRef(true);
useEffect(() => {
if (open) {
setDirty(false);
isInitialMount.current = true;
if (word) {
form.setFieldsValue(word);
} else {
form.resetFields();
form.setFieldValue('category', 'custom');
}
setTimeout(() => firstFieldRef.current?.focus(), 300);
}
}, [open, word, form]);
const onFieldsChange = useCallback(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
return;
}
if (!dirty && form.isFieldsTouched()) {
setDirty(true);
}
}, [dirty, form]);
const handleClose = useCallback(() => {
if (dirty) {
Modal.confirm({
title: '未保存的更改',
content: '当前表单有未保存的修改,确定要关闭吗?',
okText: '关闭',
cancelText: '继续编辑',
onOk: onClose,
});
} else {
onClose();
}
}, [dirty, onClose]);
const handleOk = async () => {
try {
setSubmitting(true);
const values = await form.validateFields();
if (isEdit) {
await wordBankService.update(word!.id, values);
} else {
await wordBankService.create(values);
}
onClose();
} catch {
// validation errors shown inline
} finally {
setSubmitting(false);
}
};
return (
<Modal
title={isEdit ? '编辑敏感词' : '新增敏感词'}
open={open}
onOk={handleOk}
onCancel={handleClose}
okText="保存"
cancelText="取消"
destroyOnHidden
width={520}
confirmLoading={submitting}
>
<Form
form={form}
layout="vertical"
style={{ marginTop: 8 }}
onFieldsChange={onFieldsChange}
>
<Form.Item
name="category"
label="分类"
rules={[{ required: true, message: '请选择分类' }]}
>
<Select
placeholder="选择分类"
options={CATEGORY_OPTIONS}
/>
</Form.Item>
<Form.Item
name="word"
label="敏感词"
rules={[{ required: true, message: '请输入敏感词' }]}
>
<Input placeholder="例如:身份证号、银行卡号" ref={firstFieldRef} />
</Form.Item>
<Form.Item
name="replacement"
label="替换文本"
extra="留空则默认替换为 ***"
>
<Input placeholder="例如:[已脱敏]" />
</Form.Item>
</Form>
</Modal>
);
}