@panneau/core 1.0.0-alpha.19 → 1.0.0-alpha.193
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/contexts.js +247 -198
- package/es/hooks.js +31 -14
- package/es/index.js +29 -7
- package/es/utils.js +75 -69
- package/lib/contexts.js +250 -201
- package/lib/hooks.js +32 -13
- package/lib/index.js +30 -7
- package/lib/utils.js +81 -72
- package/package.json +4 -3
package/es/hooks.js
CHANGED
|
@@ -11,7 +11,9 @@ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
|
11
11
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
12
12
|
import { getCsrfToken, postJSON, getCSRFHeaders } from '@folklore/fetch';
|
|
13
13
|
import isObject from 'lodash/isObject';
|
|
14
|
-
import
|
|
14
|
+
import isArray from 'lodash/isArray';
|
|
15
|
+
import get from 'lodash/get';
|
|
16
|
+
import { useFieldsManager, usePanneauResources, useResource, useUrlGenerator } from '@panneau/core/contexts';
|
|
15
17
|
import { match } from 'css-mediaquery';
|
|
16
18
|
|
|
17
19
|
var useAnimationFrame = function useAnimationFrame(onFrame) {
|
|
@@ -114,6 +116,7 @@ var getFieldsPropsFromFields = function getFieldsPropsFromFields(fields, _ref) {
|
|
|
114
116
|
_onChange = _ref.onChange,
|
|
115
117
|
props = _objectWithoutProperties(_ref, ["value", "errors", "onChange"]);
|
|
116
118
|
|
|
119
|
+
var locales = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
117
120
|
return fields.reduce(function (allFields, field) {
|
|
118
121
|
var _ref2 = isObject(field) ? field : {},
|
|
119
122
|
_ref2$name = _ref2.name,
|
|
@@ -121,12 +124,18 @@ var getFieldsPropsFromFields = function getFieldsPropsFromFields(fields, _ref) {
|
|
|
121
124
|
_ref2$component = _ref2.component,
|
|
122
125
|
component = _ref2$component === void 0 ? null : _ref2$component;
|
|
123
126
|
|
|
124
|
-
|
|
127
|
+
var fieldErrors = errors !== null ? errors[name] || [] : [];
|
|
128
|
+
var localizedErrors = component === 'localized' ? (locales || []).reduce(function (previousErrors, locale) {
|
|
129
|
+
var items = errors !== null ? get(errors, "".concat(name, ".").concat(locale), []) || [] : [];
|
|
130
|
+
var finalItems = isArray(items) ? items : [items];
|
|
131
|
+
var finalPrevious = isArray(previousErrors) ? previousErrors : [];
|
|
132
|
+
return [].concat(_toConsumableArray(finalPrevious), _toConsumableArray(finalItems));
|
|
133
|
+
}, fieldErrors) : fieldErrors;
|
|
125
134
|
return [].concat(_toConsumableArray(allFields), [_objectSpread(_objectSpread({}, isObject(field) ? field : null), {}, {
|
|
126
135
|
name: name,
|
|
127
136
|
component: component,
|
|
128
137
|
value: value !== null ? value[name] || null : null,
|
|
129
|
-
errors:
|
|
138
|
+
errors: localizedErrors.length > 0 ? localizedErrors : null,
|
|
130
139
|
onChange: function onChange(fieldValue) {
|
|
131
140
|
return _onChange(name, fieldValue);
|
|
132
141
|
}
|
|
@@ -161,7 +170,9 @@ var useForm = function useForm() {
|
|
|
161
170
|
_opts$setValue = opts.setValue,
|
|
162
171
|
setProvidedValue = _opts$setValue === void 0 ? null : _opts$setValue,
|
|
163
172
|
_opts$onComplete = opts.onComplete,
|
|
164
|
-
onComplete = _opts$onComplete === void 0 ? null : _opts$onComplete
|
|
173
|
+
onComplete = _opts$onComplete === void 0 ? null : _opts$onComplete,
|
|
174
|
+
_opts$locales = opts.locales,
|
|
175
|
+
locales = _opts$locales === void 0 ? [] : _opts$locales;
|
|
165
176
|
|
|
166
177
|
var _useState = useState(initialValue || providedValue),
|
|
167
178
|
_useState2 = _slicedToArray(_useState, 2),
|
|
@@ -201,7 +212,7 @@ var useForm = function useForm() {
|
|
|
201
212
|
var hasProvidedGeneralError = setProvidedGeneralError !== null;
|
|
202
213
|
var generalError = hasProvidedGeneralError ? providedGeneralError : stateGeneralError;
|
|
203
214
|
var setGeneralError = hasProvidedGeneralError ? setProvidedGeneralError : setStateGeneralError;
|
|
204
|
-
var fieldsKey = [value, errors].concat(fields);
|
|
215
|
+
var fieldsKey = [value, errors, locales].concat(fields);
|
|
205
216
|
var onFieldChange = useCallback(function (fieldName, fieldValue) {
|
|
206
217
|
var fieldErrors = errors !== null ? errors[fieldName] || null : null;
|
|
207
218
|
|
|
@@ -216,7 +227,7 @@ var useForm = function useForm() {
|
|
|
216
227
|
value: value,
|
|
217
228
|
errors: errors,
|
|
218
229
|
onChange: onFieldChange
|
|
219
|
-
});
|
|
230
|
+
}, locales);
|
|
220
231
|
}, fieldsKey);
|
|
221
232
|
var csrfToken = useMemo(function () {
|
|
222
233
|
return getCsrfToken();
|
|
@@ -951,21 +962,27 @@ var getResource = function getResource(resources, resourceId) {
|
|
|
951
962
|
var useResourceUrlGenerator = function useResourceUrlGenerator() {
|
|
952
963
|
var resourceId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
953
964
|
var resources = usePanneauResources();
|
|
954
|
-
var
|
|
965
|
+
var contextResource = useResource();
|
|
966
|
+
var resource = getResource(resources, resourceId) || contextResource;
|
|
955
967
|
var route = useUrlGenerator();
|
|
956
968
|
return function (routeResourceId) {
|
|
957
969
|
var routeName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
958
970
|
var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
959
971
|
var finalRouteName = resourceId !== null ? routeResourceId : routeName;
|
|
960
972
|
var finalParams = resourceId !== null ? routeName : params;
|
|
961
|
-
var finalResource =
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
973
|
+
var finalResource = getResource(resources, routeResourceId) || resource;
|
|
974
|
+
|
|
975
|
+
var _ref = finalResource || {},
|
|
976
|
+
_ref$id = _ref.id,
|
|
977
|
+
id = _ref$id === void 0 ? null : _ref$id; // console.log('gen', finalRouteName, route(`resources.${finalRouteName}`, {
|
|
978
|
+
// ...finalParams,
|
|
979
|
+
// resource: id,
|
|
980
|
+
// }));
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
return id !== null ? route("resources.".concat(finalRouteName), _objectSpread(_objectSpread({}, finalParams), {}, {
|
|
967
984
|
resource: id
|
|
968
|
-
}));
|
|
985
|
+
})) : null;
|
|
969
986
|
};
|
|
970
987
|
};
|
|
971
988
|
|
package/es/index.js
CHANGED
|
@@ -11,10 +11,12 @@ import 'snake-case';
|
|
|
11
11
|
import isObject from 'lodash/isObject';
|
|
12
12
|
import 'react';
|
|
13
13
|
import { pascalCase } from 'change-case';
|
|
14
|
-
import '
|
|
14
|
+
import ReactIs from 'react-is';
|
|
15
15
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
16
|
+
import 'lodash/isString';
|
|
16
17
|
import isArray from 'lodash/isArray';
|
|
17
18
|
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
19
|
+
import 'lodash/capitalize';
|
|
18
20
|
import 'slugify';
|
|
19
21
|
import uniqBy from 'lodash/uniqBy';
|
|
20
22
|
import uniq from 'lodash/uniq';
|
|
@@ -71,6 +73,7 @@ var routes = PropTypes$1.shape({
|
|
|
71
73
|
'resources.show': PropTypes$1.string.isRequired,
|
|
72
74
|
'resources.edit': PropTypes$1.string.isRequired,
|
|
73
75
|
'resources.update': PropTypes$1.string.isRequired,
|
|
76
|
+
'resources.delete': PropTypes$1.string.isRequired,
|
|
74
77
|
'resources.destroy': PropTypes$1.string.isRequired
|
|
75
78
|
});
|
|
76
79
|
var intl = PropTypes$1.shape({
|
|
@@ -85,8 +88,9 @@ var pages = PropTypes$1.objectOf(page);
|
|
|
85
88
|
var field = PropTypes$1.shape({
|
|
86
89
|
// type: PropTypes.string.isRequired,
|
|
87
90
|
name: PropTypes$1.isRequired,
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
type: PropTypes$1.string,
|
|
92
|
+
component: PropTypes$1.string,
|
|
93
|
+
label: PropTypes$1.node
|
|
90
94
|
});
|
|
91
95
|
var fields = PropTypes$1.arrayOf(field);
|
|
92
96
|
var form = PropTypes$1.shape({
|
|
@@ -97,11 +101,10 @@ var form = PropTypes$1.shape({
|
|
|
97
101
|
var forms = PropTypes$1.arrayOf(form);
|
|
98
102
|
var resource = PropTypes$1.shape({
|
|
99
103
|
id: PropTypes$1.string.isRequired,
|
|
100
|
-
|
|
104
|
+
name: PropTypes$1.string.isRequired,
|
|
101
105
|
intl: intl,
|
|
102
106
|
fields: fields,
|
|
103
107
|
forms: PropTypes$1.shape({}),
|
|
104
|
-
has_routes: PropTypes$1.bool,
|
|
105
108
|
shows_in_navbar: PropTypes$1.bool
|
|
106
109
|
});
|
|
107
110
|
var resources = PropTypes$1.arrayOf(resource);
|
|
@@ -138,6 +141,11 @@ var formError = PropTypes$1.shape({
|
|
|
138
141
|
message: PropTypes$1.string
|
|
139
142
|
});
|
|
140
143
|
var formErrors = PropTypes$1.arrayOf(formError);
|
|
144
|
+
var uppy = PropTypes$1.shape({
|
|
145
|
+
tus: PropTypes$1.object
|
|
146
|
+
}); // eslint-disable-line
|
|
147
|
+
|
|
148
|
+
var triggerUpdate = PropTypes$1.string;
|
|
141
149
|
|
|
142
150
|
var PropTypes = /*#__PURE__*/Object.freeze({
|
|
143
151
|
__proto__: null,
|
|
@@ -178,9 +186,23 @@ var PropTypes = /*#__PURE__*/Object.freeze({
|
|
|
178
186
|
selectOptions: selectOptions,
|
|
179
187
|
feedback: feedback,
|
|
180
188
|
formError: formError,
|
|
181
|
-
formErrors: formErrors
|
|
189
|
+
formErrors: formErrors,
|
|
190
|
+
uppy: uppy,
|
|
191
|
+
triggerUpdate: triggerUpdate
|
|
182
192
|
});
|
|
183
193
|
|
|
194
|
+
var flattenComponents = function flattenComponents(components) {
|
|
195
|
+
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
196
|
+
|
|
197
|
+
if (components === null) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return Object.keys(components).reduce(function (newMap, key) {
|
|
202
|
+
return ReactIs.isValidElementType(components[key]) ? _objectSpread(_objectSpread({}, newMap), {}, _defineProperty({}, prefix !== null ? "".concat(prefix, ".").concat(key) : key, components[key])) : _objectSpread(_objectSpread({}, newMap), flattenComponents(components[key], key));
|
|
203
|
+
}, {});
|
|
204
|
+
};
|
|
205
|
+
|
|
184
206
|
var getComponentFromName = function getComponentFromName() {
|
|
185
207
|
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
186
208
|
var components = arguments.length > 1 ? arguments[1] : undefined;
|
|
@@ -213,7 +235,7 @@ var ComponentsManager = /*#__PURE__*/function (_EventEmitter) {
|
|
|
213
235
|
_classCallCheck(this, ComponentsManager);
|
|
214
236
|
|
|
215
237
|
_this = _super.call(this);
|
|
216
|
-
_this.components = components;
|
|
238
|
+
_this.components = flattenComponents(components);
|
|
217
239
|
return _this;
|
|
218
240
|
}
|
|
219
241
|
|
package/es/utils.js
CHANGED
|
@@ -4,12 +4,14 @@ import isObject from 'lodash/isObject';
|
|
|
4
4
|
import { useEffect } from 'react';
|
|
5
5
|
import { pascalCase, paramCase, snakeCase as snakeCase$1 } from 'change-case';
|
|
6
6
|
export { paramCase as dashCase, pascalCase, snakeCase } from 'change-case';
|
|
7
|
-
import
|
|
7
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
8
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
9
|
+
import ReactIs from 'react-is';
|
|
8
10
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
11
|
+
import isString from 'lodash/isString';
|
|
9
12
|
import isArray from 'lodash/isArray';
|
|
10
13
|
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
11
|
-
import
|
|
12
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
14
|
+
import capitalize from 'lodash/capitalize';
|
|
13
15
|
import slugify from 'slugify';
|
|
14
16
|
|
|
15
17
|
var convertStyleToString = function convertStyleToString(style) {
|
|
@@ -56,6 +58,31 @@ var createUseEvent = function createUseEvent(eventsManager) {
|
|
|
56
58
|
};
|
|
57
59
|
};
|
|
58
60
|
|
|
61
|
+
var flattenComponents = function flattenComponents(components) {
|
|
62
|
+
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
63
|
+
|
|
64
|
+
if (components === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return Object.keys(components).reduce(function (newMap, key) {
|
|
69
|
+
return ReactIs.isValidElementType(components[key]) ? _objectSpread(_objectSpread({}, newMap), {}, _defineProperty({}, prefix !== null ? "".concat(prefix, ".").concat(key) : key, components[key])) : _objectSpread(_objectSpread({}, newMap), flattenComponents(components[key], key));
|
|
70
|
+
}, {});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
var getComponent = function getComponent(component) {
|
|
74
|
+
var _ref = isObject(component) ? component : {
|
|
75
|
+
component: component
|
|
76
|
+
};
|
|
77
|
+
_ref.component;
|
|
78
|
+
var props = _objectWithoutProperties(_ref, ["component"]);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
name: component,
|
|
82
|
+
props: props
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
59
86
|
var getColumnFromField = function getColumnFromField(field) {
|
|
60
87
|
if (field === null) {
|
|
61
88
|
return null;
|
|
@@ -63,82 +90,43 @@ var getColumnFromField = function getColumnFromField(field) {
|
|
|
63
90
|
|
|
64
91
|
|
|
65
92
|
var name = field.name,
|
|
66
|
-
_field$components = field.components
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
var order = _field$settings.orderInIndex,
|
|
93
|
+
_field$components = field.components;
|
|
94
|
+
_field$components = _field$components === void 0 ? {} : _field$components;
|
|
95
|
+
var _field$components$dis = _field$components.display,
|
|
96
|
+
display = _field$components$dis === void 0 ? null : _field$components$dis,
|
|
71
97
|
label = field.label;
|
|
72
|
-
|
|
98
|
+
|
|
99
|
+
var _getComponent = getComponent(display),
|
|
100
|
+
_getComponent$name = _getComponent.name,
|
|
101
|
+
componentName = _getComponent$name === void 0 ? null : _getComponent$name,
|
|
102
|
+
_getComponent$props = _getComponent.props,
|
|
103
|
+
componentProps = _getComponent$props === void 0 ? null : _getComponent$props;
|
|
104
|
+
|
|
105
|
+
return _objectSpread(_objectSpread({}, componentProps), {}, {
|
|
73
106
|
id: name,
|
|
74
107
|
label: label,
|
|
75
108
|
valueKey: name,
|
|
76
|
-
|
|
77
|
-
component: components !== null ? components.index_column || components.index || null : null,
|
|
109
|
+
component: componentName,
|
|
78
110
|
field: field
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
var sortColumns = function sortColumns(columns) {
|
|
82
|
-
return columns.sort(function (_ref, _ref2) {
|
|
83
|
-
var _ref$order = _ref.order,
|
|
84
|
-
aOrder = _ref$order === void 0 ? null : _ref$order;
|
|
85
|
-
var _ref2$order = _ref2.order,
|
|
86
|
-
bOrder = _ref2$order === void 0 ? null : _ref2$order;
|
|
87
|
-
|
|
88
|
-
if (aOrder === bOrder) {
|
|
89
|
-
return 0;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (aOrder === null) {
|
|
93
|
-
return 1;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (bOrder === null) {
|
|
97
|
-
return -1;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return aOrder > bOrder ? 1 : -1;
|
|
101
111
|
});
|
|
102
112
|
};
|
|
103
|
-
var
|
|
104
|
-
var fields = resource.fields
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
var newColumns = resourceColumns !== null ? resourceColumns.map(function (column) {
|
|
108
|
-
var _ref3 = isString(column) ? {
|
|
113
|
+
var getColumnsWithFields = function getColumnsWithFields(resource, columns) {
|
|
114
|
+
var fields = resource.fields;
|
|
115
|
+
var newColumns = columns !== null ? columns.map(function (column) {
|
|
116
|
+
var _ref = isString(column) ? {
|
|
109
117
|
field: column
|
|
110
118
|
} : column,
|
|
111
|
-
|
|
112
|
-
fieldName =
|
|
119
|
+
_ref$field = _ref.field,
|
|
120
|
+
fieldName = _ref$field === void 0 ? null : _ref$field,
|
|
121
|
+
otherProps = _objectWithoutProperties(_ref, ["field"]);
|
|
113
122
|
|
|
114
|
-
return fieldName !== null ? getColumnFromField(fields.find(function (it) {
|
|
123
|
+
return _objectSpread(_objectSpread({}, fieldName !== null ? getColumnFromField(fields.find(function (it) {
|
|
115
124
|
return it.name === fieldName;
|
|
116
|
-
}) || null) :
|
|
125
|
+
}) || null) : null), otherProps);
|
|
117
126
|
}).filter(function (it) {
|
|
118
127
|
return it !== null;
|
|
119
|
-
}) :
|
|
120
|
-
|
|
121
|
-
_ref4$settings = _ref4$settings === void 0 ? {} : _ref4$settings;
|
|
122
|
-
var _ref4$settings$hidden = _ref4$settings.hiddenInIndex,
|
|
123
|
-
hiddenInIndex = _ref4$settings$hidden === void 0 ? false : _ref4$settings$hidden;
|
|
124
|
-
return !hiddenInIndex;
|
|
125
|
-
}).map(function (field) {
|
|
126
|
-
return getColumnFromField(field);
|
|
127
|
-
});
|
|
128
|
-
return sortColumns(newColumns);
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
var getComponent = function getComponent(component) {
|
|
132
|
-
var _ref = isObject(component) ? component : {
|
|
133
|
-
component: component
|
|
134
|
-
};
|
|
135
|
-
_ref.component;
|
|
136
|
-
var props = _objectWithoutProperties(_ref, ["component"]);
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
name: component,
|
|
140
|
-
props: props
|
|
141
|
-
};
|
|
128
|
+
}) : [];
|
|
129
|
+
return newColumns;
|
|
142
130
|
};
|
|
143
131
|
|
|
144
132
|
var getComponentFromName = function getComponentFromName() {
|
|
@@ -251,9 +239,27 @@ var getFileName = function getFileName() {
|
|
|
251
239
|
|
|
252
240
|
var getSelectOptions = function getSelectOptions(options) {
|
|
253
241
|
return options.map(function (it) {
|
|
254
|
-
|
|
242
|
+
if (isObject(it)) {
|
|
243
|
+
var _it$label = it.label,
|
|
244
|
+
label = _it$label === void 0 ? null : _it$label,
|
|
245
|
+
value = it.value;
|
|
246
|
+
|
|
247
|
+
if (label === null) {
|
|
248
|
+
return {
|
|
249
|
+
value: value,
|
|
250
|
+
label: capitalize("".concat(value))
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
value: value,
|
|
256
|
+
label: "".concat(label)
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return {
|
|
255
261
|
value: it,
|
|
256
|
-
label: it
|
|
262
|
+
label: capitalize("".concat(it))
|
|
257
263
|
};
|
|
258
264
|
});
|
|
259
265
|
};
|
|
@@ -309,4 +315,4 @@ var validateFields = function validateFields(fields, value) {
|
|
|
309
315
|
}, true);
|
|
310
316
|
};
|
|
311
317
|
|
|
312
|
-
export { convertStyleToString, createNullableOnChange, createUseEvent,
|
|
318
|
+
export { convertStyleToString, createNullableOnChange, createUseEvent, flattenComponents, getColumnsWithFields, getComponent, getComponentFromName, getDefinitionFromId, getDisplayName, getFieldByName, getFieldFromPath, getFileName, getSelectOptions, isMessage, setValue as setFieldValue, slug, validateFields };
|