forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectControl.js
More file actions
231 lines (211 loc) · 6.43 KB
/
ObjectControl.js
File metadata and controls
231 lines (211 loc) · 6.43 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
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
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { ClassNames } from '@emotion/react';
import { List, Map } from 'immutable';
import { colors, lengths, ObjectWidgetTopBar } from 'decap-cms-ui-default';
import { stringTemplate } from 'decap-cms-lib-widgets';
const styleStrings = {
nestedObjectControl: `
padding: 6px 14px 14px;
border-top: 0;
border-top-left-radius: 0;
border-top-right-radius: 0;
`,
objectWidgetTopBarContainer: `
padding: ${lengths.objectWidgetTopBarContainerPadding};
`,
collapsedObjectControl: `
display: none;
`,
};
export default class ObjectControl extends React.Component {
childRefs = {};
processControlRef = ref => {
if (!ref) return;
const name = ref.props.field.get('name');
this.childRefs[name] = ref;
this.props.controlRef?.(ref);
};
static propTypes = {
onChangeObject: PropTypes.func.isRequired,
onValidateObject: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.node, PropTypes.object, PropTypes.bool]),
field: PropTypes.object,
forID: PropTypes.string,
classNameWrapper: PropTypes.string.isRequired,
forList: PropTypes.bool,
controlRef: PropTypes.func,
editorControl: PropTypes.elementType.isRequired,
resolveWidget: PropTypes.func.isRequired,
clearFieldErrors: PropTypes.func.isRequired,
fieldsErrors: ImmutablePropTypes.map,
hasError: PropTypes.bool,
t: PropTypes.func,
locale: PropTypes.string,
collapsed: PropTypes.bool,
};
static defaultProps = {
value: Map(),
};
constructor(props) {
super(props);
this.state = {
collapsed: props.field.get('collapsed', false),
};
}
/*
* Always update so that each nested widget has the option to update. This is
* required because ControlHOC provides a default `shouldComponentUpdate`
* which only updates if the value changes, but every widget must be allowed
* to override this.
*/
shouldComponentUpdate() {
return true;
}
validate = () => {
const { field } = this.props;
let fields = field.get('field') || field.get('fields');
fields = List.isList(fields) ? fields : List([fields]);
fields.forEach(field => {
if (field.get('widget') === 'hidden') return;
const name = field.get('name');
const control = this.childRefs[name];
if (control?.innerWrappedControl?.validate) {
control.innerWrappedControl.validate();
} else {
control?.validate?.();
}
});
};
controlFor(field, key) {
const {
value,
onChangeObject,
onValidateObject,
clearFieldErrors,
metadata,
fieldsErrors,
editorControl: EditorControl,
parentIds,
isFieldDuplicate,
isFieldHidden,
locale,
collapsed,
forID,
} = this.props;
if (field.get('widget') === 'hidden') {
return null;
}
const fieldName = field.get('name');
const fieldValue = value && Map.isMap(value) ? value.get(fieldName) : value;
const isDuplicate = isFieldDuplicate && isFieldDuplicate(field);
const isHidden = isFieldHidden && isFieldHidden(field);
return (
<EditorControl
key={key}
field={field}
value={fieldValue}
onChange={onChangeObject}
clearFieldErrors={clearFieldErrors}
fieldsMetaData={metadata}
fieldsErrors={fieldsErrors}
onValidate={onValidateObject}
controlRef={this.processControlRef}
parentIds={[...parentIds, forID]}
isDisabled={isDuplicate}
isHidden={isHidden}
isFieldDuplicate={isFieldDuplicate}
isFieldHidden={isFieldHidden}
locale={locale}
isParentListCollapsed={collapsed}
/>
);
}
handleCollapseToggle = () => {
this.setState({ collapsed: !this.state.collapsed });
};
focus(path) {
if (this.state.collapsed) {
this.setState({ collapsed: false }, () => {
if (path) {
const [fieldName, ...remainingPath] = path.split('.');
const field = this.childRefs[fieldName];
if (field?.focus) {
field.focus(remainingPath.join('.'));
}
}
});
} else if (path) {
const [fieldName, ...remainingPath] = path.split('.');
const field = this.childRefs[fieldName];
if (field?.focus) {
field.focus(remainingPath.join('.'));
}
}
}
renderFields = (multiFields, singleField) => {
if (multiFields) {
return multiFields.map((f, idx) => this.controlFor(f, idx));
}
return this.controlFor(singleField);
};
objectLabel = () => {
const { value, field } = this.props;
const label = field.get('label', field.get('name'));
const summary = field.get('summary');
return summary ? stringTemplate.compileStringTemplate(summary, null, '', value) : label;
};
render() {
const { field, forID, classNameWrapper, forList, hasError, t } = this.props;
const collapsed = forList ? this.props.collapsed : this.state.collapsed;
const multiFields = field.get('fields');
const singleField = field.get('field');
if (multiFields || singleField) {
return (
<ClassNames>
{({ css, cx }) => (
<div
id={forID}
className={cx(
classNameWrapper,
css`
${styleStrings.objectWidgetTopBarContainer}
`,
{
[css`
${styleStrings.nestedObjectControl}
`]: forList,
},
{
[css`
border-color: ${colors.textFieldBorder};
`]: forList ? !hasError : false,
},
)}
>
{forList ? null : (
<ObjectWidgetTopBar
collapsed={collapsed}
onCollapseToggle={this.handleCollapseToggle}
heading={collapsed && this.objectLabel()}
t={t}
/>
)}
<div
className={cx({
[css`
${styleStrings.collapsedObjectControl}
`]: collapsed,
})}
>
{this.renderFields(multiFields, singleField)}
</div>
</div>
)}
</ClassNames>
);
}
return <h3>No field(s) defined for this widget</h3>;
}
}