@webiny/react-properties 0.0.0-unstable.40876133bb → 0.0.0-unstable.496cf268ac

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.
@@ -3,7 +3,7 @@ export interface Property {
3
3
  id: string;
4
4
  parent: string;
5
5
  name: string;
6
- value: unknown;
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
13
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
17
-
18
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
19
-
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,14 @@ function removeByParent(id, properties) {
30
23
  }));
31
24
  }, properties);
32
25
  }
33
-
34
26
  var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
35
-
36
27
  var Properties = function Properties(_ref) {
37
28
  var onChange = _ref.onChange,
38
- children = _ref.children;
39
-
29
+ children = _ref.children;
40
30
  var _useState = (0, _react.useState)([]),
41
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
42
- properties = _useState2[0],
43
- setProperties = _useState2[1];
44
-
31
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
32
+ properties = _useState2[0],
33
+ setProperties = _useState2[1];
45
34
  (0, _react.useEffect)(function () {
46
35
  if (onChange) {
47
36
  onChange(properties);
@@ -60,31 +49,25 @@ var Properties = function Properties(_ref) {
60
49
  var index = properties.findIndex(function (prop) {
61
50
  return prop.id === property.id;
62
51
  });
63
-
64
52
  if (index > -1) {
65
53
  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
54
  }
67
-
68
55
  if (options.after) {
69
56
  var _index = properties.findIndex(function (prop) {
70
57
  return prop.id === options.after;
71
58
  });
72
-
73
59
  if (_index > -1) {
74
60
  return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
75
61
  }
76
62
  }
77
-
78
63
  if (options.before) {
79
64
  var _index2 = properties.findIndex(function (prop) {
80
65
  return prop.id === options.before;
81
66
  });
82
-
83
67
  if (_index2 > -1) {
84
68
  return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
85
69
  }
86
70
  }
87
-
88
71
  return [].concat((0, _toConsumableArray2.default)(properties), [property]);
89
72
  });
90
73
  },
@@ -100,12 +83,10 @@ var Properties = function Properties(_ref) {
100
83
  var toReplace = properties.findIndex(function (prop) {
101
84
  return prop.id === id;
102
85
  });
103
-
104
86
  if (toReplace > -1) {
105
87
  // Replace the property and remove all remaining child properties.
106
88
  return removeByParent(id, [].concat((0, _toConsumableArray2.default)(properties.slice(0, toReplace)), [property], (0, _toConsumableArray2.default)(properties.slice(toReplace + 1))));
107
89
  }
108
-
109
90
  return properties;
110
91
  });
111
92
  }
@@ -115,58 +96,74 @@ var Properties = function Properties(_ref) {
115
96
  value: context
116
97
  }, children);
117
98
  };
118
-
119
99
  exports.Properties = Properties;
120
-
121
100
  function useProperties() {
122
101
  var properties = (0, _react.useContext)(PropertiesContext);
123
-
124
102
  if (!properties) {
125
103
  throw Error("Properties context provider is missing!");
126
104
  }
127
-
128
105
  return properties;
129
106
  }
130
-
131
107
  var PropertyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
132
-
133
108
  function useParentProperty() {
134
109
  return (0, _react.useContext)(PropertyContext);
135
110
  }
