Skip to content

Commit 21a8504

Browse files
authored
🚀 add external labels for datasource (#69)
1 parent 04990cf commit 21a8504

File tree

1 file changed

+108
-10
lines changed

1 file changed

+108
-10
lines changed

src/pages/datasources/DatasourceCreateModal.jsx

Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createDatasource, DatasourcePing, updateDatasource} from '../../api/datasource'
2-
import {Modal, Form, Input, Button, Switch, Select, InputNumber, Alert, Drawer} from 'antd'
2+
import {Modal, Form, Input, Button, Switch, Select, InputNumber, Alert, Drawer, Space} from 'antd'
33
import React, { useState, useEffect } from 'react'
4+
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
45
const { TextArea } = Input
56
const MyFormItemContext = React.createContext([])
67

@@ -46,10 +47,16 @@ export const CreateDatasourceModal = ({ visible, onClose, selectedRow, type, han
4647

4748
useEffect(() => {
4849
if (selectedRow) {
50+
const labelsArray = Object.entries(selectedRow.labels || {}).map(([key, value]) => ({
51+
key,
52+
value,
53+
}));
54+
4955
setSelectedType(selectedRow.type)
5056
form.setFieldsValue({
5157
name: selectedRow.name,
5258
type: selectedRow.type,
59+
labels: labelsArray,
5360
http: {
5461
url: selectedRow.http.url,
5562
timeout: selectedRow.http.timeout
@@ -89,14 +96,26 @@ export const CreateDatasourceModal = ({ visible, onClose, selectedRow, type, han
8996
}
9097

9198
const handleFormSubmit = async (values) => {
99+
// 将 labels 数组转换为对象格式
100+
const formattedLabels = values.labels?.reduce((acc, { key, value }) => {
101+
if (key) {
102+
acc[key] = value;
103+
}
104+
return acc;
105+
}, {});
92106

93107
if (type === 'create') {
94-
await handleCreate(values)
108+
const params = {
109+
...values,
110+
labels: formattedLabels,
111+
}
112+
await handleCreate(params)
95113
}
96114

97115
if (type === 'update') {
98116
const params = {
99117
...values,
118+
labels: formattedLabels,
100119
id: selectedRow.id
101120
}
102121
await handleUpdate(params)
@@ -118,34 +137,113 @@ export const CreateDatasourceModal = ({ visible, onClose, selectedRow, type, han
118137
const handleTestConnection = async () => {
119138
// 获取表单数据
120139
const values = await form.validateFields().catch((err) => null);
140+
const formattedLabels = values.labels?.reduce((acc, { key, value }) => {
141+
if (key) {
142+
acc[key] = value;
143+
}
144+
return acc;
145+
}, {});
121146
if (!values) {
122147
// 如果表单验证失败,直接返回
123148
return;
124149
}
125150

126151
try {
127-
await DatasourcePing(values)
152+
const params = {
153+
...values,
154+
labels: formattedLabels
155+
}
156+
await DatasourcePing(params)
128157
} catch (error) {
129158
console.error('连接测试失败:', error);
130159
}
131160
};
132161

133162
return (
134-
<Drawer title="创建数据源" open={visible} onClose={onClose}>
163+
<Drawer title="创建数据源" open={visible} onClose={onClose} size='large'>
135164
<Form form={form} name="form_item_path" layout="vertical" onFinish={handleFormSubmit}>
136-
<MyFormItem name="name" label="数据源名称"
137-
rules={[
138-
{
139-
required: true,
140-
},
141-
]}>
165+
<MyFormItem
166+
name="name"
167+
label="数据源名称"
168+
rules={[{required: true}]}>
142169
<Input
143170
value={spaceValue}
144171
onChange={handleInputChange}
145172
onKeyPress={handleKeyPress}
146173
disabled={type === 'update'}/>
147174
</MyFormItem>
148175

176+
<label>标签</label>
177+
<Alert
178+
message="提示:可添加外部标签(external labels), 它将会添加到事件当中用于区分数据来源。"
179+
type="info"
180+
showIcon
181+
style={{ marginBottom: 20, marginTop:'10px' }}
182+
/>
183+
184+
<Form.List name="labels">
185+
{(fields, { add, remove }) => (
186+
<>
187+
{fields.map(({ key, name, ...restField }) => (
188+
<div
189+
key={key}
190+
style={{
191+
display: 'flex',
192+
marginBottom: 8,
193+
gap: '8px', // 控制组件之间的间距
194+
alignItems: 'center', // 垂直居中
195+
}}
196+
>
197+
{/* 键(key)输入框 */}
198+
<Form.Item
199+
{...restField}
200+
name={[name, 'key']}
201+
style={{ flex: 3 }} // 占比 3
202+
rules={[{ required: true, message: '请输入标签键(key)' }]}
203+
>
204+
<Input placeholder="键(key)" />
205+
</Form.Item>
206+
207+
{/* 值(value)输入框 */}
208+
<Form.Item
209+
{...restField}
210+
name={[name, 'value']}
211+
style={{ flex: 3 }} // 占比 3
212+
rules={[{ required: true, message: '请输入标签值(value)' }]}
213+
>
214+
<Input placeholder="值(value)" />
215+
</Form.Item>
216+
217+
{/* 删除按钮 */}
218+
<MinusCircleOutlined
219+
style={{
220+
// flex: 1, // 占比 1
221+
marginTop: '-25px',
222+
display: 'flex',
223+
justifyContent: 'center',
224+
alignItems: 'center',
225+
cursor: 'pointer',
226+
}}
227+
onClick={() => remove(name)}
228+
/>
229+
</div>
230+
))}
231+
232+
<Form.Item>
233+
<Button
234+
type="dashed"
235+
onClick={() => add()}
236+
block
237+
icon={<PlusOutlined />}
238+
disabled={fields.length >= 10}
239+
>
240+
添加标签
241+
</Button>
242+
</Form.Item>
243+
</>
244+
)}
245+
</Form.List>
246+
149247
<MyFormItem name="type" label="数据源类型"
150248
rules={[
151249
{

0 commit comments

Comments
 (0)