-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Form.tsx
270 lines (235 loc) · 6.9 KB
/
Form.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { FormTop, Title } from '../styles';
import { IForm, IFormData } from '../types';
import ControlLabel from '@erxes/ui/src/components/form/Label';
import { Description } from '@erxes/ui-settings/src/styles';
import FieldChoices from './FieldChoices';
import FieldForm from '../containers/FieldForm';
import FieldsPreview from './FieldsPreview';
import { FlexContent } from '@erxes/ui/src/layout/styles';
import FormControl from '@erxes/ui/src/components/form/Control';
import FormGroup from '@erxes/ui/src/components/form/Group';
import { IField } from '@erxes/ui/src/types';
import { LeftItem } from '@erxes/ui/src/components/step/styles';
import React from 'react';
import { __ } from '@erxes/ui/src/utils';
type Props = {
fields: IField[];
renderPreviewWrapper?: (previewRenderer, fields: IField[]) => void;
onDocChange?: (doc: IFormData) => void;
saveForm: (params: IFormData) => void;
formData?: IFormData;
isReadyToSave: boolean;
type: string;
form?: IForm;
hideOptionalFields?: boolean;
currentMode?: 'create' | 'update' | undefined;
currentField?: IField;
color?: string;
};
type State = {
fields: IField[];
currentMode: 'create' | 'update' | undefined;
currentField?: IField;
title: string;
description: string;
type?: string;
buttonText: string;
numberOfPages?: number;
currentPage: number;
};
class Form extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
const { form = {} as IForm } = props;
this.state = {
fields: (props.formData ? props.formData.fields : props.fields) || [],
title: form.title || 'Form Title',
description: form.description || '',
buttonText: form.buttonText || 'Send',
currentMode: undefined,
currentField: undefined,
type: props.type || '',
numberOfPages: form.numberOfPages || 1,
currentPage: 1
};
}
componentWillReceiveProps(nextProps: Props) {
const { saveForm, type, isReadyToSave, formData } = this.props;
const { title, buttonText, description, fields } = this.state;
if (nextProps.formData && nextProps.formData !== formData) {
this.setState({
fields: nextProps.formData.fields || []
});
}
if (nextProps.isReadyToSave && isReadyToSave !== nextProps.isReadyToSave) {
saveForm(
nextProps.formData
? { ...nextProps.formData }
: {
title,
description,
buttonText,
fields,
type
}
);
}
}
renderOptionalFields = () => {
if (this.props.hideOptionalFields) {
return null;
}
const { onDocChange } = this.props;
const { title, buttonText, description, numberOfPages } = this.state;
const onChangeField = e => {
const name: keyof State = e.target.name;
const value = (e.currentTarget as HTMLInputElement).value;
this.setState({ [name]: value } as any, () => {
if (onDocChange) {
onDocChange(this.state);
}
});
};
return (
<>
<FormGroup>
<ControlLabel required={true}>{__('Form title')}</ControlLabel>
<FormControl
required={true}
name="title"
value={title}
onChange={onChangeField}
/>
</FormGroup>
<FormGroup>
<ControlLabel>{__('Form description')}</ControlLabel>
<FormControl
componentClass="textarea"
name="description"
value={description}
onChange={onChangeField}
/>
</FormGroup>
<FormGroup>
<ControlLabel>{__('Number of pages')}</ControlLabel>
<FormControl
name="numberOfPages"
value={numberOfPages}
onChange={onChangeField}
type={'number'}
min={1}
/>
</FormGroup>
<FormGroup>
<ControlLabel>{__('Form button text')}</ControlLabel>
<FormControl
name="buttonText"
value={buttonText}
onChange={onChangeField}
/>
</FormGroup>
</>
);
};
onChoiceClick = (choice: string) => {
this.setState({
currentMode: 'create',
currentField: {
_id: `tempId${Math.random().toString()}`,
contentType: 'form',
type: choice
}
});
};
onFieldClick = (field: IField) => {
this.setState({ currentMode: 'update', currentField: field });
};
onFieldSubmit = (field: IField) => {
const { onDocChange } = this.props;
const { fields, currentMode } = this.state;
let selector = { fields, currentField: undefined };
if (currentMode === 'create') {
selector = {
fields: [...fields, field],
currentField: undefined
};
}
this.setState(selector, () => {
if (onDocChange) {
onDocChange(this.state);
}
});
};
onFieldDelete = (field: IField) => {
// remove field from state
const fields = this.state.fields.filter(f => f._id !== field._id);
this.setState({ fields, currentField: undefined });
};
onFieldFormCancel = () => {
this.setState({ currentField: undefined });
};
onChangeFieldsOrder = fields => {
const { onDocChange } = this.props;
const allFields = this.state.fields;
for (const field of fields) {
const index = allFields.map(e => e._id).indexOf(field._id);
if (index !== -1) {
allFields[index] = field;
}
}
this.setState({ fields: allFields }, () => {
if (onDocChange) {
onDocChange(this.state);
}
});
};
render() {
const { renderPreviewWrapper } = this.props;
const {
currentMode,
currentField,
fields,
description,
numberOfPages
} = this.state;
const renderer = () => {
return (
<FieldsPreview
formDesc={description}
fields={fields}
onFieldClick={this.onFieldClick}
onChangeFieldsOrder={this.onChangeFieldsOrder}
currentPage={this.state.currentPage}
/>
);
};
return (
<FlexContent>
<LeftItem>
<FormTop>{this.renderOptionalFields()}</FormTop>
<Title>{__('Add a new field')}</Title>
<Description>
{__('Choose a field type from the options below.')}
</Description>
<FieldChoices
type={this.props.type}
onChoiceClick={this.onChoiceClick}
/>
</LeftItem>
{currentField && (
<FieldForm
mode={currentMode || 'create'}
field={currentField}
fields={fields}
numberOfPages={numberOfPages || 1}
onSubmit={this.onFieldSubmit}
onDelete={this.onFieldDelete}
onCancel={this.onFieldFormCancel}
/>
)}
{renderPreviewWrapper && renderPreviewWrapper(renderer, fields)}
</FlexContent>
);
}
}
export default Form;