@webiny/react-properties 0.0.0-unstable.990c3ab1b6 → 0.0.0-unstable.99666aeb00
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 +86 -18
- 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 -0
- 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 -2
- 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
|
@@ -6,11 +6,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.Property = exports.Properties = void 0;
|
|
9
|
+
exports.useAncestor = useAncestor;
|
|
9
10
|
exports.useParentProperty = useParentProperty;
|
|
10
11
|
exports.useProperties = useProperties;
|
|
11
12
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
12
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
13
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
14
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
14
15
|
var _react = _interopRequireWildcard(require("react"));
|
|
15
16
|
var _utils = require("./utils");
|
|
16
17
|
function removeByParent(id, properties) {
|
|
@@ -22,6 +23,52 @@ function removeByParent(id, properties) {
|
|
|
22
23
|
}));
|
|
23
24
|
}, properties);
|
|
24
25
|
}
|
|
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
|
+
}
|
|
25
72
|
var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
26
73
|
var Properties = function Properties(_ref) {
|
|
27
74
|
var onChange = _ref.onChange,
|
|
@@ -44,28 +91,24 @@ var Properties = function Properties(_ref) {
|
|
|
44
91
|
addProperty: function addProperty(property) {
|
|
45
92
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
46
93
|
setProperties(function (properties) {
|
|
47
|
-
// If a property with this ID already exists, merge the two properties.
|
|
48
94
|
var index = properties.findIndex(function (prop) {
|
|
49
95
|
return prop.id === property.id;
|
|
50
96
|
});
|
|
51
97
|
if (index > -1) {
|
|
52
|
-
|
|
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;
|
|
53
106
|
}
|
|
54
107
|
if (options.after) {
|
|
55
|
-
|
|
56
|
-
return prop.id === options.after;
|
|
57
|
-
});
|
|
58
|
-
if (_index > -1) {
|
|
59
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
|
|
60
|
-
}
|
|
108
|
+
return putPropertyAfter(properties, property, options.after);
|
|
61
109
|
}
|
|
62
110
|
if (options.before) {
|
|
63
|
-
|
|
64
|
-
return prop.id === options.before;
|
|
65
|
-
});
|
|
66
|
-
if (_index2 > -1) {
|
|
67
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
|
|
68
|
-
}
|
|
111
|
+
return putPropertyBefore(properties, property, options.before);
|
|
69
112
|
}
|
|
70
113
|
return [].concat((0, _toConsumableArray2.default)(properties), [property]);
|
|
71
114
|
});
|
|
@@ -107,6 +150,26 @@ var PropertyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
|
107
150
|
function useParentProperty() {
|
|
108
151
|
return (0, _react.useContext)(PropertyContext);
|
|
109
152
|
}
|
|
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
|
+
}
|
|
110
173
|
var Property = function Property(_ref2) {
|
|
111
174
|
var id = _ref2.id,
|
|
112
175
|
name = _ref2.name,
|
|
@@ -121,11 +184,15 @@ var Property = function Property(_ref2) {
|
|
|
121
184
|
_ref2$remove = _ref2.remove,
|
|
122
185
|
remove = _ref2$remove === void 0 ? false : _ref2$remove,
|
|
123
186
|
_ref2$array = _ref2.array,
|
|
124
|
-
array = _ref2$array === void 0 ? false : _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;
|
|
125
192
|
var uniqueId = (0, _react.useMemo)(function () {
|
|
126
193
|
return id || (0, _utils.getUniqueId)();
|
|
127
194
|
}, []);
|
|
128
|
-
var
|
|
195
|
+
var parentProperty = useParentProperty();
|
|
129
196
|
var properties = useProperties();
|
|
130
197
|
if (!properties) {
|
|
131
198
|
throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
@@ -133,11 +200,12 @@ var Property = function Property(_ref2) {
|
|
|
133
200
|
var addProperty = properties.addProperty,
|
|
134
201
|
removeProperty = properties.removeProperty,
|
|
135
202
|
replaceProperty = properties.replaceProperty;
|
|
203
|
+
var parentId = parent ? parent : root ? "" : (parentProperty === null || parentProperty === void 0 ? void 0 : parentProperty.id) || "";
|
|
136
204
|
var property = {
|
|
137
205
|
id: uniqueId,
|
|
138
206
|
name: name,
|
|
139
207
|
value: value,
|
|
140
|
-
parent:
|
|
208
|
+
parent: parentId,
|
|
141
209
|
array: array
|
|
142
210
|
};
|
|
143
211
|
(0, _react.useEffect)(function () {
|
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,cAAc,CAACC,EAAU,EAAEC,UAAsB,EAAc;EACpE,OAAOA,UAAU,CACZC,MAAM,CAAC,UAAAC,IAAI;IAAA,OAAIA,IAAI,CAACC,MAAM,KAAKJ,EAAE;EAAA,EAAC,CAClCK,MAAM,CAAC,UAACC,GAAG,EAAEC,IAAI,EAAK;IACnB,OAAOR,cAAc,CACjBQ,IAAI,CAACP,EAAE,EACPM,GAAG,CAACJ,MAAM,CAAC,UAAAC,IAAI;MAAA,OAAIA,IAAI,CAACH,EAAE,KAAKO,IAAI,CAACP,EAAE;IAAA,EAAC,CAC1C;EACL,CAAC,EAAEC,UAAU,CAAC;AACtB;AAeA,IAAMO,iBAAiB,gBAAG,IAAAC,oBAAa,EAAgCC,SAAS,CAAC;AAM1E,IAAMC,UAAqC,GAAG,SAAxCA,UAAqC,OAA+B;EAAA,IAAzBC,QAAQ,QAARA,QAAQ;IAAEC,QAAQ,QAARA,QAAQ;EACtE,gBAAoC,IAAAC,eAAQ,EAAa,EAAE,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AAEtB,EAAE,EAAE2C,QAAQ;IAAEL,IAAI,EAAJA,IAAI;IAAEC,KAAK,EAALA,KAAK;IAAEnC,MAAM,EAAEA,MAAM,GAAGA,MAAM,CAACJ,EAAE,GAAG,EAAE;IAAE0C,KAAK,EAALA;EAAM,CAAC;EAEtF,IAAA1B,gBAAS,EAAC,YAAM;IACZ,IAAIyB,MAAM,EAAE;MACRZ,cAAc,CAACc,QAAQ,CAAC;MACxB;IACJ;IAEA,IAAIH,OAAO,EAAE;MACTV,eAAe,CAACU,OAAO,EAAElB,QAAQ,CAAC;MAClC;IACJ;IAEAD,WAAW,CAACC,QAAQ,EAAE;MAAEK,KAAK,EAALA,KAAK;MAAEC,MAAM,EAANA;IAAO,CAAC,CAAC;IAExC,OAAO,YAAM;MACTC,cAAc,CAACc,QAAQ,CAAC;IAC5B,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAI9B,QAAQ,EAAE;IACV,oBAAO,6BAAC,eAAe,CAAC,QAAQ;MAAC,KAAK,EAAES;IAAS,GAAET,QAAQ,CAA4B;EAC3F;EAEA,OAAO,IAAI;AACf,CAAC;AAAC"}
|
|
1
|
+
{"version":3,"names":["removeByParent","id","properties","filter","prop","parent","reduce","acc","item","putPropertyBefore","property","before","existingIndex","findIndex","existingProperty","newProperties","p","targetIndex","slice","putPropertyAfter","after","splice","removedProperty","mergeProperty","index","PropertiesContext","createContext","undefined","Properties","onChange","children","useState","setProperties","useEffect","context","useMemo","getObject","toObject","addProperty","options","removeProperty","replaceProperty","toReplace","useProperties","useContext","Error","PropertyContext","useParentProperty","useAncestor","params","matchOrGetAncestor","matchedProps","name","value","length","Object","keys","newParent","find","Property","replace","remove","array","root","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;AACA;AAUA,SAASA,cAAc,CAACC,EAAU,EAAEC,UAAsB,EAAc;EACpE,OAAOA,UAAU,CACZC,MAAM,CAAC,UAAAC,IAAI;IAAA,OAAIA,IAAI,CAACC,MAAM,KAAKJ,EAAE;EAAA,EAAC,CAClCK,MAAM,CAAC,UAACC,GAAG,EAAEC,IAAI,EAAK;IACnB,OAAOR,cAAc,CACjBQ,IAAI,CAACP,EAAE,EACPM,GAAG,CAACJ,MAAM,CAAC,UAAAC,IAAI;MAAA,OAAIA,IAAI,CAACH,EAAE,KAAKO,IAAI,CAACP,EAAE;IAAA,EAAC,CAC1C;EACL,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;IACJ;IAEA,IAAIJ,OAAO,EAAE;MACTnB,eAAe,CAACmB,OAAO,EAAElD,QAAQ,CAAC;MAClC;IACJ;IAEA4B,WAAW,CAAC5B,QAAQ,EAAE;MAAEU,KAAK,EAALA,KAAK;MAAET,MAAM,EAANA;IAAO,CAAC,CAAC;IAExC,OAAO,YAAM;MACT6B,cAAc,CAACwB,QAAQ,CAAC;IAC5B,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAIlC,QAAQ,EAAE;IACV,oBAAO,6BAAC,eAAe,CAAC,QAAQ;MAAC,KAAK,EAAEpB;IAAS,GAAEoB,QAAQ,CAA4B;EAC3F;EAEA,OAAO,IAAI;AACf,CAAC;AAAC"}
|
|
@@ -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":["createHOC","newChildren","BaseComponent","ConfigHOC","children","createConfigurableComponent","name","ConfigApply","makeComposable","Config","defaultContext","properties","ViewContext","React","createContext","WithConfig","onProperties","useState","setProperties","context","useEffect","stateUpdater","useConfig","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;AACA;AACA;AAEA,IAAMA,SAAS,GACX,SADEA,SAAS,CACVC,WAA4B;EAAA,OAC7B,UAAAC,aAAa,EAAI;IACb,OAAO,SAASC,SAAS,OAAe;MAAA,IAAZC,QAAQ,QAARA,QAAQ;MAChC,oBACI,6BAAC,aAAa,QACTH,WAAW,EACXG,QAAQ,CACG;IAExB,CAAC;EACL,CAAC;AAAA;AAOE,SAASC,2BAA2B,CAAUC,IAAY,EAAE;EAC/D;AACJ;AACA;EACI,IAAMC,WAAW,GAAG,IAAAC,gCAAc,YAAIF,IAAI,kBAAe,iBAAkB;IAAA,IAAfF,QAAQ,SAARA,QAAQ;IAChE,oBAAO,4DAAGA,QAAQ,CAAI;EAC1B,CAAC,CAAC;;EAEF;AACJ;AACA;EACI,IAAMK,MAAM,GAAG,SAATA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AAU;IACVN,MAAM,EAANA,MAAM;IACNa,SAAS,EAATA;EACJ,CAAC;AACL"}
|
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -24,4 +24,26 @@ Object.keys(_Properties).forEach(function (key) {
|
|
|
24
24
|
return _Properties[key];
|
|
25
25
|
}
|
|
26
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
|
+
});
|
|
27
49
|
});
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./utils\";\nexport * from \"./Properties\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./utils\";\nexport * from \"./Properties\";\nexport * from \"./useIdGenerator\";\nexport * from \"./createConfigurableComponent\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;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.99666aeb00",
|
|
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.20.
|
|
13
|
+
"@babel/runtime": "7.20.13",
|
|
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.99666aeb00",
|
|
21
|
+
"@webiny/project-utils": "0.0.0-unstable.99666aeb00",
|
|
22
|
+
"@webiny/react-composition": "0.0.0-unstable.99666aeb00",
|
|
23
|
+
"prettier": "2.8.3"
|
|
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": "99666aeb00056c56292eeb5dbb6aba7fda2439e2"
|
|
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":["useIdGenerator","name","parentProperty","useParentProperty","useCallback","parts","id","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;AACA;AAEO,SAASA,cAAc,CAACC,IAAY,EAAE;EACzC,IAAMC,cAAc,GAAG,IAAAC,6BAAiB,GAAE;EAE1C,OAAO,IAAAC,kBAAW,EACd,YAAwB;IAAA,kCAApBC,KAAK;MAALA,KAAK;IAAA;IACL,OAAO,CAACH,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAEI,EAAE,EAAEL,IAAI,SAAKI,KAAK,EAAEE,MAAM,CAACC,OAAO,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC;EACzE,CAAC,EACD,CAACR,IAAI,EAAEC,cAAc,CAAC,CACzB;AACL"}
|
package/utils.js
CHANGED
|
@@ -10,7 +10,7 @@ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers
|
|
|
10
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
11
|
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
12
12
|
var _nanoid = require("nanoid");
|
|
13
|
-
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef"
|
|
13
|
+
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef");
|
|
14
14
|
function buildRoots(roots, properties) {
|
|
15
15
|
var obj = roots.reduce(function (acc, item) {
|
|
16
16
|
var isArray = item.array === true || roots.filter(function (r) {
|
|
@@ -39,5 +39,6 @@ function toObject(properties) {
|
|
|
39
39
|
return buildRoots(roots, properties);
|
|
40
40
|
}
|
|
41
41
|
function getUniqueId() {
|
|
42
|
-
|
|
42
|
+
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12;
|
|
43
|
+
return nanoid(length);
|
|
43
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","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\");\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;AACA,IAAMA,MAAM,GAAG,IAAAC,sBAAc,EAAC,kBAAkB,CAAC;AAGjD,SAASC,UAAU,CAACC,KAAiB,EAAEC,UAAsB,EAAE;EAC3D,IAAMC,GAA4B,GAAGF,KAAK,CAACG,MAAM,CAAC,UAACC,GAAG,EAAEC,IAAI,EAAK;IAC7D,IAAMC,OAAO,GAAGD,IAAI,CAACE,KAAK,KAAK,IAAI,IAAIP,KAAK,CAACQ,MAAM,CAAC,UAAAC,CAAC;MAAA,OAAIA,CAAC,CAACC,IAAI,KAAKL,IAAI,CAACK,IAAI;IAAA,EAAC,CAACC,MAAM,GAAG,CAAC;IACzF,mEAAYP,GAAG,yCAAGC,IAAI,CAACK,IAAI,EAAGJ,OAAO,GAAG,EAAE,GAAG,CAAC,CAAC;EACnD,CAAC,EAAE,CAAC,CAAC,CAAC;EAENN,KAAK,CAACY,OAAO,CAAC,UAAAC,IAAI,EAAI;IAClB,IAAMP,OAAO,GAAGO,IAAI,CAACN,KAAK,KAAK,IAAI,IAAIO,KAAK,CAACR,OAAO,CAACJ,GAAG,CAACW,IAAI,CAACH,IAAI,CAAC,CAAC;IACpE,IAAIG,IAAI,CAACE,KAAK,KAAKC,SAAS,EAAE;MAC1Bd,GAAG,CAACW,IAAI,CAACH,IAAI,CAAC,GAAGJ,OAAO,8CAAQJ,GAAG,CAACW,IAAI,CAACH,IAAI,CAAC,IAAiBG,IAAI,CAACE,KAAK,KAAIF,IAAI,CAACE,KAAK;MACvF;IACJ;IAEA,IAAME,SAAS,GAAGhB,UAAU,CAACO,MAAM,CAAC,UAAAU,CAAC;MAAA,OAAIA,CAAC,CAACC,MAAM,KAAKN,IAAI,CAACO,EAAE;IAAA,EAAC;IAC9D,IAAML,KAAK,GAAGhB,UAAU,CAACkB,SAAS,EAAEhB,UAAU,CAAC;IAC/CC,GAAG,CAACW,IAAI,CAACH,IAAI,CAAC,GAAGJ,OAAO,8CAAQJ,GAAG,CAACW,IAAI,CAACH,IAAI,CAAC,IAAiBK,KAAK,KAAIA,KAAK;EACjF,CAAC,CAAC;EAEF,OAAOb,GAAG;AACd;AAEO,SAASmB,QAAQ,CAAcpB,UAAsB,EAAK;EAC7D,IAAMD,KAAK,GAAGC,UAAU,CAACO,MAAM,CAAC,UAAAc,IAAI;IAAA,OAAIA,IAAI,CAACH,MAAM,KAAK,EAAE;EAAA,EAAC;EAC3D,OAAOpB,UAAU,CAACC,KAAK,EAAEC,UAAU,CAAC;AACxC;AAEO,SAASsB,WAAW,GAAc;EAAA,IAAbZ,MAAM,uEAAG,EAAE;EACnC,OAAOd,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