@webiny/react-properties 5.34.8-beta.1 → 5.35.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/Properties.d.ts CHANGED
@@ -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 {
package/Properties.js CHANGED
@@ -1,9 +1,7 @@
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
  });
@@ -11,17 +9,11 @@ exports.Property = exports.Properties = void 0;
11
9
  exports.useAncestor = useAncestor;
12
10
  exports.useParentProperty = useParentProperty;
13
11
  exports.useProperties = useProperties;
14
-
15
12
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
-
17
13
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
18
-
19
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
20
-
21
15
  var _react = _interopRequireWildcard(require("react"));
22
-
23
16
  var _utils = require("./utils");
24
-
25
17
  function removeByParent(id, properties) {
26
18
  return properties.filter(function (prop) {
27
19
  return prop.parent === id;
@@ -31,18 +23,14 @@ function removeByParent(id, properties) {
31
23
  }));
32
24
  }, properties);
33
25
  }
34
-
35
26
  var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
36
-
37
27
  var Properties = function Properties(_ref) {
38
28
  var onChange = _ref.onChange,
39
- children = _ref.children;
40
-
29
+ children = _ref.children;
41
30
  var _useState = (0, _react.useState)([]),
42
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
43
- properties = _useState2[0],
44
- setProperties = _useState2[1];
45
-
31
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
32
+ properties = _useState2[0],
33
+ setProperties = _useState2[1];
46
34
  (0, _react.useEffect)(function () {
47
35
  if (onChange) {
48
36
  onChange(properties);
@@ -61,31 +49,25 @@ var Properties = function Properties(_ref) {
61
49
  var index = properties.findIndex(function (prop) {
62
50
  return prop.id === property.id;
63
51
  });
64
-
65
52
  if (index > -1) {
66
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)));
67
54
  }
68
-
69
55
  if (options.after) {
70
56
  var _index = properties.findIndex(function (prop) {
71
57
  return prop.id === options.after;
72
58
  });
73
-
74
59
  if (_index > -1) {
75
60
  return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
76
61
  }
77
62
  }
78
-
79
63
  if (options.before) {
80
64
  var _index2 = properties.findIndex(function (prop) {
81
65
  return prop.id === options.before;
82
66
  });
83
-
84
67
  if (_index2 > -1) {
85
68
  return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
86
69
  }
87
70
  }
88
-
89
71
  return [].concat((0, _toConsumableArray2.default)(properties), [property]);
90
72
  });
91
73
  },
@@ -101,12 +83,10 @@ var Properties = function Properties(_ref) {
101
83
  var toReplace = properties.findIndex(function (prop) {
102
84
  return prop.id === id;
103
85
  });
104
-
105
86
  if (toReplace > -1) {
106
87
  // Replace the property and remove all remaining child properties.
107
88
  return removeByParent(id, [].concat((0, _toConsumableArray2.default)(properties.slice(0, toReplace)), [property], (0, _toConsumableArray2.default)(properties.slice(toReplace + 1))));
108
89
  }
109
-
110
90
  return properties;
111
91
  });
112
92
  }
@@ -116,83 +96,68 @@ var Properties = function Properties(_ref) {
116
96
  value: context
117
97
  }, children);
118
98
  };
119
-
120
99
  exports.Properties = Properties;
121
-
122
100
  function useProperties() {
123
101
  var properties = (0, _react.useContext)(PropertiesContext);
124
-
125
102
  if (!properties) {
126
103
  throw Error("Properties context provider is missing!");
127
104
  }
128
-
129
105
  return properties;
130
106
  }
131
-
132
107
  var PropertyContext = /*#__PURE__*/(0, _react.createContext)(undefined);
133
-
134
108
  function useParentProperty() {
135
109
  return (0, _react.useContext)(PropertyContext);
136
110
  }
