/
EditField.tsx
163 lines (140 loc) · 4.91 KB
/
EditField.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
import { Button, Col, Divider, Form, Input, Row, Select } from 'antd';
import { FormComponentProps } from 'antd/lib/form';
import { camelCase, defaultsDeep, get } from 'lodash';
import { toJS } from 'mobx';
import React, { useState } from 'react';
import stores from '../../../stores';
import { fields } from '../../../utils/fields';
interface IProps extends FormComponentProps {
field: any;
availableFields: any[];
schema: any;
onCancel(): void;
onSubmit(data: any): void;
}
const EditFieldBase = ({ form, onCancel, onSubmit, field, schema, availableFields }: IProps) => {
const { getFieldDecorator } = form;
const [autoName, setAutoName] = useState(form.getFieldValue('name') === '');
const type = form.getFieldValue('type');
const theField = get(fields, type);
const SchemaSettingsComponent = get(theField, 'SchemaSettingsComponent', () => null);
const fromAvailableField = availableFields.find(f => f.type === type);
const options = defaultsDeep(
{},
field ? toJS(field.options) : {},
fromAvailableField ? toJS(fromAvailableField.defaultOptions) : {}
);
const onFormSubmit = async (e: any) => {
e.preventDefault();
form.validateFieldsAndScroll(async (error, values) => {
if (!error) {
const result = {
...field,
...values,
};
if (SchemaSettingsComponent && SchemaSettingsComponent.BEFORE_SUBMIT) {
await SchemaSettingsComponent.BEFORE_SUBMIT(result.options);
}
await onSubmit(result);
}
});
return false;
};
const onNameKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
setAutoName(e.currentTarget.value === '');
};
const onTitleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (!autoName) {
return;
}
const name = camelCase(form.getFieldValue('title'));
let proposedName = name;
let i = 1;
while (schema.fields.find((f: any) => f !== field && f.name === proposedName)) {
proposedName = `${name}${i}`;
i += 1;
}
form.setFieldsValue({
name: proposedName,
});
};
const ensureUniqueName = (rule: any, value: any, callback: (input?: string) => void) => {
if (value === 'id') {
return callback('Forbidden name: id');
}
if (value === '_meta') {
return callback('Forbidden name: _meta');
}
const hasMatch = schema.fields.find((f: any) => f !== field && f.name === value);
callback(hasMatch ? 'Field with that name already exists' : undefined);
};
return (
<>
<Form layout="vertical" hideRequiredMark onSubmit={onFormSubmit}>
<Form.Item style={{ marginBottom: 8 }}>
{getFieldDecorator('title', {
rules: [{ required: true }],
})(
<Input
addonBefore="Title*"
onKeyUp={onTitleKeyUp}
placeholder="Please enter title"
autoFocus
/>
)}
</Form.Item>
<Form.Item style={{ marginBottom: 8 }}>
{getFieldDecorator('name', {
rules: [
{
required: true,
},
{
pattern: /^[A-Za-z][A-Za-z0-9_]+$/,
message: 'Must be alphanumeric and start with non-number',
},
{
validator: ensureUniqueName,
},
],
})(
<Input addonBefore="API*" onKeyUp={onNameKeyUp} placeholder="Please enter api name" />
)}
</Form.Item>
<Form.Item style={{ marginBottom: 8 }}>
{getFieldDecorator('description', {
rules: [{ max: 80 }],
})(<Input addonBefore="Description" placeholder="(optional)" />)}
</Form.Item>
<Divider dashed />
<SchemaSettingsComponent field={field} form={form} options={options} stores={stores} />
<div className="prime__drawer__bottom">
<Button style={{ marginRight: 8 }} onClick={onCancel}>
Cancel
</Button>
<Button onClick={onFormSubmit} type="primary" htmlType="submit">
Save
</Button>
</div>
</Form>
</>
);
};
export const EditField = Form.create({
mapPropsToFields(props: any) {
const { field } = props;
const res: any = {
name: Form.createFormField({ value: field.name }),
title: Form.createFormField({ value: field.title }),
description: Form.createFormField({ value: field.description }),
type: Form.createFormField({ value: field.type }),
};
const fromAvailableField = props.availableFields.find((f: any) => f.type === field.type);
const options = defaultsDeep(get(field, 'options', {}), get(fromAvailableField, 'options', {}));
Object.entries(options).forEach(([key, value]) => {
res[`options.${key}`] = Form.createFormField({ value });
});
res.optionsJson = Form.createFormField({ value: JSON.stringify(options) });
return res;
},
})(EditFieldBase);