@webiny/react-properties 0.0.0-unstable.97a151f74d → 0.0.0-unstable.98511f29f1
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/{src/Properties.d.ts → Properties.d.ts} +7 -1
- package/Properties.js +104 -70
- package/Properties.js.map +1 -1
- package/createConfigurableComponent.d.ts +13 -0
- package/createConfigurableComponent.js +80 -0
- package/createConfigurableComponent.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.js +22 -4
- package/index.js.map +1 -1
- package/package.json +8 -7
- package/useIdGenerator.d.ts +1 -0
- package/useIdGenerator.js +17 -0
- package/useIdGenerator.js.map +1 -0
- package/{src/utils.d.ts → utils.d.ts} +1 -1
- package/utils.js +3 -13
- package/utils.js.map +1 -1
- package/__tests__/cases/dashboard/App.d.ts +0 -23
- package/__tests__/cases/dashboard/dashboard.test.d.ts +0 -1
- package/__tests__/cases/pbEditorSettings/PbEditorSettingsView.d.ts +0 -21
- package/__tests__/cases/pbEditorSettings/createConfigurableView.d.ts +0 -9
- package/__tests__/cases/pbEditorSettings/pbEditorSettings.test.d.ts +0 -1
- package/__tests__/properties.test.d.ts +0 -1
- package/__tests__/setupEnv.d.ts +0 -1
- package/__tests__/utils.d.ts +0 -2
- package/src/index.d.ts +0 -2
|
@@ -3,7 +3,7 @@ export interface Property {
|
|
|
3
3
|
id: string;
|
|
4
4
|
parent: string;
|
|
5
5
|
name: string;
|
|
6
|
-
value
|
|
6
|
+
value?: unknown;
|
|
7
7
|
array?: boolean;
|
|
8
8
|
}
|
|
9
9
|
interface AddPropertyOptions {
|
|
@@ -32,7 +32,13 @@ interface PropertyProps {
|
|
|
32
32
|
before?: string;
|
|
33
33
|
replace?: string;
|
|
34
34
|
remove?: boolean;
|
|
35
|
+
parent?: string;
|
|
36
|
+
root?: boolean;
|
|
35
37
|
}
|
|
36
38
|
export declare function useParentProperty(): Property | undefined;
|
|
39
|
+
interface AncestorMatch {
|
|
40
|
+
[key: string]: string | boolean | number | null | undefined;
|
|
41
|
+
}
|
|
42
|
+
export declare function useAncestor(params: AncestorMatch): Property | undefined;
|
|
37
43
|
export declare const Property: React.FC<PropertyProps>;
|
|
38
44
|
export {};
|
package/Properties.js
CHANGED
|
@@ -1,26 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
-
|
|
5
4
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
-
|
|
7
5
|
Object.defineProperty(exports, "__esModule", {
|
|
8
6
|
value: true
|
|
9
7
|
});
|
|
10
8
|
exports.Property = exports.Properties = void 0;
|
|
9
|
+
exports.useAncestor = useAncestor;
|
|
11
10
|
exports.useParentProperty = useParentProperty;
|
|
12
11
|
exports.useProperties = useProperties;
|
|
13
|
-
|
|
14
12
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
15
|
-
|
|
16
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
17
|
-
|
|
18
13
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
19
|
-
|
|
14
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
20
15
|
var _react = _interopRequireWildcard(require("react"));
|
|
21
|
-
|
|
22
16
|
var _utils = require("./utils");
|
|
23
|
-
|
|
24
17
|
function removeByParent(id, properties) {
|
|
25
18
|
return properties.filter(function (prop) {
|
|
26
19
|
return prop.parent === id;
|
|
@@ -30,18 +23,60 @@ function removeByParent(id, properties) {
|
|
|
30
23
|
}));
|
|
31
24
|
}, properties);
|
|
32
25
|
}
|
|
33
|
-
|
|
26
|
+
function putPropertyBefore(properties, property, before) {
|
|
27
|
+
var existingIndex = properties.findIndex(function (prop) {
|
|
28
|
+
return prop.id === property.id;
|
|
29
|
+
});
|
|
30
|
+
if (existingIndex > -1) {
|
|
31
|
+
var existingProperty = properties[existingIndex];
|
|
32
|
+
var newProperties = properties.filter(function (p) {
|
|
33
|
+
return p.id !== property.id;
|
|
34
|
+
});
|
|
35
|
+
var _targetIndex = newProperties.findIndex(function (prop) {
|
|
36
|
+
return prop.id === before;
|
|
37
|
+
});
|
|
38
|
+
return [].concat((0, _toConsumableArray2.default)(newProperties.slice(0, _targetIndex)), [existingProperty], (0, _toConsumableArray2.default)(newProperties.slice(_targetIndex)));
|
|
39
|
+
}
|
|
40
|
+
var targetIndex = properties.findIndex(function (prop) {
|
|
41
|
+
return prop.id === before;
|
|
42
|
+
});
|
|
43
|
+
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, targetIndex)), [property], (0, _toConsumableArray2.default)(properties.slice(targetIndex)));
|
|
44
|
+
}
|
|
45
|
+
function putPropertyAfter(properties, property, after) {
|
|
46
|
+
var existingIndex = properties.findIndex(function (prop) {
|
|
47
|
+
return prop.id === property.id;
|
|
48
|
+
});
|
|
49
|
+
if (existingIndex > -1) {
|
|
50
|
+
var _properties$splice = properties.splice(existingIndex, 1),
|
|
51
|
+
_properties$splice2 = (0, _slicedToArray2.default)(_properties$splice, 1),
|
|
52
|
+
removedProperty = _properties$splice2[0];
|
|
53
|
+
var _targetIndex2 = properties.findIndex(function (prop) {
|
|
54
|
+
return prop.id === after;
|
|
55
|
+
});
|
|
56
|
+
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _targetIndex2 + 1)), [removedProperty], (0, _toConsumableArray2.default)(properties.slice(_targetIndex2 + 1)));
|
|
57
|
+
}
|
|
58
|
+
var targetIndex = properties.findIndex(function (prop) {
|
|
59
|
+
return prop.id === after;
|
|
60
|
+
});
|
|
61
|
+
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, targetIndex + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(targetIndex + 1)));
|
|
62
|
+
}
|
|
63
|
+
function mergeProperty(properties, property) {
|
|
64
|
+
var index = properties.findIndex(function (prop) {
|
|
65
|
+
return prop.id === property.id;
|
|
66
|
+
});
|
|
67
|
+
if (index > -1) {
|
|
68
|
+
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, index)), [(0, _objectSpread2.default)((0, _objectSpread2.default)({}, properties[index]), property)], (0, _toConsumableArray2.default)(properties.slice(index + 1)));
|
|
69
|
+
}
|
|
70
|
+
return properties;
|
|
71
|
+
}
|
|
34
72
|
var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
35
|
-
|
|
36
73
|
var Properties = function Properties(_ref) {
|
|
37
74
|
var onChange = _ref.onChange,
|
|
38
|
-
|
|
39
|
-
|
|
75
|
+
children = _ref.children;
|
|
40
76
|
var _useState = (0, _react.useState)([]),
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
77
|
+
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
78
|
+
properties = _useState2[0],
|
|
79
|
+
setProperties = _useState2[1];
|
|
45
80
|
(0, _react.useEffect)(function () {
|
|
46
81
|
if (onChange) {
|
|
47
82
|
onChange(properties);
|
|
@@ -56,35 +91,25 @@ var Properties = function Properties(_ref) {
|
|
|
56
91
|
addProperty: function addProperty(property) {
|
|
57
92
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
58
93
|
setProperties(function (properties) {
|
|
59
|
-
// If a property with this ID already exists, merge the two properties.
|
|
60
94
|
var index = properties.findIndex(function (prop) {
|
|
61
95
|
return prop.id === property.id;
|
|
62
96
|
});
|
|
63
|
-
|
|
64
97
|
if (index > -1) {
|
|
65
|
-
|
|
98
|
+
var newProperties = mergeProperty(properties, property);
|
|
99
|
+
if (options.after) {
|
|
100
|
+
return putPropertyAfter(newProperties, property, options.after);
|
|
101
|
+
}
|
|
102
|
+
if (options.before) {
|
|
103
|
+
return putPropertyBefore(newProperties, property, options.before);
|
|
104
|
+
}
|
|
105
|
+
return newProperties;
|
|
66
106
|
}
|
|
67
|
-
|
|
68
107
|
if (options.after) {
|
|
69
|
-
|
|
70
|
-
return prop.id === options.after;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (_index > -1) {
|
|
74
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
|
|
75
|
-
}
|
|
108
|
+
return putPropertyAfter(properties, property, options.after);
|
|
76
109
|
}
|
|
77
|
-
|
|
78
110
|
if (options.before) {
|
|
79
|
-
|
|
80
|
-
return prop.id === options.before;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (_index2 > -1) {
|
|
84
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
|
|
85
|
-
}
|
|
111
|
+
return putPropertyBefore(properties, property, options.before);
|
|
86
112
|
}
|
|
87
|
-
|
|
88
113
|
return [].concat((0, _toConsumableArray2.default)(properties), [property]);
|
|
89
114
|
});
|
|
90
115
|
},
|
|
@@ -100,12 +125,10 @@ var Properties = function Properties(_ref) {
|
|
|
100
125
|
var toReplace = properties.findIndex(function (prop) {
|
|
101
126
|
return prop.id === id;
|
|
102
127
|
});
|
|
103
|
-
|
|
104
128
|
if (toReplace > -1) {
|
|
105
129
|
// Replace the property and remove all remaining child properties.
|
|
106
130
|
return removeByParent(id, [].concat((0, _toConsumableArray2.default)(properties.slice(0, toReplace)), [property], (0, _toConsumableArray2.default)(properties.slice(toReplace + 1))));
|
|
107
131
|
}
|
|
108
|
-
|
|
109
132
|
return properties;
|
|
110
133
|
});
|
|
111
134
|
}
|
|
@@ -115,58 +138,74 @@ var Properties = function Properties(_ref) {
|
|
|
115
138
|
value: context
|
|
116
139
|
}, children);
|
|
117
140
|
};
|
|
118
|
-
|
|
119
141
|
exports.Properties = Properties;
|
|
120
|
-
|
|
121
142
|
function useProperties() {
|
|
122
143
|
var properties = (0, _react.useContext)(PropertiesContext);
|
|
123
|
-
|
|
124
144
|
if (!properties) {
|
|
125
145
|
throw Error("Properties context provider is missing!");
|
|
126
146
|
}
|
|
127
|
-
|
|
128
147
|
return properties;
|
|
129
148
|
}
|
|
130
|
-
|
|
131
149
|
var PropertyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
132
|
-
|
|
133
150
|
function useParentProperty() {
|
|
134
151
|
return (0, _react.useContext)(PropertyContext);
|
|
135
152
|
}
|
|
136
|
-
|
|
153
|
+
function useAncestor(params) {
|
|
154
|
+
var property = useParentProperty();
|
|
155
|
+
var _useProperties = useProperties(),
|
|
156
|
+
properties = _useProperties.properties;
|
|
157
|
+
var matchOrGetAncestor = function matchOrGetAncestor(property, params) {
|
|
158
|
+
var matchedProps = properties.filter(function (prop) {
|
|
159
|
+
return prop.parent === property.id;
|
|
160
|
+
}).filter(function (prop) {
|
|
161
|
+
return prop.name in params && prop.value === params[prop.name];
|
|
162
|
+
});
|
|
163
|
+
if (matchedProps.length === Object.keys(params).length) {
|
|
164
|
+
return property;
|
|
165
|
+
}
|
|
166
|
+
var newParent = property.parent ? properties.find(function (prop) {
|
|
167
|
+
return prop.id === property.parent;
|
|
168
|
+
}) : undefined;
|
|
169
|
+
return newParent ? matchOrGetAncestor(newParent, params) : undefined;
|
|
170
|
+
};
|
|
171
|
+
return property ? matchOrGetAncestor(property, params) : undefined;
|
|
172
|
+
}
|
|
137
173
|
var Property = function Property(_ref2) {
|
|
138
174
|
var id = _ref2.id,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
175
|
+
name = _ref2.name,
|
|
176
|
+
value = _ref2.value,
|
|
177
|
+
children = _ref2.children,
|
|
178
|
+
_ref2$after = _ref2.after,
|
|
179
|
+
after = _ref2$after === void 0 ? undefined : _ref2$after,
|
|
180
|
+
_ref2$before = _ref2.before,
|
|
181
|
+
before = _ref2$before === void 0 ? undefined : _ref2$before,
|
|
182
|
+
_ref2$replace = _ref2.replace,
|
|
183
|
+
replace = _ref2$replace === void 0 ? undefined : _ref2$replace,
|
|
184
|
+
_ref2$remove = _ref2.remove,
|
|
185
|
+
remove = _ref2$remove === void 0 ? false : _ref2$remove,
|
|
186
|
+
_ref2$array = _ref2.array,
|
|
187
|
+
array = _ref2$array === void 0 ? false : _ref2$array,
|
|
188
|
+
_ref2$root = _ref2.root,
|
|
189
|
+
root = _ref2$root === void 0 ? false : _ref2$root,
|
|
190
|
+
_ref2$parent = _ref2.parent,
|
|
191
|
+
parent = _ref2$parent === void 0 ? undefined : _ref2$parent;
|
|
152
192
|
var uniqueId = (0, _react.useMemo)(function () {
|
|
153
193
|
return id || (0, _utils.getUniqueId)();
|
|
154
194
|
}, []);
|
|
155
|
-
var
|
|
195
|
+
var parentProperty = useParentProperty();
|
|
156
196
|
var properties = useProperties();
|
|
157
|
-
|
|
158
197
|
if (!properties) {
|
|
159
198
|
throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
160
199
|
}
|
|
161
|
-
|
|
162
200
|
var addProperty = properties.addProperty,
|
|
163
|
-
|
|
164
|
-
|
|
201
|
+
removeProperty = properties.removeProperty,
|
|
202
|
+
replaceProperty = properties.replaceProperty;
|
|
203
|
+
var parentId = parent ? parent : root ? "" : (parentProperty === null || parentProperty === void 0 ? void 0 : parentProperty.id) || "";
|
|
165
204
|
var property = {
|
|
166
205
|
id: uniqueId,
|
|
167
206
|
name: name,
|
|
168
207
|
value: value,
|
|
169
|
-
parent:
|
|
208
|
+
parent: parentId,
|
|
170
209
|
array: array
|
|
171
210
|
};
|
|
172
211
|
(0, _react.useEffect)(function () {
|
|
@@ -174,12 +213,10 @@ var Property = function Property(_ref2) {
|
|
|
174
213
|
removeProperty(uniqueId);
|
|
175
214
|
return;
|
|
176
215
|
}
|
|
177
|
-
|
|
178
216
|
if (replace) {
|
|
179
217
|
replaceProperty(replace, property);
|
|
180
218
|
return;
|
|
181
219
|
}
|
|
182
|
-
|
|
183
220
|
addProperty(property, {
|
|
184
221
|
after: after,
|
|
185
222
|
before: before
|
|
@@ -188,14 +225,11 @@ var Property = function Property(_ref2) {
|
|
|
188
225
|
removeProperty(uniqueId);
|
|
189
226
|
};
|
|
190
227
|
}, []);
|
|
191
|
-
|
|
192
228
|
if (children) {
|
|
193
229
|
return /*#__PURE__*/_react.default.createElement(PropertyContext.Provider, {
|
|
194
230
|
value: property
|
|
195
231
|
}, children);
|
|
196
232
|
}
|
|
197
|
-
|
|
198
233
|
return null;
|
|
199
234
|
};
|
|
200
|
-
|
|
201
235
|
exports.Property = Property;
|
package/Properties.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["removeByParent","id","properties","filter","prop","parent","reduce","acc","item","PropertiesContext","createContext","undefined","Properties","onChange","children","useState","setProperties","useEffect","context","useMemo","getObject","toObject","addProperty","property","options","index","findIndex","slice","after","before","removeProperty","replaceProperty","toReplace","useProperties","useContext","Error","PropertyContext","useParentProperty","Property","name","value","replace","remove","array","uniqueId","getUniqueId"],"sources":["Properties.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo, useState } from \"react\";\nimport { getUniqueId, toObject } from \"./utils\";\n\nexport interface Property {\n id: string;\n parent: string;\n name: string;\n value: unknown;\n array?: boolean;\n}\n\nfunction removeByParent(id: string, properties: Property[]): Property[] {\n return properties\n .filter(prop => prop.parent === id)\n .reduce((acc, item) => {\n return removeByParent(\n item.id,\n acc.filter(prop => prop.id !== item.id)\n );\n }, properties);\n}\n\ninterface AddPropertyOptions {\n after?: string;\n before?: string;\n}\n\ninterface PropertiesContext {\n properties: Property[];\n getObject<T = unknown>(): T;\n addProperty(property: Property, options?: AddPropertyOptions): void;\n removeProperty(id: string): void;\n replaceProperty(id: string, property: Property): void;\n}\n\nconst PropertiesContext = createContext<PropertiesContext | undefined>(undefined);\n\ninterface PropertiesProps {\n onChange?(properties: Property[]): void;\n}\n\nexport const Properties: React.FC<PropertiesProps> = ({ onChange, children }) => {\n const [properties, setProperties] = useState<Property[]>([]);\n\n useEffect(() => {\n if (onChange) {\n onChange(properties);\n }\n }, [properties]);\n\n const context: PropertiesContext = useMemo(\n () => ({\n properties,\n getObject<T>() {\n return toObject(properties) as T;\n },\n addProperty(property, options = {}) {\n setProperties(properties => {\n // If a property with this ID already exists, merge the two properties.\n const index = properties.findIndex(prop => prop.id === property.id);\n if (index > -1) {\n return [\n ...properties.slice(0, index),\n { ...properties[index], ...property },\n ...properties.slice(index + 1)\n ];\n }\n\n if (options.after) {\n const index = properties.findIndex(prop => prop.id === options.after);\n if (index > -1) {\n return [\n ...properties.slice(0, index + 1),\n property,\n ...properties.slice(index + 1)\n ];\n }\n }\n\n if (options.before) {\n const index = properties.findIndex(prop => prop.id === options.before);\n if (index > -1) {\n return [\n ...properties.slice(0, index),\n property,\n ...properties.slice(index)\n ];\n }\n }\n\n return [...properties, property];\n });\n },\n removeProperty(id) {\n setProperties(properties => {\n return removeByParent(\n id,\n properties.filter(prop => prop.id !== id)\n );\n });\n },\n replaceProperty(id, property) {\n setProperties(properties => {\n const toReplace = properties.findIndex(prop => prop.id === id);\n\n if (toReplace > -1) {\n // Replace the property and remove all remaining child properties.\n return removeByParent(id, [\n ...properties.slice(0, toReplace),\n property,\n ...properties.slice(toReplace + 1)\n ]);\n }\n return properties;\n });\n }\n }),\n [properties]\n );\n\n return <PropertiesContext.Provider value={context}>{children}</PropertiesContext.Provider>;\n};\n\nexport function useProperties() {\n const properties = useContext(PropertiesContext);\n if (!properties) {\n throw Error(\"Properties context provider is missing!\");\n }\n\n return properties;\n}\n\ninterface PropertyProps {\n id?: string;\n name: string;\n value?: unknown;\n array?: boolean;\n after?: string;\n before?: string;\n replace?: string;\n remove?: boolean;\n}\n\nconst PropertyContext = createContext<Property | undefined>(undefined);\n\nexport function useParentProperty() {\n return useContext(PropertyContext);\n}\n\nexport const Property: React.FC<PropertyProps> = ({\n id,\n name,\n value,\n children,\n after = undefined,\n before = undefined,\n replace = undefined,\n remove = false,\n array = false\n}) => {\n const uniqueId = useMemo(() => id || getUniqueId(), []);\n const parent = useParentProperty();\n const properties = useProperties();\n\n if (!properties) {\n throw Error(\"<Properties> provider is missing higher in the hierarchy!\");\n }\n\n const { addProperty, removeProperty, replaceProperty } = properties;\n const property = { id: uniqueId, name, value, parent: parent ? parent.id : \"\", array };\n\n useEffect(() => {\n if (remove) {\n removeProperty(uniqueId);\n return;\n }\n\n if (replace) {\n replaceProperty(replace, property);\n return;\n }\n\n addProperty(property, { after, before });\n\n return () => {\n removeProperty(uniqueId);\n };\n }, []);\n\n if (children) {\n return <PropertyContext.Provider value={property}>{children}</PropertyContext.Provider>;\n }\n\n return null;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AAUA,SAASA,cAAT,CAAwBC,EAAxB,EAAoCC,UAApC,EAAwE;EACpE,OAAOA,UAAU,CACZC,MADE,CACK,UAAAC,IAAI;IAAA,OAAIA,IAAI,CAACC,MAAL,KAAgBJ,EAApB;EAAA,CADT,EAEFK,MAFE,CAEK,UAACC,GAAD,EAAMC,IAAN,EAAe;IACnB,OAAOR,cAAc,CACjBQ,IAAI,CAACP,EADY,EAEjBM,GAAG,CAACJ,MAAJ,CAAW,UAAAC,IAAI;MAAA,OAAIA,IAAI,CAACH,EAAL,KAAYO,IAAI,CAACP,EAArB;IAAA,CAAf,CAFiB,CAArB;EAIH,CAPE,EAOAC,UAPA,CAAP;AAQH;;AAeD,IAAMO,iBAAiB,gBAAG,IAAAC,oBAAA,EAA6CC,SAA7C,CAA1B;;AAMO,IAAMC,UAAqC,GAAG,SAAxCA,UAAwC,OAA4B;EAAA,IAAzBC,QAAyB,QAAzBA,QAAyB;EAAA,IAAfC,QAAe,QAAfA,QAAe;;EAC7E,gBAAoC,IAAAC,eAAA,EAAqB,EAArB,CAApC;EAAA;EAAA,IAAOb,UAAP;EAAA,I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B,UAAzD,CAAQoB,WAAR;EAAA,IAAqBQ,cAArB,GAAyD5B,UAAzD,CAAqB4B,cAArB;EAAA,IAAqCC,eAArC,GAAyD7B,UAAzD,CAAqC6B,eAArC;EACA,IAAMR,QAAQ,GAAG;IAAEtB,EAAE,EAAE2C,QAAN;IAAgBL,IAAI,EAAJA,IAAhB;IAAsBC,KAAK,EAALA,KAAtB;IAA6BnC,MAAM,EAAEA,MAAM,GAAGA,MAAM,CAACJ,EAAV,GAAe,EAA1D;IAA8D0C,KAAK,EAALA;EAA9D,CAAjB;EAEA,IAAA1B,gBAAA,EAAU,YAAM;IACZ,IAAIyB,MAAJ,EAAY;MACRZ,cAAc,CAACc,QAAD,CAAd;MACA;IACH;;IAED,IAAIH,OAAJ,EAAa;MACTV,eAAe,CAACU,OAAD,EAAUlB,QAAV,CAAf;MACA;IACH;;IAEDD,WAAW,CAACC,QAAD,EAAW;MAAEK,KAAK,EAALA,KAAF;MAASC,MAAM,EAANA;IAAT,CAAX,CAAX;IAEA,OAAO,YAAM;MACTC,cAAc,CAACc,QAAD,CAAd;IACH,CAFD;EAGH,CAhBD,EAgBG,EAhBH;;EAkBA,IAAI9B,QAAJ,EAAc;IACV,oBAAO,6BAAC,eAAD,CAAiB,QAAjB;MAA0B,KAAK,EAAES;IAAjC,GAA4CT,QAA5C,CAAP;EACH;;EAED,OAAO,IAAP;AACH,CA7CM"}
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_utils","removeByParent","id","properties","filter","prop","parent","reduce","acc","item","putPropertyBefore","property","before","existingIndex","findIndex","existingProperty","newProperties","p","targetIndex","concat","_toConsumableArray2","default","slice","putPropertyAfter","after","_properties$splice","splice","_properties$splice2","_slicedToArray2","removedProperty","mergeProperty","index","_objectSpread2","PropertiesContext","createContext","undefined","Properties","_ref","onChange","children","_useState","useState","_useState2","setProperties","useEffect","context","useMemo","getObject","toObject","addProperty","options","arguments","length","removeProperty","replaceProperty","toReplace","createElement","Provider","value","exports","useProperties","useContext","Error","PropertyContext","useParentProperty","useAncestor","params","_useProperties","matchOrGetAncestor","matchedProps","name","Object","keys","newParent","find","Property","_ref2","_ref2$after","_ref2$before","_ref2$replace","replace","_ref2$remove","remove","_ref2$array","array","_ref2$root","root","_ref2$parent","uniqueId","getUniqueId","parentProperty","parentId"],"sources":["Properties.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo, useState } from \"react\";\nimport { getUniqueId, toObject } from \"./utils\";\n\nexport interface Property {\n id: string;\n parent: string;\n name: string;\n value?: unknown;\n array?: boolean;\n}\n\nfunction removeByParent(id: string, properties: Property[]): Property[] {\n return properties\n .filter(prop => prop.parent === id)\n .reduce((acc, item) => {\n return removeByParent(\n item.id,\n acc.filter(prop => prop.id !== item.id)\n );\n }, properties);\n}\n\ninterface AddPropertyOptions {\n after?: string;\n before?: string;\n}\n\ninterface PropertiesContext {\n properties: Property[];\n getObject<T = unknown>(): T;\n addProperty(property: Property, options?: AddPropertyOptions): void;\n removeProperty(id: string): void;\n replaceProperty(id: string, property: Property): void;\n}\n\nfunction putPropertyBefore(properties: Property[], property: Property, before: string) {\n const existingIndex = properties.findIndex(prop => prop.id === property.id);\n if (existingIndex > -1) {\n const existingProperty = properties[existingIndex];\n const newProperties = properties.filter(p => p.id !== property.id);\n const targetIndex = newProperties.findIndex(prop => prop.id === before);\n return [\n ...newProperties.slice(0, targetIndex),\n existingProperty,\n ...newProperties.slice(targetIndex)\n ];\n }\n\n const targetIndex = properties.findIndex(prop => prop.id === before);\n\n return [...properties.slice(0, targetIndex), property, ...properties.slice(targetIndex)];\n}\n\nfunction putPropertyAfter(properties: Property[], property: Property, after: string) {\n const existingIndex = properties.findIndex(prop => prop.id === property.id);\n\n if (existingIndex > -1) {\n const [removedProperty] = properties.splice(existingIndex, 1);\n const targetIndex = properties.findIndex(prop => prop.id === after);\n return [\n ...properties.slice(0, targetIndex + 1),\n removedProperty,\n ...properties.slice(targetIndex + 1)\n ];\n }\n\n const targetIndex = properties.findIndex(prop => prop.id === after);\n\n return [\n ...properties.slice(0, targetIndex + 1),\n property,\n ...properties.slice(targetIndex + 1)\n ];\n}\n\nfunction mergeProperty(properties: Property[], property: Property) {\n const index = properties.findIndex(prop => prop.id === property.id);\n if (index > -1) {\n return [\n ...properties.slice(0, index),\n { ...properties[index], ...property },\n ...properties.slice(index + 1)\n ];\n }\n return properties;\n}\n\nconst PropertiesContext = createContext<PropertiesContext | undefined>(undefined);\n\ninterface PropertiesProps {\n onChange?(properties: Property[]): void;\n}\n\nexport const Properties: React.FC<PropertiesProps> = ({ onChange, children }) => {\n const [properties, setProperties] = useState<Property[]>([]);\n\n useEffect(() => {\n if (onChange) {\n onChange(properties);\n }\n }, [properties]);\n\n const context: PropertiesContext = useMemo(\n () => ({\n properties,\n getObject<T>() {\n return toObject(properties) as T;\n },\n addProperty(property, options = {}) {\n setProperties(properties => {\n const index = properties.findIndex(prop => prop.id === property.id);\n\n if (index > -1) {\n const newProperties = mergeProperty(properties, property);\n if (options.after) {\n return putPropertyAfter(newProperties, property, options.after);\n }\n if (options.before) {\n return putPropertyBefore(newProperties, property, options.before);\n }\n\n return newProperties;\n }\n\n if (options.after) {\n return putPropertyAfter(properties, property, options.after);\n }\n\n if (options.before) {\n return putPropertyBefore(properties, property, options.before);\n }\n\n return [...properties, property];\n });\n },\n removeProperty(id) {\n setProperties(properties => {\n return removeByParent(\n id,\n properties.filter(prop => prop.id !== id)\n );\n });\n },\n replaceProperty(id, property) {\n setProperties(properties => {\n const toReplace = properties.findIndex(prop => prop.id === id);\n\n if (toReplace > -1) {\n // Replace the property and remove all remaining child properties.\n return removeByParent(id, [\n ...properties.slice(0, toReplace),\n property,\n ...properties.slice(toReplace + 1)\n ]);\n }\n return properties;\n });\n }\n }),\n [properties]\n );\n\n return <PropertiesContext.Provider value={context}>{children}</PropertiesContext.Provider>;\n};\n\nexport function useProperties() {\n const properties = useContext(PropertiesContext);\n if (!properties) {\n throw Error(\"Properties context provider is missing!\");\n }\n\n return properties;\n}\n\ninterface PropertyProps {\n id?: string;\n name: string;\n value?: unknown;\n array?: boolean;\n after?: string;\n before?: string;\n replace?: string;\n remove?: boolean;\n parent?: string;\n root?: boolean;\n}\n\nconst PropertyContext = createContext<Property | undefined>(undefined);\n\nexport function useParentProperty() {\n return useContext(PropertyContext);\n}\n\ninterface AncestorMatch {\n [key: string]: string | boolean | number | null | undefined;\n}\n\nexport function useAncestor(params: AncestorMatch) {\n const property = useParentProperty();\n const { properties } = useProperties();\n\n const matchOrGetAncestor = (\n property: Property,\n params: AncestorMatch\n ): Property | undefined => {\n const matchedProps = properties\n .filter(prop => prop.parent === property.id)\n .filter(prop => prop.name in params && prop.value === params[prop.name]);\n\n if (matchedProps.length === Object.keys(params).length) {\n return property;\n }\n\n const newParent = property.parent\n ? properties.find(prop => prop.id === property.parent)\n : undefined;\n\n return newParent ? matchOrGetAncestor(newParent, params) : undefined;\n };\n\n return property ? matchOrGetAncestor(property, params) : undefined;\n}\n\nexport const Property: React.FC<PropertyProps> = ({\n id,\n name,\n value,\n children,\n after = undefined,\n before = undefined,\n replace = undefined,\n remove = false,\n array = false,\n root = false,\n parent = undefined\n}) => {\n const uniqueId = useMemo(() => id || getUniqueId(), []);\n const parentProperty = useParentProperty();\n const properties = useProperties();\n\n if (!properties) {\n throw Error(\"<Properties> provider is missing higher in the hierarchy!\");\n }\n\n const { addProperty, removeProperty, replaceProperty } = properties;\n const parentId = parent ? parent : root ? \"\" : parentProperty?.id || \"\";\n const property = { id: uniqueId, name, value, parent: parentId, array };\n\n useEffect(() => {\n if (remove) {\n removeProperty(uniqueId);\n return;\n }\n\n if (replace) {\n replaceProperty(replace, property);\n return;\n }\n\n addProperty(property, { after, before });\n\n return () => {\n removeProperty(uniqueId);\n };\n }, []);\n\n if (children) {\n return <PropertyContext.Provider value={property}>{children}</PropertyContext.Provider>;\n }\n\n return null;\n};\n"],"mappings":";;;;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAA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5E;EAEA,IAAQb,WAAW,GAAsC9C,UAAU,CAA3D8C,WAAW;IAAEI,cAAc,GAAsBlD,UAAU,CAA9CkD,cAAc;IAAEC,eAAe,GAAKnD,UAAU,CAA9BmD,eAAe;EACpD,IAAMqC,QAAQ,GAAGrF,MAAM,GAAGA,MAAM,GAAGgF,IAAI,GAAG,EAAE,GAAG,CAAAI,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAExF,EAAE,KAAI,EAAE;EACvE,IAAMS,QAAQ,GAAG;IAAET,EAAE,EAAEsF,QAAQ;IAAElB,IAAI,EAAJA,IAAI;IAAEZ,KAAK,EAALA,KAAK;IAAEpD,MAAM,EAAEqF,QAAQ;IAAEP,KAAK,EAALA;EAAM,CAAC;EAEvE,IAAAxC,gBAAS,EAAC,YAAM;IACZ,IAAIsC,MAAM,EAAE;MACR7B,cAAc,CAACmC,QAAQ,CAAC;MACxB;IACJ;IAEA,IAAIR,OAAO,EAAE;MACT1B,eAAe,CAAC0B,OAAO,EAAErE,QAAQ,CAAC;MAClC;IACJ;IAEAsC,WAAW,CAACtC,QAAQ,EAAE;MAAEa,KAAK,EAALA,KAAK;MAAEZ,MAAM,EAANA;IAAO,CAAC,CAAC;IAExC,OAAO,YAAM;MACTyC,cAAc,CAACmC,QAAQ,CAAC;IAC5B,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAIjD,QAAQ,EAAE;IACV,oBAAO1C,MAAA,CAAAwB,OAAA,CAAAmC,aAAA,CAACO,eAAe,CAACN,QAAQ;MAACC,KAAK,EAAE/C;IAAS,GAAE4B,QAAmC,CAAC;EAC3F;EAEA,OAAO,IAAI;AACf,CAAC;AAACoB,OAAA,CAAAgB,QAAA,GAAAA,QAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Property } from "./index";
|
|
3
|
+
export interface WithConfigProps {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
onProperties?(properties: Property[]): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function createConfigurableComponent<TConfig>(name: string): {
|
|
8
|
+
WithConfig: ({ onProperties, children }: WithConfigProps) => JSX.Element;
|
|
9
|
+
Config: ({ children }: {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
}) => JSX.Element;
|
|
12
|
+
useConfig: <TExtra extends object>() => TConfig & TExtra;
|
|
13
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createConfigurableComponent = createConfigurableComponent;
|
|
9
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
10
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
11
|
+
var _reactComposition = require("@webiny/react-composition");
|
|
12
|
+
var _index = require("./index");
|
|
13
|
+
var createHOC = function createHOC(newChildren) {
|
|
14
|
+
return function (BaseComponent) {
|
|
15
|
+
return function ConfigHOC(_ref) {
|
|
16
|
+
var children = _ref.children;
|
|
17
|
+
return /*#__PURE__*/_react.default.createElement(BaseComponent, null, newChildren, children);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
function createConfigurableComponent(name) {
|
|
22
|
+
/**
|
|
23
|
+
* This component is used when we want to mount all composed configs.
|
|
24
|
+
*/
|
|
25
|
+
var ConfigApply = (0, _reactComposition.makeComposable)("".concat(name, "ConfigApply"), function (_ref2) {
|
|
26
|
+
var children = _ref2.children;
|
|
27
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This component is used to configure the component (it can be mounted many times).
|
|
32
|
+
*/
|
|
33
|
+
var Config = function Config(_ref3) {
|
|
34
|
+
var children = _ref3.children;
|
|
35
|
+
return /*#__PURE__*/_react.default.createElement(_reactComposition.Compose, {
|
|
36
|
+
component: ConfigApply,
|
|
37
|
+
with: createHOC(children)
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
var defaultContext = {
|
|
41
|
+
properties: []
|
|
42
|
+
};
|
|
43
|
+
var ViewContext = /*#__PURE__*/_react.default.createContext(defaultContext);
|
|
44
|
+
var WithConfig = function WithConfig(_ref4) {
|
|
45
|
+
var onProperties = _ref4.onProperties,
|
|
46
|
+
children = _ref4.children;
|
|
47
|
+
var _useState = (0, _react.useState)([]),
|
|
48
|
+
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
49
|
+
properties = _useState2[0],
|
|
50
|
+
setProperties = _useState2[1];
|
|
51
|
+
var context = {
|
|
52
|
+
properties: properties
|
|
53
|
+
};
|
|
54
|
+
(0, _react.useEffect)(function () {
|
|
55
|
+
if (typeof onProperties === "function") {
|
|
56
|
+
onProperties(properties);
|
|
57
|
+
}
|
|
58
|
+
}, [properties]);
|
|
59
|
+
var stateUpdater = function stateUpdater(properties) {
|
|
60
|
+
setProperties(properties);
|
|
61
|
+
};
|
|
62
|
+
return /*#__PURE__*/_react.default.createElement(ViewContext.Provider, {
|
|
63
|
+
value: context
|
|
64
|
+
}, /*#__PURE__*/_react.default.createElement(_index.Properties, {
|
|
65
|
+
onChange: stateUpdater
|
|
66
|
+
}, /*#__PURE__*/_react.default.createElement(ConfigApply, null), children));
|
|
67
|
+
};
|
|
68
|
+
function useConfig() {
|
|
69
|
+
var _useContext = (0, _react.useContext)(ViewContext),
|
|
70
|
+
properties = _useContext.properties;
|
|
71
|
+
return (0, _react.useMemo)(function () {
|
|
72
|
+
return (0, _index.toObject)(properties);
|
|
73
|
+
}, [properties]);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
WithConfig: WithConfig,
|
|
77
|
+
Config: Config,
|
|
78
|
+
useConfig: useConfig
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactComposition","_index","createHOC","newChildren","BaseComponent","ConfigHOC","_ref","children","default","createElement","createConfigurableComponent","name","ConfigApply","makeComposable","concat","_ref2","Fragment","Config","_ref3","Compose","component","with","defaultContext","properties","ViewContext","React","createContext","WithConfig","_ref4","onProperties","_useState","useState","_useState2","_slicedToArray2","setProperties","context","useEffect","stateUpdater","Provider","value","Properties","onChange","useConfig","_useContext","useContext","useMemo","toObject"],"sources":["createConfigurableComponent.tsx"],"sourcesContent":["import React, { useContext, useEffect, useMemo, useState } from \"react\";\nimport { Compose, HigherOrderComponent, makeComposable } from \"@webiny/react-composition\";\nimport { Property, Properties, toObject } from \"~/index\";\n\nconst createHOC =\n (newChildren: React.ReactNode): HigherOrderComponent =>\n BaseComponent => {\n return function ConfigHOC({ children }) {\n return (\n <BaseComponent>\n {newChildren}\n {children}\n </BaseComponent>\n );\n };\n };\n\nexport interface WithConfigProps {\n children: React.ReactNode;\n onProperties?(properties: Property[]): void;\n}\n\nexport function createConfigurableComponent<TConfig>(name: string) {\n /**\n * This component is used when we want to mount all composed configs.\n */\n const ConfigApply = makeComposable(`${name}ConfigApply`, ({ children }) => {\n return <>{children}</>;\n });\n\n /**\n * This component is used to configure the component (it can be mounted many times).\n */\n const Config = ({ children }: { children: React.ReactNode }) => {\n return <Compose component={ConfigApply} with={createHOC(children)} />;\n };\n\n interface ViewContext {\n properties: Property[];\n }\n\n const defaultContext = { properties: [] };\n\n const ViewContext = React.createContext<ViewContext>(defaultContext);\n\n const WithConfig = ({ onProperties, children }: WithConfigProps) => {\n const [properties, setProperties] = useState<Property[]>([]);\n const context = { properties };\n\n useEffect(() => {\n if (typeof onProperties === \"function\") {\n onProperties(properties);\n }\n }, [properties]);\n\n const stateUpdater = (properties: Property[]) => {\n setProperties(properties);\n };\n\n return (\n <ViewContext.Provider value={context}>\n <Properties onChange={stateUpdater}>\n <ConfigApply />\n {children}\n </Properties>\n </ViewContext.Provider>\n );\n };\n\n function useConfig<TExtra extends object>(): TConfig & TExtra {\n const { properties } = useContext(ViewContext);\n return useMemo(() => toObject<TConfig & TExtra>(properties), [properties]);\n }\n\n return {\n WithConfig,\n Config,\n useConfig\n };\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAEA,IAAMG,SAAS,GACX,SADEA,SAASA,CACVC,WAA4B;EAAA,OAC7B,UAAAC,aAAa,EAAI;IACb,OAAO,SAASC,SAASA,CAAAC,IAAA,EAAe;MAAA,IAAZC,QAAQ,GAAAD,IAAA,CAARC,QAAQ;MAChC,oBACIV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAACL,aAAa,QACTD,WAAW,EACXI,QACU,CAAC;IAExB,CAAC;EACL,CAAC;AAAA;AAOE,SAASG,2BAA2BA,CAAUC,IAAY,EAAE;EAC/D;AACJ;AACA;EACI,IAAMC,WAAW,GAAG,IAAAC,gCAAc,KAAAC,MAAA,CAAIH,IAAI,kBAAe,UAAAI,KAAA,EAAkB;IAAA,IAAfR,QAAQ,GAAAQ,KAAA,CAARR,QAAQ;IAChE,oBAAOV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAAAZ,MAAA,CAAAW,OAAA,CAAAQ,QAAA,QAAGT,QAAW,CAAC;EAC1B,CAAC,CAAC;;EAEF;AACJ;AACA;EACI,IAAMU,MAAM,GAAG,SAATA,MAAMA,CAAAC,KAAA,EAAoD;IAAA,IAA9CX,QAAQ,GAAAW,KAAA,CAARX,QAAQ;IACtB,oBAAOV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAACT,iBAAA,CAAAmB,OAAO;MAACC,SAAS,EAAER,WAAY;MAACS,IAAI,EAAEnB,SAAS,CAACK,QA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AU;IAClB,OAAO,IAAAsB,cAAO,EAAC;MAAA,OAAM,IAAAC,eAAQ,EAAmBvB,UAAU,CAAC;IAAA,GAAE,CAACA,UAAU,CAAC,CAAC;EAC9E;EAEA,OAAO;IACHI,UAAU,EAAVA,UAAU;IACVV,MAAM,EAANA,MAAM;IACNyB,SAAS,EAATA;EACJ,CAAC;AACL"}
|
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
6
|
var _utils = require("./utils");
|
|
8
|
-
|
|
9
7
|
Object.keys(_utils).forEach(function (key) {
|
|
10
8
|
if (key === "default" || key === "__esModule") return;
|
|
11
9
|
if (key in exports && exports[key] === _utils[key]) return;
|
|
@@ -16,9 +14,7 @@ Object.keys(_utils).forEach(function (key) {
|
|
|
16
14
|
}
|
|
17
15
|
});
|
|
18
16
|
});
|
|
19
|
-
|
|
20
17
|
var _Properties = require("./Properties");
|
|
21
|
-
|
|
22
18
|
Object.keys(_Properties).forEach(function (key) {
|
|
23
19
|
if (key === "default" || key === "__esModule") return;
|
|
24
20
|
if (key in exports && exports[key] === _Properties[key]) return;
|
|
@@ -28,4 +24,26 @@ Object.keys(_Properties).forEach(function (key) {
|
|
|
28
24
|
return _Properties[key];
|
|
29
25
|
}
|
|
30
26
|
});
|
|
27
|
+
});
|
|
28
|
+
var _useIdGenerator = require("./useIdGenerator");
|
|
29
|
+
Object.keys(_useIdGenerator).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _useIdGenerator[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function get() {
|
|
35
|
+
return _useIdGenerator[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
var _createConfigurableComponent = require("./createConfigurableComponent");
|
|
40
|
+
Object.keys(_createConfigurableComponent).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _createConfigurableComponent[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function get() {
|
|
46
|
+
return _createConfigurableComponent[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
31
49
|
});
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./utils\";\nexport * from \"./Properties\";\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_utils","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_Properties","_useIdGenerator","_createConfigurableComponent"],"sources":["index.ts"],"sourcesContent":["export * from \"./utils\";\nexport * from \"./Properties\";\nexport * from \"./useIdGenerator\";\nexport * from \"./createConfigurableComponent\";\n"],"mappings":";;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,MAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,MAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAT,MAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,WAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,WAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,WAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAC,WAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,eAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,eAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,eAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAE,eAAA,CAAAN,GAAA;IAAA;EAAA;AAAA;AACA,IAAAO,4BAAA,GAAAX,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAS,4BAAA,EAAAR,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAO,4BAAA,CAAAP,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAG,4BAAA,CAAAP,GAAA;IAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/react-properties",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.98511f29f1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,16 +10,17 @@
|
|
|
10
10
|
"author": "Webiny Ltd",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@babel/runtime": "7.
|
|
13
|
+
"@babel/runtime": "7.22.6",
|
|
14
14
|
"@types/react": "17.0.39",
|
|
15
15
|
"nanoid": "3.3.4",
|
|
16
16
|
"react": "17.0.2"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@testing-library/react": "
|
|
20
|
-
"@webiny/cli": "
|
|
21
|
-
"@webiny/project-utils": "
|
|
22
|
-
"@webiny/react-composition": "
|
|
19
|
+
"@testing-library/react": "12.1.5",
|
|
20
|
+
"@webiny/cli": "0.0.0-unstable.98511f29f1",
|
|
21
|
+
"@webiny/project-utils": "0.0.0-unstable.98511f29f1",
|
|
22
|
+
"@webiny/react-composition": "0.0.0-unstable.98511f29f1",
|
|
23
|
+
"prettier": "2.8.8"
|
|
23
24
|
},
|
|
24
25
|
"publishConfig": {
|
|
25
26
|
"access": "public",
|
|
@@ -29,5 +30,5 @@
|
|
|
29
30
|
"build": "yarn webiny run build",
|
|
30
31
|
"watch": "yarn webiny run watch"
|
|
31
32
|
},
|
|
32
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "98511f29f1e1fc7d03ed0d1379d0a0aeff6dae84"
|
|
33
34
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useIdGenerator(name: string): (...parts: string[]) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useIdGenerator = useIdGenerator;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _Properties = require("./Properties");
|
|
9
|
+
function useIdGenerator(name) {
|
|
10
|
+
var parentProperty = (0, _Properties.useParentProperty)();
|
|
11
|
+
return (0, _react.useCallback)(function () {
|
|
12
|
+
for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
13
|
+
parts[_key] = arguments[_key];
|
|
14
|
+
}
|
|
15
|
+
return [parentProperty === null || parentProperty === void 0 ? void 0 : parentProperty.id, name].concat(parts).filter(Boolean).join(":");
|
|
16
|
+
}, [name, parentProperty]);
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","require","_Properties","useIdGenerator","name","parentProperty","useParentProperty","useCallback","_len","arguments","length","parts","Array","_key","id","concat","filter","Boolean","join"],"sources":["useIdGenerator.ts"],"sourcesContent":["import { useCallback } from \"react\";\nimport { useParentProperty } from \"~/Properties\";\n\nexport function useIdGenerator(name: string) {\n const parentProperty = useParentProperty();\n\n return useCallback(\n (...parts: string[]) => {\n return [parentProperty?.id, name, ...parts].filter(Boolean).join(\":\");\n },\n [name, parentProperty]\n );\n}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAEO,SAASE,cAAcA,CAACC,IAAY,EAAE;EACzC,IAAMC,cAAc,GAAG,IAAAC,6BAAiB,EAAC,CAAC;EAE1C,OAAO,IAAAC,kBAAW,EACd,YAAwB;IAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAApBC,KAAK,OAAAC,KAAA,CAAAJ,IAAA,GAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;MAALF,KAAK,CAAAE,IAAA,IAAAJ,SAAA,CAAAI,IAAA;IAAA;IACL,OAAO,CAACR,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAES,EAAE,EAAEV,IAAI,EAAAW,MAAA,CAAKJ,KAAK,EAAEK,MAAM,CAACC,OAAO,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC;EACzE,CAAC,EACD,CAACd,IAAI,EAAEC,cAAc,CACzB,CAAC;AACL"}
|
package/utils.js
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.getUniqueId = getUniqueId;
|
|
9
8
|
exports.toObject = toObject;
|
|
10
|
-
|
|
11
9
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
12
|
-
|
|
13
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
14
|
-
|
|
15
11
|
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
16
|
-
|
|
17
12
|
var _nanoid = require("nanoid");
|
|
18
|
-
|
|
19
|
-
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef", 6);
|
|
20
|
-
|
|
13
|
+
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef");
|
|
21
14
|
function buildRoots(roots, properties) {
|
|
22
15
|
var obj = roots.reduce(function (acc, item) {
|
|
23
16
|
var isArray = item.array === true || roots.filter(function (r) {
|
|
@@ -27,12 +20,10 @@ function buildRoots(roots, properties) {
|
|
|
27
20
|
}, {});
|
|
28
21
|
roots.forEach(function (root) {
|
|
29
22
|
var isArray = root.array === true || Array.isArray(obj[root.name]);
|
|
30
|
-
|
|
31
23
|
if (root.value !== undefined) {
|
|
32
24
|
obj[root.name] = isArray ? [].concat((0, _toConsumableArray2.default)(obj[root.name]), [root.value]) : root.value;
|
|
33
25
|
return;
|
|
34
26
|
}
|
|
35
|
-
|
|
36
27
|
var nextRoots = properties.filter(function (p) {
|
|
37
28
|
return p.parent === root.id;
|
|
38
29
|
});
|
|
@@ -41,14 +32,13 @@ function buildRoots(roots, properties) {
|
|
|
41
32
|
});
|
|
42
33
|
return obj;
|
|
43
34
|
}
|
|
44
|
-
|
|
45
35
|
function toObject(properties) {
|
|
46
36
|
var roots = properties.filter(function (prop) {
|
|
47
37
|
return prop.parent === "";
|
|
48
38
|
});
|
|
49
39
|
return buildRoots(roots, properties);
|
|
50
40
|
}
|
|
51
|
-
|
|
52
41
|
function getUniqueId() {
|
|
53
|
-
|
|
42
|
+
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12;
|
|
43
|
+
return nanoid(length);
|
|
54
44
|
}
|
package/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["nanoid","customAlphabet","buildRoots","roots","properties","obj","reduce","acc","item","isArray","array","filter","r","name","length","forEach","root","Array","value","undefined","nextRoots","p","parent","id","toObject","prop","getUniqueId"],"sources":["utils.ts"],"sourcesContent":["import { customAlphabet } from \"nanoid\";\nconst nanoid = customAlphabet(\"1234567890abcdef\"
|
|
1
|
+
{"version":3,"names":["_nanoid","require","nanoid","customAlphabet","buildRoots","roots","properties","obj","reduce","acc","item","isArray","array","filter","r","name","length","_objectSpread3","default","_defineProperty2","forEach","root","Array","value","undefined","concat","_toConsumableArray2","nextRoots","p","parent","id","toObject","prop","getUniqueId","arguments"],"sources":["utils.ts"],"sourcesContent":["import { customAlphabet } from \"nanoid\";\nconst nanoid = customAlphabet(\"1234567890abcdef\");\nimport { Property } from \"./Properties\";\n\nfunction buildRoots(roots: Property[], properties: Property[]) {\n const obj: Record<string, unknown> = roots.reduce((acc, item) => {\n const isArray = item.array === true || roots.filter(r => r.name === item.name).length > 1;\n return { ...acc, [item.name]: isArray ? [] : {} };\n }, {});\n\n roots.forEach(root => {\n const isArray = root.array === true || Array.isArray(obj[root.name]);\n if (root.value !== undefined) {\n obj[root.name] = isArray ? [...(obj[root.name] as Array<any>), root.value] : root.value;\n return;\n }\n\n const nextRoots = properties.filter(p => p.parent === root.id);\n const value = buildRoots(nextRoots, properties);\n obj[root.name] = isArray ? [...(obj[root.name] as Property[]), value] : value;\n });\n\n return obj;\n}\n\nexport function toObject<T = unknown>(properties: Property[]): T {\n const roots = properties.filter(prop => prop.parent === \"\");\n return buildRoots(roots, properties) as T;\n}\n\nexport function getUniqueId(length = 12) {\n return nanoid(length);\n}\n"],"mappings":";;;;;;;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAMC,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CC,GAAG,CAACc,IAAI,CAACN,IAAI,CAAC,GAAGJ,OAAO,MAAAc,MAAA,KAAAC,mBAAA,CAAAR,OAAA,EAAQX,GAAG,CAACc,IAAI,CAACN,IAAI,CAAC,IAAiBQ,KAAK,KAAIA,KAAK;EACjF,CAAC,CAAC;EAEF,OAAOhB,GAAG;AACd;AAEO,SAASwB,QAAQA,CAAczB,UAAsB,EAAK;EAC7D,IAAMD,KAAK,GAAGC,UAAU,CAACO,MAAM,CAAC,UAAAmB,IAAI;IAAA,OAAIA,IAAI,CAACH,MAAM,KAAK,EAAE;EAAA,EAAC;EAC3D,OAAOzB,UAAU,CAACC,KAAK,EAAEC,UAAU,CAAC;AACxC;AAEO,SAAS2B,WAAWA,CAAA,EAAc;EAAA,IAAbjB,MAAM,GAAAkB,SAAA,CAAAlB,MAAA,QAAAkB,SAAA,QAAAV,SAAA,GAAAU,SAAA,MAAG,EAAE;EACnC,OAAOhC,MAAM,CAACc,MAAM,CAAC;AACzB"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { Property } from "../../../src/index";
|
|
3
|
-
interface AddWidgetProps {
|
|
4
|
-
name: string;
|
|
5
|
-
type: string;
|
|
6
|
-
}
|
|
7
|
-
declare const AddWidget: <T extends Record<string, unknown>>(props: T & AddWidgetProps) => JSX.Element;
|
|
8
|
-
export interface CardWidget extends Record<string, unknown> {
|
|
9
|
-
title: string;
|
|
10
|
-
description: string;
|
|
11
|
-
button: React.ReactElement;
|
|
12
|
-
}
|
|
13
|
-
interface DashboardConfig extends React.FC<unknown> {
|
|
14
|
-
AddWidget: typeof AddWidget;
|
|
15
|
-
DashboardRenderer: typeof DashboardRenderer;
|
|
16
|
-
}
|
|
17
|
-
export declare const DashboardConfig: DashboardConfig;
|
|
18
|
-
declare const DashboardRenderer: import("@webiny/react-composition").ComposableFC<unknown>;
|
|
19
|
-
interface DashboardViewProps {
|
|
20
|
-
onProperties(properties: Property[]): void;
|
|
21
|
-
}
|
|
22
|
-
export declare const App: React.FC<DashboardViewProps>;
|
|
23
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
interface SettingsGroupProps {
|
|
3
|
-
name: string;
|
|
4
|
-
title?: string;
|
|
5
|
-
icon?: string;
|
|
6
|
-
remove?: boolean;
|
|
7
|
-
replace?: string;
|
|
8
|
-
}
|
|
9
|
-
interface FormFieldProps extends Record<string, unknown> {
|
|
10
|
-
name: string;
|
|
11
|
-
component?: string;
|
|
12
|
-
after?: string;
|
|
13
|
-
remove?: boolean;
|
|
14
|
-
replace?: string;
|
|
15
|
-
}
|
|
16
|
-
declare const PageSettingsView: React.FC<import("./createConfigurableView").ViewProps>;
|
|
17
|
-
declare const PageSettingsConfig: React.FC<{}> & {
|
|
18
|
-
FormField: React.FC<FormFieldProps>;
|
|
19
|
-
SettingsGroup: React.FC<SettingsGroupProps>;
|
|
20
|
-
};
|
|
21
|
-
export { PageSettingsView, PageSettingsConfig };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { Property } from "../../../src/index";
|
|
3
|
-
export interface ViewProps {
|
|
4
|
-
onProperties?(properties: Property[]): void;
|
|
5
|
-
}
|
|
6
|
-
export declare function createConfigurableView(name: string): {
|
|
7
|
-
View: React.FC<ViewProps>;
|
|
8
|
-
Config: React.FC<{}>;
|
|
9
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/__tests__/setupEnv.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
declare const randomFillSync: any;
|
package/__tests__/utils.d.ts
DELETED
package/src/index.d.ts
DELETED