@webiny/react-properties 5.30.0-beta.0
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/LICENSE +21 -0
- package/Properties.js +201 -0
- package/Properties.js.map +1 -0
- package/README.md +65 -0
- package/__tests__/cases/dashboard/App.d.ts +23 -0
- package/__tests__/cases/dashboard/dashboard.test.d.ts +1 -0
- package/__tests__/cases/pbEditorSettings/PbEditorSettingsView.d.ts +21 -0
- package/__tests__/cases/pbEditorSettings/createConfigurableView.d.ts +9 -0
- package/__tests__/cases/pbEditorSettings/pbEditorSettings.test.d.ts +1 -0
- package/__tests__/properties.test.d.ts +1 -0
- package/__tests__/setupEnv.d.ts +1 -0
- package/__tests__/utils.d.ts +2 -0
- package/index.js +31 -0
- package/index.js.map +1 -0
- package/package.json +33 -0
- package/src/Properties.d.ts +38 -0
- package/src/index.d.ts +2 -0
- package/src/utils.d.ts +3 -0
- package/utils.js +54 -0
- package/utils.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Properties.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.Property = exports.Properties = void 0;
|
|
11
|
+
exports.useParentProperty = useParentProperty;
|
|
12
|
+
exports.useProperties = useProperties;
|
|
13
|
+
|
|
14
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
15
|
+
|
|
16
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
17
|
+
|
|
18
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
19
|
+
|
|
20
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
21
|
+
|
|
22
|
+
var _utils = require("./utils");
|
|
23
|
+
|
|
24
|
+
function removeByParent(id, properties) {
|
|
25
|
+
return properties.filter(function (prop) {
|
|
26
|
+
return prop.parent === id;
|
|
27
|
+
}).reduce(function (acc, item) {
|
|
28
|
+
return removeByParent(item.id, acc.filter(function (prop) {
|
|
29
|
+
return prop.id !== item.id;
|
|
30
|
+
}));
|
|
31
|
+
}, properties);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
35
|
+
|
|
36
|
+
var Properties = function Properties(_ref) {
|
|
37
|
+
var onChange = _ref.onChange,
|
|
38
|
+
children = _ref.children;
|
|
39
|
+
|
|
40
|
+
var _useState = (0, _react.useState)([]),
|
|
41
|
+
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
42
|
+
properties = _useState2[0],
|
|
43
|
+
setProperties = _useState2[1];
|
|
44
|
+
|
|
45
|
+
(0, _react.useEffect)(function () {
|
|
46
|
+
if (onChange) {
|
|
47
|
+
onChange(properties);
|
|
48
|
+
}
|
|
49
|
+
}, [properties]);
|
|
50
|
+
var context = (0, _react.useMemo)(function () {
|
|
51
|
+
return {
|
|
52
|
+
properties: properties,
|
|
53
|
+
getObject: function getObject() {
|
|
54
|
+
return (0, _utils.toObject)(properties);
|
|
55
|
+
},
|
|
56
|
+
addProperty: function addProperty(property) {
|
|
57
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
58
|
+
setProperties(function (properties) {
|
|
59
|
+
// If a property with this ID already exists, merge the two properties.
|
|
60
|
+
var index = properties.findIndex(function (prop) {
|
|
61
|
+
return prop.id === property.id;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (index > -1) {
|
|
65
|
+
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)));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (options.after) {
|
|
69
|
+
var _index = properties.findIndex(function (prop) {
|
|
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
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (options.before) {
|
|
79
|
+
var _index2 = properties.findIndex(function (prop) {
|
|
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
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [].concat((0, _toConsumableArray2.default)(properties), [property]);
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
removeProperty: function removeProperty(id) {
|
|
92
|
+
setProperties(function (properties) {
|
|
93
|
+
return removeByParent(id, properties.filter(function (prop) {
|
|
94
|
+
return prop.id !== id;
|
|
95
|
+
}));
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
replaceProperty: function replaceProperty(id, property) {
|
|
99
|
+
setProperties(function (properties) {
|
|
100
|
+
var toReplace = properties.findIndex(function (prop) {
|
|
101
|
+
return prop.id === id;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (toReplace > -1) {
|
|
105
|
+
// Replace the property and remove all remaining child properties.
|
|
106
|
+
return removeByParent(id, [].concat((0, _toConsumableArray2.default)(properties.slice(0, toReplace)), [property], (0, _toConsumableArray2.default)(properties.slice(toReplace + 1))));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return properties;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}, [properties]);
|
|
114
|
+
return /*#__PURE__*/_react.default.createElement(PropertiesContext.Provider, {
|
|
115
|
+
value: context
|
|
116
|
+
}, children);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
exports.Properties = Properties;
|
|
120
|
+
|
|
121
|
+
function useProperties() {
|
|
122
|
+
var properties = (0, _react.useContext)(PropertiesContext);
|
|
123
|
+
|
|
124
|
+
if (!properties) {
|
|
125
|
+
throw Error("Properties context provider is missing!");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return properties;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
var PropertyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
132
|
+
|
|
133
|
+
function useParentProperty() {
|
|
134
|
+
return (0, _react.useContext)(PropertyContext);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
var Property = function Property(_ref2) {
|
|
138
|
+
var id = _ref2.id,
|
|
139
|
+
name = _ref2.name,
|
|
140
|
+
value = _ref2.value,
|
|
141
|
+
children = _ref2.children,
|
|
142
|
+
_ref2$after = _ref2.after,
|
|
143
|
+
after = _ref2$after === void 0 ? undefined : _ref2$after,
|
|
144
|
+
_ref2$before = _ref2.before,
|
|
145
|
+
before = _ref2$before === void 0 ? undefined : _ref2$before,
|
|
146
|
+
_ref2$replace = _ref2.replace,
|
|
147
|
+
replace = _ref2$replace === void 0 ? undefined : _ref2$replace,
|
|
148
|
+
_ref2$remove = _ref2.remove,
|
|
149
|
+
remove = _ref2$remove === void 0 ? false : _ref2$remove,
|
|
150
|
+
_ref2$array = _ref2.array,
|
|
151
|
+
array = _ref2$array === void 0 ? false : _ref2$array;
|
|
152
|
+
var uniqueId = (0, _react.useMemo)(function () {
|
|
153
|
+
return id || (0, _utils.getUniqueId)();
|
|
154
|
+
}, []);
|
|
155
|
+
var parent = useParentProperty();
|
|
156
|
+
var properties = useProperties();
|
|
157
|
+
|
|
158
|
+
if (!properties) {
|
|
159
|
+
throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
var addProperty = properties.addProperty,
|
|
163
|
+
removeProperty = properties.removeProperty,
|
|
164
|
+
replaceProperty = properties.replaceProperty;
|
|
165
|
+
var property = {
|
|
166
|
+
id: uniqueId,
|
|
167
|
+
name: name,
|
|
168
|
+
value: value,
|
|
169
|
+
parent: parent ? parent.id : "",
|
|
170
|
+
array: array
|
|
171
|
+
};
|
|
172
|
+
(0, _react.useEffect)(function () {
|
|
173
|
+
if (remove) {
|
|
174
|
+
removeProperty(uniqueId);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (replace) {
|
|
179
|
+
replaceProperty(replace, property);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
addProperty(property, {
|
|
184
|
+
after: after,
|
|
185
|
+
before: before
|
|
186
|
+
});
|
|
187
|
+
return function () {
|
|
188
|
+
removeProperty(uniqueId);
|
|
189
|
+
};
|
|
190
|
+
}, []);
|
|
191
|
+
|
|
192
|
+
if (children) {
|
|
193
|
+
return /*#__PURE__*/_react.default.createElement(PropertyContext.Provider, {
|
|
194
|
+
value: property
|
|
195
|
+
}, children);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return null;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
exports.Property = Property;
|
|
@@ -0,0 +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,QA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,IAAI,CAAC/B,UAAL,EAAiB;IACb,MAAMiC,KAAK,CAAC,2DAAD,CAAX;EACH;;EAED,IAAQb,WAAR,GAAyDpB,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"}
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# React Properties
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/react-properties)
|
|
4
|
+
[](https://www.npmjs.com/package/@webiny/react-properties)
|
|
5
|
+
[](https://github.com/prettier/prettier)
|
|
6
|
+
[](http://makeapullrequest.com)
|
|
7
|
+
|
|
8
|
+
A tiny React properties framework, to build dynamic data objects using React components, which can be customized after initial creation. The usage is very similar to how you write XML data structures, but in this case you're using actual React.
|
|
9
|
+
|
|
10
|
+
## Basic Example
|
|
11
|
+
|
|
12
|
+
```jsx
|
|
13
|
+
import React, { useCallback } from "react";
|
|
14
|
+
import { Properties, Property, toObject } from "@webiny/react-properties";
|
|
15
|
+
|
|
16
|
+
const View = () => {
|
|
17
|
+
const onChange = useCallback(properties => {
|
|
18
|
+
console.log(toObject(properties));
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Properties onChange={onChange}>
|
|
23
|
+
<Property name={"group"}>
|
|
24
|
+
<Property name={"name"} value={"layout"} />
|
|
25
|
+
<Property name={"label"} value={"Layout"} />
|
|
26
|
+
<Property name={"toolbar"}>
|
|
27
|
+
<Property name={"name"} value={"basic"} />
|
|
28
|
+
</Property>
|
|
29
|
+
</Property>
|
|
30
|
+
<Property name={"group"}>
|
|
31
|
+
<Property name={"name"} value={"heroes"} />
|
|
32
|
+
<Property name={"label"} value={"Heroes"} />
|
|
33
|
+
<Property name={"toolbar"}>
|
|
34
|
+
<Property name={"name"} value={"heroes"} />
|
|
35
|
+
</Property>
|
|
36
|
+
</Property>
|
|
37
|
+
</Properties>
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Output:
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"group": [
|
|
47
|
+
{
|
|
48
|
+
"name": "layout",
|
|
49
|
+
"label": "Layout",
|
|
50
|
+
"toolbar": {
|
|
51
|
+
"name": "basic"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "heroes",
|
|
56
|
+
"label": "Heroes",
|
|
57
|
+
"toolbar": {
|
|
58
|
+
"name": "heroes"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For more examples, check out the test files.
|
|
@@ -0,0 +1,23 @@
|
|
|
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 {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
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 };
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare const randomFillSync: any;
|
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
|
|
9
|
+
Object.keys(_utils).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _utils[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _utils[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _Properties = require("./Properties");
|
|
21
|
+
|
|
22
|
+
Object.keys(_Properties).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _Properties[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function get() {
|
|
28
|
+
return _Properties[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
package/index.js.map
ADDED
|
@@ -0,0 +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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/react-properties",
|
|
3
|
+
"version": "5.30.0-beta.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/webiny/webiny-js.git"
|
|
8
|
+
},
|
|
9
|
+
"description": "Build pluggable data objects using React components.",
|
|
10
|
+
"author": "Webiny Ltd",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@babel/runtime": "7.18.6",
|
|
14
|
+
"@types/react": "16.14.2",
|
|
15
|
+
"nanoid": "3.3.4",
|
|
16
|
+
"react": "16.14.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@testing-library/react": "^12.1.5",
|
|
20
|
+
"@webiny/cli": "^5.30.0-beta.0",
|
|
21
|
+
"@webiny/project-utils": "^5.30.0-beta.0",
|
|
22
|
+
"@webiny/react-composition": "^5.30.0-beta.0"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"directory": "dist"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "yarn webiny run build",
|
|
30
|
+
"watch": "yarn webiny run watch"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "622d120f5637e3ca807b8bfc9ed2cd034ac85fb7"
|
|
33
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface Property {
|
|
3
|
+
id: string;
|
|
4
|
+
parent: string;
|
|
5
|
+
name: string;
|
|
6
|
+
value: unknown;
|
|
7
|
+
array?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface AddPropertyOptions {
|
|
10
|
+
after?: string;
|
|
11
|
+
before?: string;
|
|
12
|
+
}
|
|
13
|
+
interface PropertiesContext {
|
|
14
|
+
properties: Property[];
|
|
15
|
+
getObject<T = unknown>(): T;
|
|
16
|
+
addProperty(property: Property, options?: AddPropertyOptions): void;
|
|
17
|
+
removeProperty(id: string): void;
|
|
18
|
+
replaceProperty(id: string, property: Property): void;
|
|
19
|
+
}
|
|
20
|
+
declare const PropertiesContext: React.Context<PropertiesContext | undefined>;
|
|
21
|
+
interface PropertiesProps {
|
|
22
|
+
onChange?(properties: Property[]): void;
|
|
23
|
+
}
|
|
24
|
+
export declare const Properties: React.FC<PropertiesProps>;
|
|
25
|
+
export declare function useProperties(): PropertiesContext;
|
|
26
|
+
interface PropertyProps {
|
|
27
|
+
id?: string;
|
|
28
|
+
name: string;
|
|
29
|
+
value?: unknown;
|
|
30
|
+
array?: boolean;
|
|
31
|
+
after?: string;
|
|
32
|
+
before?: string;
|
|
33
|
+
replace?: string;
|
|
34
|
+
remove?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function useParentProperty(): Property | undefined;
|
|
37
|
+
export declare const Property: React.FC<PropertyProps>;
|
|
38
|
+
export {};
|
package/src/index.d.ts
ADDED
package/src/utils.d.ts
ADDED
package/utils.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.getUniqueId = getUniqueId;
|
|
9
|
+
exports.toObject = toObject;
|
|
10
|
+
|
|
11
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
12
|
+
|
|
13
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
14
|
+
|
|
15
|
+
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
16
|
+
|
|
17
|
+
var _nanoid = require("nanoid");
|
|
18
|
+
|
|
19
|
+
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef", 6);
|
|
20
|
+
|
|
21
|
+
function buildRoots(roots, properties) {
|
|
22
|
+
var obj = roots.reduce(function (acc, item) {
|
|
23
|
+
var isArray = item.array === true || roots.filter(function (r) {
|
|
24
|
+
return r.name === item.name;
|
|
25
|
+
}).length > 1;
|
|
26
|
+
return (0, _objectSpread3.default)((0, _objectSpread3.default)({}, acc), {}, (0, _defineProperty2.default)({}, item.name, isArray ? [] : {}));
|
|
27
|
+
}, {});
|
|
28
|
+
roots.forEach(function (root) {
|
|
29
|
+
var isArray = root.array === true || Array.isArray(obj[root.name]);
|
|
30
|
+
|
|
31
|
+
if (root.value !== undefined) {
|
|
32
|
+
obj[root.name] = isArray ? [].concat((0, _toConsumableArray2.default)(obj[root.name]), [root.value]) : root.value;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var nextRoots = properties.filter(function (p) {
|
|
37
|
+
return p.parent === root.id;
|
|
38
|
+
});
|
|
39
|
+
var value = buildRoots(nextRoots, properties);
|
|
40
|
+
obj[root.name] = isArray ? [].concat((0, _toConsumableArray2.default)(obj[root.name]), [value]) : value;
|
|
41
|
+
});
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function toObject(properties) {
|
|
46
|
+
var roots = properties.filter(function (prop) {
|
|
47
|
+
return prop.parent === "";
|
|
48
|
+
});
|
|
49
|
+
return buildRoots(roots, properties);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getUniqueId() {
|
|
53
|
+
return nanoid();
|
|
54
|
+
}
|
package/utils.js.map
ADDED
|
@@ -0,0 +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\", 6);\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() {\n return nanoid();\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;;AACA,IAAMA,MAAM,GAAG,IAAAC,sBAAA,EAAe,kBAAf,EAAmC,CAAnC,CAAf;;AAGA,SAASC,UAAT,CAAoBC,KAApB,EAAuCC,UAAvC,EAA+D;EAC3D,IAAMC,GAA4B,GAAGF,KAAK,CAACG,MAAN,CAAa,UAACC,GAAD,EAAMC,IAAN,EAAe;IAC7D,IAAMC,OAAO,GAAGD,IAAI,CAACE,KAAL,KAAe,IAAf,IAAuBP,KAAK,CAACQ,MAAN,CAAa,UAAAC,CAAC;MAAA,OAAIA,CAAC,CAACC,IAAF,KAAWL,IAAI,CAACK,IAApB;IAAA,CAAd,EAAwCC,MAAxC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