-
Notifications
You must be signed in to change notification settings - Fork 90
/
global.tsx
175 lines (163 loc) · 4.5 KB
/
global.tsx
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
/* istanbul ignore file */
import React from 'react';
import { Icon, registerFieldPlugin, Field, request } from './packages/sula/src/index';
import {
TabletFilled,
AppstoreOutlined,
CarOutlined,
CoffeeOutlined,
PlusOutlined,
MinusCircleOutlined,
} from '@ant-design/icons';
import { Button, Space, Select } from 'antd';
// umi-plugin-sula 承载
// registerFieldPlugins();
// registerRenderPlugins();
// registerActionPlugin();
Icon.iconRegister({
tablet: {
filled: TabletFilled,
},
appstore: {
outlined: AppstoreOutlined,
},
car: {
outlined: CarOutlined,
},
coffee: CoffeeOutlined,
});
const DynamicFieldComp = (props) => {
const { list } = props;
const [fields, { add, remove }] = list;
return (
<>
{fields.map((field) => {
const { name, key, fieldKey, ...rest } = field;
return (
<Space key={key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
{props.fields.map((fieldConfig) => {
return (
<Field
{...rest}
key={key}
name={[name, fieldConfig.name]}
fieldKey={[fieldKey, fieldConfig.name]}
rules={fieldConfig.rules}
field={fieldConfig.field}
/>
);
})}
<MinusCircleOutlined onClick={() => remove(field.name)} key="remove" />
</Space>
);
})}
<Button
style={{ width: 300 }}
type="dashed"
onClick={() => add()}
block
icon={<PlusOutlined />}
>
添加
</Button>
</>
);
};
registerFieldPlugin('dynamicfieldcomp')(DynamicFieldComp);
const DynamicDepFieldComp = (props) => {
const { list } = props;
const [fields, { add, remove }] = list;
return (
<>
{fields.map((field) => {
const { name, key, fieldKey, ...rest } = field;
return (
<Space key={key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
{props.fields.map((fieldConfig) => {
const { dependency = {} } = fieldConfig;
const transedDep = Object.keys(dependency).reduce((memo, depType) => {
const dep = dependency[depType];
const relates = dep.relates.map((r) => {
return [fieldKey, ...(Array.isArray(r) ? r : [r])];
});
memo[depType] = {
...dep,
relates,
};
return memo;
}, {});
return (
<Field
{...rest}
key={key}
name={[name, fieldConfig.name]}
fieldKey={[fieldKey, fieldConfig.name]}
rules={fieldConfig.rules}
field={fieldConfig.field}
remoteSource={fieldConfig.remoteSource}
initialSource={fieldConfig.initialSource}
initialValue={fieldConfig.initialValue}
dependency={transedDep}
/>
);
})}
<MinusCircleOutlined onClick={() => remove(field.name)} key="remove" />
</Space>
);
})}
<Button
style={{ width: 300 }}
type="dashed"
onClick={() => add()}
block
icon={<PlusOutlined />}
>
添加
</Button>
</>
);
};
registerFieldPlugin('dynamicdepfieldcomp')(DynamicDepFieldComp);
const RemoteSearch = (props) => {
const { source = [], ctx, value, onChange, placeholder } = props;
const handleSearch = (q) => {
if(!q) {
return;
}
request({
url: 'https://jsonplaceholder.typicode.com/todos/1',
params: {
q,
},
}).then(() => {
ctx.form.setFieldSource(ctx.name, Array(10).fill(0).map((_, index) => {
return {
text: `商品_${q}_${index}`,
value: `价格_${q}_${index}`,
}
}));
});
};
return (
<Select
showSearch
placeholder={placeholder}
defaultActiveFirstOption={false}
showArrow={false}
filterOption={false}
notFoundContent={null}
value={value}
onChange={onChange}
onSearch={handleSearch}
>
{source.map((item) => {
return (
<Select.Option key={item.value} value={item.value}>
{item.text}
</Select.Option>
);
})}
</Select>
);
};
registerFieldPlugin('customremotesearch')(RemoteSearch, true, true);