137
-
138
111
  function useAncestor(params) {
139
112
  var property = useParentProperty();
140
-
141
113
  var _useProperties = useProperties(),
142
- properties = _useProperties.properties;
143
-
114
+ properties = _useProperties.properties;
144
115
  var matchOrGetAncestor = function matchOrGetAncestor(property, params) {
145
116
  var matchedProps = properties.filter(function (prop) {
146
117
  return prop.parent === property.id;
147
118
  }).filter(function (prop) {
148
119
  return prop.name in params && prop.value === params[prop.name];
149
120
  });
150
-
151
121
  if (matchedProps.length === Object.keys(params).length) {
152
122
  return property;
153
123
  }
154
-
155
124
  var newParent = property.parent ? properties.find(function (prop) {
156
125
  return prop.id === property.parent;
157
126
  }) : undefined;
158
127
  return newParent ? matchOrGetAncestor(newParent, params) : undefined;
159
128
  };
160
-
161
129
  return property ? matchOrGetAncestor(property, params) : undefined;
162
130
  }
163
-
164
131
  var Property = function Property(_ref2) {
165
132
  var id = _ref2.id,
166
- name = _ref2.name,
167
- value = _ref2.value,
168
- children = _ref2.children,
169
- _ref2$after = _ref2.after,
170
- after = _ref2$after === void 0 ? undefined : _ref2$after,
171
- _ref2$before = _ref2.before,
172
- before = _ref2$before === void 0 ? undefined : _ref2$before,
173
- _ref2$replace = _ref2.replace,
174
- replace = _ref2$replace === void 0 ? undefined : _ref2$replace,
175
- _ref2$remove = _ref2.remove,
176
- remove = _ref2$remove === void 0 ? false : _ref2$remove,
177
- _ref2$array = _ref2.array,
178
- array = _ref2$array === void 0 ? false : _ref2$array,
179
- _ref2$root = _ref2.root,
180
- root = _ref2$root === void 0 ? false : _ref2$root,
181
- _ref2$parent = _ref2.parent,
182
- parent = _ref2$parent === void 0 ? undefined : _ref2$parent;
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;
183
150
  var uniqueId = (0, _react.useMemo)(function () {
184
151
  return id || (0, _utils.getUniqueId)();
185
152
  }, []);
186
153
  var parentProperty = useParentProperty();
187
154
  var properties = useProperties();
188
-
189
155
  if (!properties) {
190
156
  throw Error("<Properties> provider is missing higher in the hierarchy!");
191
157
  }
192
-
193
158
  var addProperty = properties.addProperty,
194
- removeProperty = properties.removeProperty,
195
- replaceProperty = properties.replaceProperty;
159
+ removeProperty = properties.removeProperty,
160
+ replaceProperty = properties.replaceProperty;
196
161
  var parentId = parent ? parent : root ? "" : (parentProperty === null || parentProperty === void 0 ? void 0 : parentProperty.id) || "";
197
162
  var property = {
198
163
  id: uniqueId,
@@ -206,12 +171,10 @@ var Property = function Property(_ref2) {
206
171
  removeProperty(uniqueId);
207
172
  return;
208
173
  }
209
-
210
174
  if (replace) {
211
175
  replaceProperty(replace, property);
212
176
  return;
213
177
  }
214
-
215
178
  addProperty(property, {
216
179
  after: after,
217
180
  before: before
@@ -220,14 +183,11 @@ var Property = function Property(_ref2) {
220
183
  removeProperty(uniqueId);
221
184
  };
222
185
  }, []);
223
-
224
186
  if (children) {
225
187
  return /*#__PURE__*/_react.default.createElement(PropertyContext.Provider, {
226
188
  value: property
227
189
  }, children);
228
190
  }
229
-
230
191
  return null;
231
192
  };
232
-
233
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","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={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,SAA7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nC,UAAU,GAAG+B,aAAa,EAAhC;;EAEA,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,IAAM0B,QAAQ,GAAGpD,MAAM,GAAGA,MAAH,GAAYgD,IAAI,GAAG,EAAH,GAAQ,CAAAG,cAAc,SAAd,IAAAA,cAAc,WAAd,YAAAA,cAAc,CAAEvD,EAAhB,KAAsB,EAArE;EACA,IAAMsB,QAAQ,GAAG;IAAEtB,EAAE,EAAEqD,QAAN;IAAgBZ,IAAI,EAAJA,IAAhB;IAAsBC,KAAK,EAALA,KAAtB;IAA6BtC,MAAM,EAAEoD,QAArC;IAA+CL,KAAK,EAALA;EAA/C,CAAjB;EAEA,IAAAnC,gBAAA,EAAU,YAAM;IACZ,IAAIkC,MAAJ,EAAY;MACRrB,cAAc,CAACwB,QAAD,CAAd;MACA;IACH;;IAED,IAAIJ,OAAJ,EAAa;MACTnB,eAAe,CAACmB,OAAD,EAAU3B,QAAV,CAAf;MACA;IACH;;IAEDD,WAAW,CAACC,QAAD,EAAW;MAAEK,KAAK,EAALA,KAAF;MAASC,MAAM,EAANA;IAAT,CAAX,CAAX;IAEA,OAAO,YAAM;MACTC,cAAc,CAACwB,QAAD,CAAd;IACH,CAFD;EAGH,CAhBD,EAgBG,EAhBH;;EAkBA,IAAIxC,QAAJ,EAAc;IACV,oBAAO,6BAAC,eAAD,CAAiB,QAAjB;MAA0B,KAAK,EAAES;IAAjC,GAA4CT,QAA5C,CAAP;EACH;;EAED,OAAO,IAAP;AACH,CAhDM"}
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={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,CAAC;IAAA;IAArDb,UAAU;IAAEc,aAAa;EAEhC,IAAAC,gBAAS,EAAC,YAAM;IACZ,IAAIJ,QAAQ,EAAE;MACVA,QAAQ,CA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tC,MAAM,EAAEoD,QAAQ;IAAEL,KAAK,EAALA;EAAM,CAAC;EAEvE,IAAAnC,gBAAS,EAAC,YAAM;IACZ,IAAIkC,MAAM,EAAE;MACRrB,cAAc,CAACwB,QAAQ,CAAC;MACxB;IACJ;IAEA,IAAIJ,OAAO,EAAE;MACTnB,eAAe,CAACmB,OAAO,EAAE3B,QAAQ,CAAC;MAClC;IACJ;IAEAD,WAAW,CAACC,QAAQ,EAAE;MAAEK,KAAK,EAALA,KAAK;MAAEC,MAAM,EAANA;IAAO,CAAC,CAAC;IAExC,OAAO,YAAM;MACTC,cAAc,CAACwB,QAAQ,CAAC;IAC5B,CAAC;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": "5.34.8-beta.1",
3
+ "version": "5.35.0-beta.0",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,16 +10,16 @@
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": "^5.34.8-beta.1",
21
- "@webiny/project-utils": "^5.34.8-beta.1",
22
- "@webiny/react-composition": "^5.34.8-beta.1",
20
+ "@webiny/cli": "^5.35.0-beta.0",
21
+ "@webiny/project-utils": "^5.35.0-beta.0",
22
+ "@webiny/react-composition": "^5.35.0-beta.0",
23
23
  "prettier": "^2.3.2"
24
24
  },
25
25
  "publishConfig": {
@@ -30,5 +30,5 @@
30
30
  "build": "yarn webiny run build",
31
31
  "watch": "yarn webiny run watch"
32
32
  },
33
- "gitHead": "6e77eebaac687279fe82ea04f667b7e84214b96a"
33
+ "gitHead": "8acc9e8892842cabb3980ce0b6432fde55968d5b"
34
34
  }
package/utils.d.ts CHANGED
@@ -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,CAA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