136
-
111
+ function useAncestor(params) {
112
+ var property = useParentProperty();
113
+ var _useProperties = useProperties(),
114
+ properties = _useProperties.properties;
115
+ var matchOrGetAncestor = function matchOrGetAncestor(property, params) {
116
+ var matchedProps = properties.filter(function (prop) {
117
+ return prop.parent === property.id;
118
+ }).filter(function (prop) {
119
+ return prop.name in params && prop.value === params[prop.name];
120
+ });
121
+ if (matchedProps.length === Object.keys(params).length) {
122
+ return property;
123
+ }
124
+ var newParent = property.parent ? properties.find(function (prop) {
125
+ return prop.id === property.parent;
126
+ }) : undefined;
127
+ return newParent ? matchOrGetAncestor(newParent, params) : undefined;
128
+ };
129
+ return property ? matchOrGetAncestor(property, params) : undefined;
130
+ }
137
131
  var Property = function Property(_ref2) {
138
132
  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;
133
+ name = _ref2.name,
134
+ value = _ref2.value,
135
+ children = _ref2.children,
136
+ _ref2$after = _ref2.after,
137
+ after = _ref2$after === void 0 ? undefined : _ref2$after,
138
+ _ref2$before = _ref2.before,
139
+ before = _ref2$before === void 0 ? undefined : _ref2$before,
140
+ _ref2$replace = _ref2.replace,
141
+ replace = _ref2$replace === void 0 ? undefined : _ref2$replace,
142
+ _ref2$remove = _ref2.remove,
143
+ remove = _ref2$remove === void 0 ? false : _ref2$remove,
144
+ _ref2$array = _ref2.array,
145
+ array = _ref2$array === void 0 ? false : _ref2$array,
146
+ _ref2$root = _ref2.root,
147
+ root = _ref2$root === void 0 ? false : _ref2$root,
148
+ _ref2$parent = _ref2.parent,
149
+ parent = _ref2$parent === void 0 ? undefined : _ref2$parent;
152
150
  var uniqueId = (0, _react.useMemo)(function () {
153
151
  return id || (0, _utils.getUniqueId)();
154
152
  }, []);
155
- var parent = useParentProperty();
153
+ var parentProperty = useParentProperty();
156
154
  var properties = useProperties();
157
-
158
155
  if (!properties) {
159
156
  throw Error("<Properties> provider is missing higher in the hierarchy!");
160
157
  }
161
-
162
158
  var addProperty = properties.addProperty,
163
- removeProperty = properties.removeProperty,
164
- replaceProperty = properties.replaceProperty;
159
+ removeProperty = properties.removeProperty,
160
+ replaceProperty = properties.replaceProperty;
161
+ var parentId = parent ? parent : root ? "" : (parentProperty === null || parentProperty === void 0 ? void 0 : parentProperty.id) || "";
165
162
  var property = {
166
163
  id: uniqueId,
167
164
  name: name,
168
165
  value: value,
169
- parent: parent ? parent.id : "",
166
+ parent: parentId,
170
167
  array: array
171
168
  };
172
169
  (0, _react.useEffect)(function () {
@@ -174,12 +171,10 @@ var Property = function Property(_ref2) {
174
171
  removeProperty(uniqueId);
175
172
  return;
176
173
  }
177
-
178
174
  if (replace) {
179
175
  replaceProperty(replace, property);
180
176
  return;
181
177
  }
182
-
183
178
  addProperty(property, {
184
179
  after: after,
185
180
  before: before
@@ -188,14 +183,11 @@ var Property = function Property(_ref2) {
188
183
  removeProperty(uniqueId);
189
184
  };
190
185
  }, []);
191
-
192
186
  if (children) {
193
187
  return /*#__PURE__*/_react.default.createElement(PropertyContext.Provider, {
194
188
  value: property
195
189
  }, children);
196
190
  }
197
-
198
191
  return null;
199
192
  };
200
-
201
193
  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,IAAmBc,aAAnB;;EAEA,IAAAC,gBAAA,EAAU,YAAM;IACZ,IAAIJ,QAAJ,EAAc;MACVA,QAAQ,CAACX,UAAD,CAAR;IACH;EACJ,CAJD,EAIG,CAACA,UAAD,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,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":["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","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\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 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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EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAIxC,QAAQ,EAAE;IACV,oBAAO,6BAAC,eAAe,CAAC,QAAQ;MAAC,KAAK,EAAES;IAAS,GAAET,QAAQ,CAA4B;EAC3F;EAEA,OAAO,IAAI;AACf,CAAC;AAAC"}
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;
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\";\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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/react-properties",
3
- "version": "0.0.0-unstable.40876133bb",
3
+ "version": "0.0.0-unstable.496cf268ac",
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.19.0",
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
19
  "@testing-library/react": "^12.1.5",
20
- "@webiny/cli": "^0.0.0-unstable.40876133bb",
21
- "@webiny/project-utils": "^0.0.0-unstable.40876133bb",
22
- "@webiny/react-composition": "^0.0.0-unstable.40876133bb"
20
+ "@webiny/cli": "^0.0.0-unstable.496cf268ac",
21
+ "@webiny/project-utils": "^0.0.0-unstable.496cf268ac",
22
+ "@webiny/react-composition": "^0.0.0-unstable.496cf268ac",
23
+ "prettier": "^2.3.2"
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": "f33811072795d25c5787ae39808e75e3312fb247"
33
+ "gitHead": "496cf268acd5b092c860b62d75c7d765c9472043"
33
34
  }
@@ -1,3 +1,3 @@
1
1
  import { Property } from "./Properties";
2
2
  export declare function toObject<T = unknown>(properties: Property[]): T;
3
- export declare function getUniqueId(): string;
3
+ export declare function getUniqueId(length?: number): string;
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
- return nanoid();
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\", 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,GAAiD,CAAxF;IACA,mEAAYP,GAAZ,yCAAkBC,IAAI,CAACK,IAAvB,EAA8BJ,OAAO,GAAG,EAAH,GAAQ,EAA7C;EACH,CAHoC,EAGlC,EAHkC,CAArC;EAKAN,KAAK,CAACY,OAAN,CAAc,UAAAC,IAAI,EAAI;IAClB,IAAMP,OAAO,GAAGO,IAAI,CAACN,KAAL,KAAe,IAAf,IAAuBO,KAAK,CAACR,OAAN,CAAcJ,GAAG,CAACW,IAAI,CAACH,IAAN,CAAjB,CAAvC;;IACA,IAAIG,IAAI,CAACE,KAAL,KAAeC,SAAnB,EAA8B;MAC1Bd,GAAG,CAACW,IAAI,CAACH,IAAN,CAAH,GAAiBJ,OAAO,8CAAQJ,GAAG,CAACW,IAAI,CAACH,IAAN,CAAX,IAAuCG,IAAI,CAACE,KAA5C,KAAqDF,IAAI,CAACE,KAAlF;MACA;IACH;;IAED,IAAME,SAAS,GAAGhB,UAAU,CAACO,MAAX,CAAkB,UAAAU,CAAC;MAAA,OAAIA,CAAC,CAACC,MAAF,KAAaN,IAAI,CAACO,EAAtB;IAAA,CAAnB,CAAlB;IACA,IAAML,KAAK,GAAGhB,UAAU,CAACkB,SAAD,EAAYhB,UAAZ,CAAxB;IACAC,GAAG,CAACW,IAAI,CAACH,IAAN,CAAH,GAAiBJ,OAAO,8CAAQJ,GAAG,CAACW,IAAI,CAACH,IAAN,CAAX,IAAuCK,KAAvC,KAAgDA,KAAxE;EACH,CAVD;EAYA,OAAOb,GAAP;AACH;;AAEM,SAASmB,QAAT,CAA+BpB,UAA/B,EAA0D;EAC7D,IAAMD,KAAK,GAAGC,UAAU,CAACO,MAAX,CAAkB,UAAAc,IAAI;IAAA,OAAIA,IAAI,CAACH,MAAL,KAAgB,EAApB;EAAA,CAAtB,CAAd;EACA,OAAOpB,UAAU,CAACC,KAAD,EAAQC,UAAR,CAAjB;AACH;;AAEM,SAASsB,WAAT,GAAuB;EAC1B,OAAO1B,MAAM,EAAb;AACH"}
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
- declare const randomFillSync: any;
@@ -1,2 +0,0 @@
1
- /// <reference types="jest" />
2
- export declare function getLastCall(callable: jest.Mock): any;
File without changes