@webiny/react-properties 0.0.0-unstable.615a930a68 → 0.0.0-unstable.61c048f412
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 +58 -0
- package/Properties.js +196 -157
- package/Properties.js.map +1 -1
- package/README.md +7 -61
- package/createConfigurableComponent.d.ts +15 -0
- package/createConfigurableComponent.js +107 -0
- package/createConfigurableComponent.js.map +1 -0
- package/index.d.ts +5 -0
- package/index.js +6 -30
- package/index.js.map +1 -1
- package/package.json +12 -14
- package/useDebugConfig.d.ts +7 -0
- package/useDebugConfig.js +19 -0
- package/useDebugConfig.js.map +1 -0
- package/useIdGenerator.d.ts +1 -0
- package/useIdGenerator.js +14 -0
- package/useIdGenerator.js.map +1 -0
- package/utils.d.ts +3 -0
- package/utils.js +36 -43
- package/utils.js.map +1 -1
- package/__tests__/cases/dashboard/App.d.ts +0 -23
- package/__tests__/cases/dashboard/dashboard.test.d.ts +0 -1
- package/__tests__/cases/pbEditorSettings/PbEditorSettingsView.d.ts +0 -21
- package/__tests__/cases/pbEditorSettings/createConfigurableView.d.ts +0 -9
- package/__tests__/cases/pbEditorSettings/pbEditorSettings.test.d.ts +0 -1
- package/__tests__/properties.test.d.ts +0 -1
- package/__tests__/setupEnv.d.ts +0 -1
- package/__tests__/utils.d.ts +0 -2
- package/src/Properties.d.ts +0 -38
- package/src/index.d.ts +0 -2
- package/src/utils.d.ts +0 -3
package/Properties.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface ConnectToPropertiesProps {
|
|
3
|
+
name: string;
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export declare const ConnectToProperties: ({ name, children }: ConnectToPropertiesProps) => React.JSX.Element;
|
|
7
|
+
export interface Property {
|
|
8
|
+
id: string;
|
|
9
|
+
parent: string;
|
|
10
|
+
name: string;
|
|
11
|
+
value?: unknown;
|
|
12
|
+
array?: boolean;
|
|
13
|
+
$isFirst?: boolean;
|
|
14
|
+
$isLast?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface AddPropertyOptions {
|
|
17
|
+
after?: string;
|
|
18
|
+
before?: string;
|
|
19
|
+
}
|
|
20
|
+
interface PropertiesContext {
|
|
21
|
+
name?: string;
|
|
22
|
+
properties: Property[];
|
|
23
|
+
getAncestor(name: string): PropertiesContext | undefined;
|
|
24
|
+
getObject<T = unknown>(): T;
|
|
25
|
+
addProperty(property: Property, options?: AddPropertyOptions): void;
|
|
26
|
+
removeProperty(id: string): void;
|
|
27
|
+
replaceProperty(id: string, property: Property): void;
|
|
28
|
+
}
|
|
29
|
+
declare const PropertiesContext: React.Context<PropertiesContext | undefined>;
|
|
30
|
+
interface PropertiesProps {
|
|
31
|
+
name?: string;
|
|
32
|
+
onChange?(properties: Property[]): void;
|
|
33
|
+
children: React.ReactNode;
|
|
34
|
+
}
|
|
35
|
+
export declare const Properties: ({ name, onChange, children }: PropertiesProps) => React.JSX.Element;
|
|
36
|
+
export declare function useProperties(): PropertiesContext;
|
|
37
|
+
export declare function useMaybeProperties(): PropertiesContext | undefined;
|
|
38
|
+
export declare function useAncestorByName(name: string | undefined): PropertiesContext | undefined;
|
|
39
|
+
interface PropertyProps {
|
|
40
|
+
id?: string;
|
|
41
|
+
name: string;
|
|
42
|
+
value?: unknown;
|
|
43
|
+
array?: boolean;
|
|
44
|
+
after?: string;
|
|
45
|
+
before?: string;
|
|
46
|
+
replace?: string;
|
|
47
|
+
remove?: boolean;
|
|
48
|
+
parent?: string;
|
|
49
|
+
root?: boolean;
|
|
50
|
+
children?: React.ReactNode;
|
|
51
|
+
}
|
|
52
|
+
export declare function useParentProperty(): Property | undefined;
|
|
53
|
+
interface AncestorMatch {
|
|
54
|
+
[key: string]: string | boolean | number | null | undefined;
|
|
55
|
+
}
|
|
56
|
+
export declare function useAncestor(params: AncestorMatch): Property | undefined;
|
|
57
|
+
export declare const Property: ({ id, name, value, children, after, before, replace, remove, array, root, parent }: PropertyProps) => React.JSX.Element | null;
|
|
58
|
+
export {};
|
package/Properties.js
CHANGED
|
@@ -1,201 +1,240 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
1
|
+
import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { getUniqueId, toObject } from "./utils.js";
|
|
3
|
+
const PropertiesTargetContext = /*#__PURE__*/createContext(undefined);
|
|
4
|
+
export const ConnectToProperties = ({
|
|
5
|
+
name,
|
|
6
|
+
children
|
|
7
|
+
}) => {
|
|
8
|
+
return /*#__PURE__*/React.createElement(PropertiesTargetContext.Provider, {
|
|
9
|
+
value: name
|
|
10
|
+
}, children);
|
|
11
|
+
};
|
|
24
12
|
function removeByParent(id, properties) {
|
|
25
|
-
return properties.filter(
|
|
26
|
-
return prop.
|
|
27
|
-
}).reduce(function (acc, item) {
|
|
28
|
-
return removeByParent(item.id, acc.filter(function (prop) {
|
|
29
|
-
return prop.id !== item.id;
|
|
30
|
-
}));
|
|
13
|
+
return properties.filter(prop => prop.parent === id).reduce((acc, item) => {
|
|
14
|
+
return removeByParent(item.id, acc.filter(prop => prop.id !== item.id));
|
|
31
15
|
}, properties);
|
|
32
16
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
17
|
+
function putPropertyBefore(properties, property, before) {
|
|
18
|
+
const existingIndex = properties.findIndex(prop => prop.id === property.id);
|
|
19
|
+
if (existingIndex > -1) {
|
|
20
|
+
const existingProperty = properties[existingIndex];
|
|
21
|
+
const newProperties = properties.filter(p => p.id !== property.id);
|
|
22
|
+
const targetIndex = before.endsWith("$first") ? 0 : newProperties.findIndex(prop => prop.id === before);
|
|
23
|
+
return [...newProperties.slice(0, targetIndex), existingProperty, ...newProperties.slice(targetIndex)];
|
|
24
|
+
}
|
|
25
|
+
const targetIndex = properties.findIndex(prop => prop.id === before);
|
|
26
|
+
return [...properties.slice(0, targetIndex), property, ...properties.slice(targetIndex)];
|
|
27
|
+
}
|
|
28
|
+
function putPropertyAfter(properties, property, after) {
|
|
29
|
+
const existingIndex = properties.findIndex(prop => prop.id === property.id);
|
|
30
|
+
if (existingIndex > -1) {
|
|
31
|
+
const [removedProperty] = properties.splice(existingIndex, 1);
|
|
32
|
+
const targetIndex = after.endsWith("$last") ? properties.length - 1 : properties.findIndex(prop => prop.id === after);
|
|
33
|
+
return [...properties.slice(0, targetIndex + 1), removedProperty, ...properties.slice(targetIndex + 1)];
|
|
34
|
+
}
|
|
35
|
+
const targetIndex = properties.findIndex(prop => prop.id === after);
|
|
36
|
+
return [...properties.slice(0, targetIndex + 1), property, ...properties.slice(targetIndex + 1)];
|
|
37
|
+
}
|
|
38
|
+
function mergeProperty(properties, property) {
|
|
39
|
+
const index = properties.findIndex(prop => prop.id === property.id);
|
|
40
|
+
if (index > -1) {
|
|
41
|
+
return [...properties.slice(0, index), {
|
|
42
|
+
...properties[index],
|
|
43
|
+
...property
|
|
44
|
+
}, ...properties.slice(index + 1)];
|
|
45
|
+
}
|
|
46
|
+
return properties;
|
|
47
|
+
}
|
|
48
|
+
const PropertiesContext = /*#__PURE__*/createContext(undefined);
|
|
49
|
+
export const Properties = ({
|
|
50
|
+
name,
|
|
51
|
+
onChange,
|
|
52
|
+
children
|
|
53
|
+
}) => {
|
|
54
|
+
const [properties, setProperties] = useState([]);
|
|
55
|
+
let parent;
|
|
56
|
+
try {
|
|
57
|
+
parent = useProperties();
|
|
58
|
+
} catch {
|
|
59
|
+
// Do nothing, if there's no parent.
|
|
60
|
+
}
|
|
61
|
+
useEffect(() => {
|
|
46
62
|
if (onChange) {
|
|
47
63
|
onChange(properties);
|
|
48
64
|
}
|
|
49
65
|
}, [properties]);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
const context = useMemo(() => ({
|
|
67
|
+
name,
|
|
68
|
+
properties,
|
|
69
|
+
getAncestor(ancestorName) {
|
|
70
|
+
if (!parent) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
return parent && parent.name === ancestorName ? parent : parent.getAncestor(ancestorName);
|
|
74
|
+
},
|
|
75
|
+
getObject() {
|
|
76
|
+
return toObject(properties);
|
|
77
|
+
},
|
|
78
|
+
addProperty(property, options = {}) {
|
|
79
|
+
setProperties(properties => {
|
|
80
|
+
const index = properties.findIndex(prop => prop.id === property.id);
|
|
81
|
+
if (index > -1) {
|
|
82
|
+
const newProperties = mergeProperty(properties, property);
|
|
68
83
|
if (options.after) {
|
|
69
|
-
|
|
70
|
-
return prop.id === options.after;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (_index > -1) {
|
|
74
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
|
|
75
|
-
}
|
|
84
|
+
return putPropertyAfter(newProperties, property, options.after);
|
|
76
85
|
}
|
|
77
|
-
|
|
78
86
|
if (options.before) {
|
|
79
|
-
|
|
80
|
-
return prop.id === options.before;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (_index2 > -1) {
|
|
84
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
|
|
85
|
-
}
|
|
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))));
|
|
87
|
+
return putPropertyBefore(newProperties, property, options.before);
|
|
107
88
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
89
|
+
return newProperties;
|
|
90
|
+
}
|
|
91
|
+
if (options.after) {
|
|
92
|
+
return putPropertyAfter(properties, property, options.after);
|
|
93
|
+
}
|
|
94
|
+
if (options.before) {
|
|
95
|
+
return putPropertyBefore(properties, property, options.before);
|
|
96
|
+
}
|
|
97
|
+
return [...properties, property];
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
removeProperty(id) {
|
|
101
|
+
setProperties(properties => {
|
|
102
|
+
return removeByParent(id, properties.filter(prop => prop.id !== id));
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
replaceProperty(id, property) {
|
|
106
|
+
setProperties(properties => {
|
|
107
|
+
const toReplace = properties.findIndex(prop => prop.id === id);
|
|
108
|
+
if (toReplace > -1) {
|
|
109
|
+
// Replace the property and remove all remaining child properties.
|
|
110
|
+
return removeByParent(id, [...properties.slice(0, toReplace), property, ...properties.slice(toReplace + 1)]);
|
|
111
|
+
}
|
|
112
|
+
return properties;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}), [properties]);
|
|
116
|
+
return /*#__PURE__*/React.createElement(PropertiesContext.Provider, {
|
|
115
117
|
value: context
|
|
116
118
|
}, children);
|
|
117
119
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
function useProperties() {
|
|
122
|
-
var properties = (0, _react.useContext)(PropertiesContext);
|
|
123
|
-
|
|
120
|
+
export function useProperties() {
|
|
121
|
+
const properties = useContext(PropertiesContext);
|
|
124
122
|
if (!properties) {
|
|
125
123
|
throw Error("Properties context provider is missing!");
|
|
126
124
|
}
|
|
127
|
-
|
|
128
125
|
return properties;
|
|
129
126
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
127
|
+
export function useMaybeProperties() {
|
|
128
|
+
const properties = useContext(PropertiesContext);
|
|
129
|
+
if (!properties) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
return properties;
|
|
135
133
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
134
|
+
export function useAncestorByName(name) {
|
|
135
|
+
const parent = useMaybeProperties();
|
|
136
|
+
return useMemo(() => {
|
|
137
|
+
if (!name || !parent) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
if (parent.name === name) {
|
|
141
|
+
return parent;
|
|
142
|
+
}
|
|
143
|
+
return parent.getAncestor(name);
|
|
144
|
+
}, [name]);
|
|
145
|
+
}
|
|
146
|
+
const PropertyContext = /*#__PURE__*/createContext(undefined);
|
|
147
|
+
export function useParentProperty() {
|
|
148
|
+
return useContext(PropertyContext);
|
|
149
|
+
}
|
|
150
|
+
export function useAncestor(params) {
|
|
151
|
+
const property = useParentProperty();
|
|
152
|
+
const {
|
|
153
|
+
properties
|
|
154
|
+
} = useProperties();
|
|
155
|
+
const matchOrGetAncestor = (property, params) => {
|
|
156
|
+
const matchedProps = properties.filter(prop => prop.parent === property.id).filter(prop => prop.name in params && prop.value === params[prop.name]);
|
|
157
|
+
if (matchedProps.length === Object.keys(params).length) {
|
|
158
|
+
return property;
|
|
159
|
+
}
|
|
160
|
+
const newParent = property.parent ? properties.find(prop => prop.id === property.parent) : undefined;
|
|
161
|
+
return newParent ? matchOrGetAncestor(newParent, params) : undefined;
|
|
162
|
+
};
|
|
163
|
+
return property ? matchOrGetAncestor(property, params) : undefined;
|
|
164
|
+
}
|
|
165
|
+
export const Property = ({
|
|
166
|
+
id,
|
|
167
|
+
name,
|
|
168
|
+
value,
|
|
169
|
+
children,
|
|
170
|
+
after = undefined,
|
|
171
|
+
before = undefined,
|
|
172
|
+
replace = undefined,
|
|
173
|
+
remove = false,
|
|
174
|
+
array = false,
|
|
175
|
+
root = false,
|
|
176
|
+
parent = undefined
|
|
177
|
+
}) => {
|
|
178
|
+
const targetName = useContext(PropertiesTargetContext);
|
|
179
|
+
const uniqueId = useMemo(() => id || getUniqueId(), []);
|
|
180
|
+
const parentProperty = useParentProperty();
|
|
181
|
+
const immediateProperties = useProperties();
|
|
182
|
+
const ancestorByName = useAncestorByName(targetName);
|
|
183
|
+
const previousValue = useRef(value);
|
|
184
|
+
const properties = targetName && ancestorByName ? ancestorByName : immediateProperties;
|
|
158
185
|
if (!properties) {
|
|
159
186
|
throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
160
187
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
188
|
+
const {
|
|
189
|
+
addProperty,
|
|
190
|
+
removeProperty,
|
|
191
|
+
replaceProperty
|
|
192
|
+
} = properties;
|
|
193
|
+
const parentId = parent ? parent : root ? "" : parentProperty?.id || "";
|
|
194
|
+
const property = {
|
|
166
195
|
id: uniqueId,
|
|
167
|
-
name
|
|
168
|
-
value
|
|
169
|
-
parent:
|
|
170
|
-
array
|
|
196
|
+
name,
|
|
197
|
+
value,
|
|
198
|
+
parent: parentId,
|
|
199
|
+
array
|
|
171
200
|
};
|
|
172
|
-
|
|
201
|
+
useEffect(() => {
|
|
173
202
|
if (remove) {
|
|
174
203
|
removeProperty(uniqueId);
|
|
175
204
|
return;
|
|
176
205
|
}
|
|
177
|
-
|
|
178
206
|
if (replace) {
|
|
179
207
|
replaceProperty(replace, property);
|
|
180
208
|
return;
|
|
181
209
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
210
|
+
const $isFirst = before === "$first";
|
|
211
|
+
const $isLast = after === "$last";
|
|
212
|
+
addProperty({
|
|
213
|
+
...property,
|
|
214
|
+
$isFirst,
|
|
215
|
+
$isLast
|
|
216
|
+
}, {
|
|
217
|
+
after,
|
|
218
|
+
before
|
|
186
219
|
});
|
|
187
|
-
return
|
|
220
|
+
return () => {
|
|
188
221
|
removeProperty(uniqueId);
|
|
189
222
|
};
|
|
190
223
|
}, []);
|
|
191
|
-
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
if (previousValue.current !== value) {
|
|
226
|
+
previousValue.current = value;
|
|
227
|
+
if (!remove && !replace) {
|
|
228
|
+
replaceProperty(uniqueId, property);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}, [value]);
|
|
192
232
|
if (children) {
|
|
193
|
-
return /*#__PURE__*/
|
|
233
|
+
return /*#__PURE__*/React.createElement(PropertyContext.Provider, {
|
|
194
234
|
value: property
|
|
195
235
|
}, children);
|
|
196
236
|
}
|
|
197
|
-
|
|
198
237
|
return null;
|
|
199
238
|
};
|
|
200
239
|
|
|
201
|
-
|
|
240
|
+
//# sourceMappingURL=Properties.js.map
|
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;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AyD5B,UAAzD,CAAqB4B,cAArB;EAAA,IAAqCC,eAArC,GAAyD7B,UAAzD,CAAqC6B,eAArC;EACA,IAAMR,QAAQ,GAAG;IAAEtB,EAAE,EAAE2C,QAAN;IAAgBL,IAAI,EAAJA,IAAhB;IAAsBC,KAAK,EAALA,KAAtB;IAA6BnC,MAAM,EAAEA,MAAM,GAAGA,MAAM,CAACJ,EAAV,GAAe,EAA1D;IAA8D0C,KAAK,EAALA;EAA9D,CAAjB;EAEA,IAAA1B,gBAAA,EAAU,YAAM;IACZ,IAAIyB,MAAJ,EAAY;MACRZ,cAAc,CAACc,QAAD,CAAd;MACA;IACH;;IAED,IAAIH,OAAJ,EAAa;MACTV,eAAe,CAACU,OAAD,EAAUlB,QAAV,CAAf;MACA;IACH;;IAEDD,WAAW,CAACC,QAAD,EAAW;MAAEK,KAAK,EAALA,KAAF;MAASC,MAAM,EAANA;IAAT,CAAX,CAAX;IAEA,OAAO,YAAM;MACTC,cAAc,CAACc,QAAD,CAAd;IACH,CAFD;EAGH,CAhBD,EAgBG,EAhBH;;EAkBA,IAAI9B,QAAJ,EAAc;IACV,oBAAO,6BAAC,eAAD,CAAiB,QAAjB;MAA0B,KAAK,EAAES;IAAjC,GAA4CT,QAA5C,CAAP;EACH;;EAED,OAAO,IAAP;AACH,CA7CM"}
|
|
1
|
+
{"version":3,"names":["React","createContext","useContext","useEffect","useMemo","useRef","useState","getUniqueId","toObject","PropertiesTargetContext","undefined","ConnectToProperties","name","children","createElement","Provider","value","removeByParent","id","properties","filter","prop","parent","reduce","acc","item","putPropertyBefore","property","before","existingIndex","findIndex","existingProperty","newProperties","p","targetIndex","endsWith","slice","putPropertyAfter","after","removedProperty","splice","length","mergeProperty","index","PropertiesContext","Properties","onChange","setProperties","useProperties","context","getAncestor","ancestorName","getObject","addProperty","options","removeProperty","replaceProperty","toReplace","Error","useMaybeProperties","useAncestorByName","PropertyContext","useParentProperty","useAncestor","params","matchOrGetAncestor","matchedProps","Object","keys","newParent","find","Property","replace","remove","array","root","targetName","uniqueId","parentProperty","immediateProperties","ancestorByName","previousValue","parentId","$isFirst","$isLast","current"],"sources":["Properties.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from \"react\";\nimport { getUniqueId, toObject } from \"./utils.js\";\n\nconst PropertiesTargetContext = createContext<string | undefined>(undefined);\n\nexport interface ConnectToPropertiesProps {\n name: string;\n children: React.ReactNode;\n}\n\nexport const ConnectToProperties = ({ name, children }: ConnectToPropertiesProps) => {\n return (\n <PropertiesTargetContext.Provider value={name}>{children}</PropertiesTargetContext.Provider>\n );\n};\n\nexport interface Property {\n id: string;\n parent: string;\n name: string;\n value?: unknown;\n array?: boolean;\n $isFirst?: boolean;\n $isLast?: 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 name?: string;\n properties: Property[];\n getAncestor(name: string): PropertiesContext | undefined;\n getObject<T = unknown>(): T;\n addProperty(property: Property, options?: AddPropertyOptions): void;\n removeProperty(id: string): void;\n replaceProperty(id: string, property: Property): void;\n}\n\nfunction putPropertyBefore(properties: Property[], property: Property, before: string) {\n const existingIndex = properties.findIndex(prop => prop.id === property.id);\n if (existingIndex > -1) {\n const existingProperty = properties[existingIndex];\n const newProperties = properties.filter(p => p.id !== property.id);\n const targetIndex = before.endsWith(\"$first\")\n ? 0\n : newProperties.findIndex(prop => prop.id === before);\n return [\n ...newProperties.slice(0, targetIndex),\n existingProperty,\n ...newProperties.slice(targetIndex)\n ];\n }\n\n const targetIndex = properties.findIndex(prop => prop.id === before);\n\n return [...properties.slice(0, targetIndex), property, ...properties.slice(targetIndex)];\n}\n\nfunction putPropertyAfter(properties: Property[], property: Property, after: string) {\n const existingIndex = properties.findIndex(prop => prop.id === property.id);\n\n if (existingIndex > -1) {\n const [removedProperty] = properties.splice(existingIndex, 1);\n const targetIndex = after.endsWith(\"$last\")\n ? properties.length - 1\n : properties.findIndex(prop => prop.id === after);\n return [\n ...properties.slice(0, targetIndex + 1),\n removedProperty,\n ...properties.slice(targetIndex + 1)\n ];\n }\n\n const targetIndex = properties.findIndex(prop => prop.id === after);\n\n return [\n ...properties.slice(0, targetIndex + 1),\n property,\n ...properties.slice(targetIndex + 1)\n ];\n}\n\nfunction mergeProperty(properties: Property[], property: Property) {\n const index = properties.findIndex(prop => prop.id === property.id);\n if (index > -1) {\n return [\n ...properties.slice(0, index),\n { ...properties[index], ...property },\n ...properties.slice(index + 1)\n ];\n }\n return properties;\n}\n\nconst PropertiesContext = createContext<PropertiesContext | undefined>(undefined);\n\ninterface PropertiesProps {\n name?: string;\n onChange?(properties: Property[]): void;\n children: React.ReactNode;\n}\n\nexport const Properties = ({ name, onChange, children }: PropertiesProps) => {\n const [properties, setProperties] = useState<Property[]>([]);\n let parent: PropertiesContext;\n\n try {\n parent = useProperties();\n } catch {\n // Do nothing, if there's no parent.\n }\n\n useEffect(() => {\n if (onChange) {\n onChange(properties);\n }\n }, [properties]);\n\n const context: PropertiesContext = useMemo(\n () => ({\n name,\n properties,\n getAncestor(ancestorName: string) {\n if (!parent) {\n return undefined;\n }\n\n return parent && parent.name === ancestorName\n ? parent\n : parent.getAncestor(ancestorName);\n },\n getObject<T>() {\n return toObject(properties) as T;\n },\n addProperty(property, options = {}) {\n setProperties(properties => {\n const index = properties.findIndex(prop => prop.id === property.id);\n\n if (index > -1) {\n const newProperties = mergeProperty(properties, property);\n if (options.after) {\n return putPropertyAfter(newProperties, property, options.after);\n }\n if (options.before) {\n return putPropertyBefore(newProperties, property, options.before);\n }\n\n return newProperties;\n }\n\n if (options.after) {\n return putPropertyAfter(properties, property, options.after);\n }\n\n if (options.before) {\n return putPropertyBefore(properties, property, options.before);\n }\n\n return [...properties, property];\n });\n },\n removeProperty(id) {\n setProperties(properties => {\n return removeByParent(\n id,\n properties.filter(prop => prop.id !== id)\n );\n });\n },\n replaceProperty(id, property) {\n setProperties(properties => {\n const toReplace = properties.findIndex(prop => prop.id === id);\n\n if (toReplace > -1) {\n // Replace the property and remove all remaining child properties.\n return removeByParent(id, [\n ...properties.slice(0, toReplace),\n property,\n ...properties.slice(toReplace + 1)\n ]);\n }\n return properties;\n });\n }\n }),\n [properties]\n );\n\n return <PropertiesContext.Provider value={context}>{children}</PropertiesContext.Provider>;\n};\n\nexport function useProperties() {\n const properties = useContext(PropertiesContext);\n if (!properties) {\n throw Error(\"Properties context provider is missing!\");\n }\n\n return properties;\n}\n\nexport function useMaybeProperties() {\n const properties = useContext(PropertiesContext);\n if (!properties) {\n return undefined;\n }\n\n return properties;\n}\n\nexport function useAncestorByName(name: string | undefined) {\n const parent = useMaybeProperties();\n\n return useMemo(() => {\n if (!name || !parent) {\n return undefined;\n }\n\n if (parent.name === name) {\n return parent;\n }\n\n return parent.getAncestor(name);\n }, [name]);\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 children?: React.ReactNode;\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 = ({\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}: PropertyProps) => {\n const targetName = useContext(PropertiesTargetContext);\n const uniqueId = useMemo(() => id || getUniqueId(), []);\n const parentProperty = useParentProperty();\n const immediateProperties = useProperties();\n const ancestorByName = useAncestorByName(targetName);\n const previousValue = useRef(value);\n\n const properties = targetName && ancestorByName ? ancestorByName : immediateProperties;\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 const $isFirst = before === \"$first\";\n const $isLast = after === \"$last\";\n\n addProperty({ ...property, $isFirst, $isLast }, { after, before });\n\n return () => {\n removeProperty(uniqueId);\n };\n }, []);\n\n useEffect(() => {\n if (previousValue.current !== value) {\n previousValue.current = value;\n if (!remove && !replace) {\n replaceProperty(uniqueId, property);\n }\n }\n }, [value]);\n\n if (children) {\n return <PropertyContext.Provider value={property}>{children}</PropertyContext.Provider>;\n }\n\n return null;\n};\n"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,EAAEC,UAAU,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAC9F,SAASC,WAAW,EAAEC,QAAQ;AAE9B,MAAMC,uBAAuB,gBAAGR,aAAa,CAAqBS,SAAS,CAAC;AAO5E,OAAO,MAAMC,mBAAmB,GAAGA,CAAC;EAAEC,IAAI;EAAEC;AAAmC,CAAC,KAAK;EACjF,oBACIb,KAAA,CAAAc,aAAA,CAACL,uBAAuB,CAACM,QAAQ;IAACC,KAAK,EAAEJ;EAAK,GAAE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C,QAAQ,CAAC;MAClC;IACJ;IAEA,MAAMwD,QAAQ,GAAGvD,MAAM,KAAK,QAAQ;IACpC,MAAMwD,OAAO,GAAG9C,KAAK,KAAK,OAAO;IAEjCe,WAAW,CAAC;MAAE,GAAG1B,QAAQ;MAAEwD,QAAQ;MAAEC;IAAQ,CAAC,EAAE;MAAE9C,KAAK;MAAEV;IAAO,CAAC,CAAC;IAElE,OAAO,MAAM;MACT2B,cAAc,CAACsB,QAAQ,CAAC;IAC5B,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN1E,SAAS,CAAC,MAAM;IACZ,IAAI8E,aAAa,CAACI,OAAO,KAAKrE,KAAK,EAAE;MACjCiE,aAAa,CAACI,OAAO,GAAGrE,KAAK;MAC7B,IAAI,CAACyD,MAAM,IAAI,CAACD,OAAO,EAAE;QACrBhB,eAAe,CAACqB,QAAQ,EAAElD,QAAQ,CAAC;MACvC;IACJ;EACJ,CAAC,EAAE,CAACX,KAAK,CAAC,CAAC;EAEX,IAAIH,QAAQ,EAAE;IACV,oBAAOb,KAAA,CAAAc,aAAA,CAAC+C,eAAe,CAAC9C,QAAQ;MAACC,KAAK,EAAEW;IAAS,GAAEd,QAAmC,CAAC;EAC3F;EAEA,OAAO,IAAI;AACf,CAAC","ignoreList":[]}
|
package/README.md
CHANGED
|
@@ -1,65 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webiny/react-properties
|
|
2
2
|
|
|
3
|
-
[!
|
|
4
|
-
[
|
|
5
|
-
|
|
6
|
-
[](http://makeapullrequest.com)
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
---
|
|
11
10
|
|
|
12
|
-
|
|
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.
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { Property } from "./index.js";
|
|
3
|
+
export interface WithConfigProps {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
onProperties?(properties: Property[]): void;
|
|
6
|
+
}
|
|
7
|
+
export interface ConfigProps {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
priority?: "primary" | "secondary";
|
|
10
|
+
}
|
|
11
|
+
export declare function createConfigurableComponent<TConfig>(name: string): {
|
|
12
|
+
WithConfig: ({ onProperties, children }: WithConfigProps) => React.JSX.Element;
|
|
13
|
+
Config: ({ priority, children }: ConfigProps) => React.JSX.Element;
|
|
14
|
+
useConfig: <TExtra extends object>() => TConfig & TExtra;
|
|
15
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import React, { useContext, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { Compose, makeDecoratable } from "@webiny/react-composition";
|
|
3
|
+
import { Properties, toObject } from "./index.js";
|
|
4
|
+
import { useDebugConfig } from "./useDebugConfig.js";
|
|
5
|
+
import debounce from "lodash/debounce.js";
|
|
6
|
+
const createHOC = newChildren => BaseComponent => {
|
|
7
|
+
return function ConfigHOC({
|
|
8
|
+
children
|
|
9
|
+
}) {
|
|
10
|
+
return /*#__PURE__*/React.createElement(BaseComponent, null, children, newChildren);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export function createConfigurableComponent(name) {
|
|
14
|
+
/**
|
|
15
|
+
* This component is used when we want to mount all composed configs.
|
|
16
|
+
*/
|
|
17
|
+
const ConfigApplyPrimary = makeDecoratable(`${name}ConfigApply<Primary>`, ({
|
|
18
|
+
children
|
|
19
|
+
}) => {
|
|
20
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
21
|
+
});
|
|
22
|
+
const ConfigApplySecondary = makeDecoratable(`${name}ConfigApply<Secondary>`, ({
|
|
23
|
+
children
|
|
24
|
+
}) => {
|
|
25
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* This component is used to configure the component (it can be mounted many times).
|
|
30
|
+
*/
|
|
31
|
+
const Config = ({
|
|
32
|
+
priority = "primary",
|
|
33
|
+
children
|
|
34
|
+
}) => {
|
|
35
|
+
if (priority === "primary") {
|
|
36
|
+
return /*#__PURE__*/React.createElement(Compose, {
|
|
37
|
+
component: ConfigApplyPrimary,
|
|
38
|
+
with: createHOC(children)
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return /*#__PURE__*/React.createElement(Compose, {
|
|
42
|
+
component: ConfigApplySecondary,
|
|
43
|
+
with: createHOC(children)
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
const defaultContext = {
|
|
47
|
+
properties: []
|
|
48
|
+
};
|
|
49
|
+
const ViewContext = /*#__PURE__*/React.createContext(defaultContext);
|
|
50
|
+
const WithConfig = ({
|
|
51
|
+
onProperties,
|
|
52
|
+
children
|
|
53
|
+
}) => {
|
|
54
|
+
const [properties, setProperties] = useState([]);
|
|
55
|
+
useDebugConfig(name, properties);
|
|
56
|
+
const context = {
|
|
57
|
+
properties
|
|
58
|
+
};
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (typeof onProperties === "function") {
|
|
61
|
+
onProperties(properties);
|
|
62
|
+
}
|
|
63
|
+
}, [properties]);
|
|
64
|
+
const stateUpdater = properties => {
|
|
65
|
+
setProperties(properties);
|
|
66
|
+
};
|
|
67
|
+
return /*#__PURE__*/React.createElement(ViewContext.Provider, {
|
|
68
|
+
value: context
|
|
69
|
+
}, /*#__PURE__*/React.createElement(Properties, {
|
|
70
|
+
onChange: stateUpdater
|
|
71
|
+
}, /*#__PURE__*/React.createElement(ConfigApplyPrimary, null), /*#__PURE__*/React.createElement(DebounceRenderer, null, /*#__PURE__*/React.createElement(ConfigApplySecondary, null), /*#__PURE__*/React.createElement(DebounceRenderer, null, children))));
|
|
72
|
+
};
|
|
73
|
+
function useConfig() {
|
|
74
|
+
const {
|
|
75
|
+
properties
|
|
76
|
+
} = useContext(ViewContext);
|
|
77
|
+
return useMemo(() => toObject(properties), [properties]);
|
|
78
|
+
}
|
|
79
|
+
const DebounceRenderer = ({
|
|
80
|
+
children
|
|
81
|
+
}) => {
|
|
82
|
+
const [render, setRender] = useState(false);
|
|
83
|
+
const editorConfig = useConfig();
|
|
84
|
+
const debouncedRender = useMemo(() => {
|
|
85
|
+
return debounce(() => {
|
|
86
|
+
setRender(true);
|
|
87
|
+
}, 10);
|
|
88
|
+
}, [setRender]);
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (render) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
debouncedRender();
|
|
94
|
+
return () => {
|
|
95
|
+
debouncedRender.cancel();
|
|
96
|
+
};
|
|
97
|
+
}, [editorConfig]);
|
|
98
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, render ? children : null);
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
WithConfig,
|
|
102
|
+
Config,
|
|
103
|
+
useConfig
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//# sourceMappingURL=createConfigurableComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","useContext","useEffect","useMemo","useState","Compose","makeDecoratable","Properties","toObject","useDebugConfig","debounce","createHOC","newChildren","BaseComponent","ConfigHOC","children","createElement","createConfigurableComponent","name","ConfigApplyPrimary","Fragment","ConfigApplySecondary","Config","priority","component","with","defaultContext","properties","ViewContext","createContext","WithConfig","onProperties","setProperties","context","stateUpdater","Provider","value","onChange","DebounceRenderer","useConfig","render","setRender","editorConfig","debouncedRender","cancel"],"sources":["createConfigurableComponent.tsx"],"sourcesContent":["import React, { useContext, useEffect, useMemo, useState } from \"react\";\nimport type { Decorator } from \"@webiny/react-composition\";\nimport { Compose, makeDecoratable } from \"@webiny/react-composition\";\nimport type { GenericComponent } from \"@webiny/react-composition/types.js\";\nimport type { Property } from \"~/index.js\";\nimport { Properties, toObject } from \"~/index.js\";\nimport { useDebugConfig } from \"./useDebugConfig.js\";\nimport debounce from \"lodash/debounce.js\";\n\nconst createHOC =\n (newChildren: React.ReactNode): Decorator<GenericComponent<{ children?: React.ReactNode }>> =>\n BaseComponent => {\n return function ConfigHOC({ children }) {\n return (\n <BaseComponent>\n {children}\n {newChildren}\n </BaseComponent>\n );\n };\n };\n\nexport interface WithConfigProps {\n children: React.ReactNode;\n onProperties?(properties: Property[]): void;\n}\n\ninterface ConfigApplyProps {\n children?: React.ReactNode;\n}\n\nexport interface ConfigProps {\n children: React.ReactNode;\n priority?: \"primary\" | \"secondary\";\n}\n\nexport function createConfigurableComponent<TConfig>(name: string) {\n /**\n * This component is used when we want to mount all composed configs.\n */\n const ConfigApplyPrimary = makeDecoratable(\n `${name}ConfigApply<Primary>`,\n ({ children }: ConfigApplyProps) => {\n return <>{children}</>;\n }\n );\n\n const ConfigApplySecondary = makeDecoratable(\n `${name}ConfigApply<Secondary>`,\n ({ children }: ConfigApplyProps) => {\n return <>{children}</>;\n }\n );\n\n /**\n * This component is used to configure the component (it can be mounted many times).\n */\n const Config = ({ priority = \"primary\", children }: ConfigProps) => {\n if (priority === \"primary\") {\n return <Compose component={ConfigApplyPrimary} with={createHOC(children)} />;\n }\n return <Compose component={ConfigApplySecondary} with={createHOC(children)} />;\n };\n\n interface ViewContext {\n properties: Property[];\n }\n\n const defaultContext = { properties: [] };\n\n const ViewContext = React.createContext<ViewContext>(defaultContext);\n\n const WithConfig = ({ onProperties, children }: WithConfigProps) => {\n const [properties, setProperties] = useState<Property[]>([]);\n useDebugConfig(name, properties);\n const context = { properties };\n\n useEffect(() => {\n if (typeof onProperties === \"function\") {\n onProperties(properties);\n }\n }, [properties]);\n\n const stateUpdater = (properties: Property[]) => {\n setProperties(properties);\n };\n\n return (\n <ViewContext.Provider value={context}>\n <Properties onChange={stateUpdater}>\n <ConfigApplyPrimary />\n <DebounceRenderer>\n <ConfigApplySecondary />\n <DebounceRenderer>{children}</DebounceRenderer>\n </DebounceRenderer>\n </Properties>\n </ViewContext.Provider>\n );\n };\n\n function useConfig<TExtra extends object>(): TConfig & TExtra {\n const { properties } = useContext(ViewContext);\n return useMemo(() => toObject<TConfig & TExtra>(properties), [properties]);\n }\n\n interface Props {\n children?: React.ReactNode;\n }\n\n const DebounceRenderer = ({ children }: Props) => {\n const [render, setRender] = useState(false);\n const editorConfig = useConfig();\n\n const debouncedRender = useMemo(() => {\n return debounce(() => {\n setRender(true);\n }, 10);\n }, [setRender]);\n\n useEffect(() => {\n if (render) {\n return;\n }\n\n debouncedRender();\n\n return () => {\n debouncedRender.cancel();\n };\n }, [editorConfig]);\n\n return <>{render ? children : null}</>;\n };\n\n return {\n WithConfig,\n Config,\n useConfig\n };\n}\n"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,UAAU,EAAEC,SAAS,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,OAAO;AAEvE,SAASC,OAAO,EAAEC,eAAe,QAAQ,2BAA2B;AAGpE,SAASC,UAAU,EAAEC,QAAQ;AAC7B,SAASC,cAAc;AACvB,OAAOC,QAAQ,MAAM,oBAAoB;AAEzC,MAAMC,SAAS,GACVC,WAA4B,IAC7BC,aAAa,IAAI;EACb,OAAO,SAASC,SAASA,CAAC;IAAEC;EAAS,CAAC,EAAE;IACpC,oBACIf,KAAA,CAAAgB,aAAA,CAACH,aAAa,QACTE,QAAQ,EACRH,WACU,CAAC;EAExB,CAAC;AACL,CAAC;AAgBL,OAAO,SAASK,2BAA2BA,CAAUC,IAAY,EAAE;EAC/D;AACJ;AACA;EACI,MAAMC,kBAAkB,GAAGb,eAAe,CACtC,GAAGY,IAAI,sBAAsB,EAC7B,CAAC;IAAEH;EAA2B,CAAC,KAAK;IAChC,oBAAOf,KAAA,CAAAgB,aAAA,CAAAhB,KAAA,CAAAoB,QAAA,QAAGL,QAAW,CAAC;EAC1B,CACJ,CAAC;EAED,MAAMM,oBAAoB,GAAGf,eAAe,CACxC,GAAGY,IAAI,wBAAwB,EAC/B,C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CAAC;EAED,OAAO;IACHe,UAAU;IACVR,MAAM;IACNiB;EACJ,CAAC;AACL","ignoreList":[]}
|
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -1,31 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
export * from "./utils.js";
|
|
2
|
+
export * from "./Properties.js";
|
|
3
|
+
export * from "./useDebugConfig.js";
|
|
4
|
+
export * from "./useIdGenerator.js";
|
|
5
|
+
export * from "./createConfigurableComponent.js";
|
|
2
6
|
|
|
3
|
-
|
|
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
|
-
});
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./utils\";\nexport * from \"./Properties\";\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./utils.js\";\nexport * from \"./Properties.js\";\nexport * from \"./useDebugConfig.js\";\nexport * from \"./useIdGenerator.js\";\nexport * from \"./createConfigurableComponent.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/react-properties",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.61c048f412",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -10,24 +11,21 @@
|
|
|
10
11
|
"author": "Webiny Ltd",
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"dependencies": {
|
|
13
|
-
"@
|
|
14
|
-
"@
|
|
15
|
-
"
|
|
16
|
-
"
|
|
14
|
+
"@types/react": "18.2.79",
|
|
15
|
+
"@webiny/react-composition": "0.0.0-unstable.61c048f412",
|
|
16
|
+
"lodash": "4.17.23",
|
|
17
|
+
"nanoid": "3.3.11",
|
|
18
|
+
"react": "18.2.0"
|
|
17
19
|
},
|
|
18
20
|
"devDependencies": {
|
|
19
|
-
"@testing-library/react": "
|
|
20
|
-
"@webiny/
|
|
21
|
-
"
|
|
22
|
-
"
|
|
21
|
+
"@testing-library/react": "15.0.7",
|
|
22
|
+
"@webiny/build-tools": "0.0.0-unstable.61c048f412",
|
|
23
|
+
"prettier": "3.6.2",
|
|
24
|
+
"vitest": "3.2.4"
|
|
23
25
|
},
|
|
24
26
|
"publishConfig": {
|
|
25
27
|
"access": "public",
|
|
26
28
|
"directory": "dist"
|
|
27
29
|
},
|
|
28
|
-
"
|
|
29
|
-
"build": "yarn webiny run build",
|
|
30
|
-
"watch": "yarn webiny run watch"
|
|
31
|
-
},
|
|
32
|
-
"gitHead": "615a930a68d692c832e4f100405eeb3f5a8874af"
|
|
30
|
+
"gitHead": "61c048f412d6b4aa70c1d105aab21e3fa69730f3"
|
|
33
31
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { toObject } from "./utils.js";
|
|
3
|
+
export function useDebugConfig(name, properties) {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
if (process.env.NODE_ENV !== "development") {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const configs = window.__debugConfigs ?? {};
|
|
9
|
+
configs[name] = () => console.log(toObject(properties));
|
|
10
|
+
window.__debugConfigs = configs;
|
|
11
|
+
return () => {
|
|
12
|
+
const configs = window.__debugConfigs ?? {};
|
|
13
|
+
delete configs[name];
|
|
14
|
+
window.__debugConfigs = configs;
|
|
15
|
+
};
|
|
16
|
+
}, [properties]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=useDebugConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","toObject","useDebugConfig","name","properties","process","env","NODE_ENV","configs","window","__debugConfigs","console","log"],"sources":["useDebugConfig.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport type { Property } from \"./Properties.js\";\nimport { toObject } from \"./utils.js\";\n\ndeclare global {\n interface Window {\n __debugConfigs: Record<string, () => void>;\n }\n}\n\nexport function useDebugConfig(name: string, properties: Property[]) {\n useEffect(() => {\n if (process.env.NODE_ENV !== \"development\") {\n return;\n }\n\n const configs = window.__debugConfigs ?? {};\n configs[name] = () => console.log(toObject(properties));\n window.__debugConfigs = configs;\n\n return () => {\n const configs = window.__debugConfigs ?? {};\n delete configs[name];\n window.__debugConfigs = configs;\n };\n }, [properties]);\n}\n"],"mappings":"AAAA,SAASA,SAAS,QAAQ,OAAO;AAEjC,SAASC,QAAQ;AAQjB,OAAO,SAASC,cAAcA,CAACC,IAAY,EAAEC,UAAsB,EAAE;EACjEJ,SAAS,CAAC,MAAM;IACZ,IAAIK,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,aAAa,EAAE;MACxC;IACJ;IAEA,MAAMC,OAAO,GAAGC,MAAM,CAACC,cAAc,IAAI,CAAC,CAAC;IAC3CF,OAAO,CAACL,IAAI,CAAC,GAAG,MAAMQ,OAAO,CAACC,GAAG,CAACX,QAAQ,CAACG,UAAU,CAAC,CAAC;IACvDK,MAAM,CAACC,cAAc,GAAGF,OAAO;IAE/B,OAAO,MAAM;MACT,MAAMA,OAAO,GAAGC,MAAM,CAACC,cAAc,IAAI,CAAC,CAAC;MAC3C,OAAOF,OAAO,CAACL,IAAI,CAAC;MACpBM,MAAM,CAACC,cAAc,GAAGF,OAAO;IACnC,CAAC;EACL,CAAC,EAAE,CAACJ,UAAU,CAAC,CAAC;AACpB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useIdGenerator(name: string): (...parts: string[]) => string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useParentProperty } from "./Properties.js";
|
|
3
|
+
const keywords = ["$first", "$last"];
|
|
4
|
+
export function useIdGenerator(name) {
|
|
5
|
+
const parentProperty = useParentProperty();
|
|
6
|
+
return useCallback((...parts) => {
|
|
7
|
+
if (keywords.includes(parts[0])) {
|
|
8
|
+
return parts[0];
|
|
9
|
+
}
|
|
10
|
+
return [parentProperty?.id, name, ...parts].filter(Boolean).join(":");
|
|
11
|
+
}, [name, parentProperty]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=useIdGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useParentProperty","keywords","useIdGenerator","name","parentProperty","parts","includes","id","filter","Boolean","join"],"sources":["useIdGenerator.ts"],"sourcesContent":["import { useCallback } from \"react\";\nimport { useParentProperty } from \"~/Properties.js\";\n\nconst keywords = [\"$first\", \"$last\"];\n\nexport function useIdGenerator(name: string) {\n const parentProperty = useParentProperty();\n\n return useCallback(\n (...parts: string[]) => {\n if (keywords.includes(parts[0])) {\n return parts[0];\n }\n return [parentProperty?.id, name, ...parts].filter(Boolean).join(\":\");\n },\n [name, parentProperty]\n );\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,OAAO;AACnC,SAASC,iBAAiB;AAE1B,MAAMC,QAAQ,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC;AAEpC,OAAO,SAASC,cAAcA,CAACC,IAAY,EAAE;EACzC,MAAMC,cAAc,GAAGJ,iBAAiB,CAAC,CAAC;EAE1C,OAAOD,WAAW,CACd,CAAC,GAAGM,KAAe,KAAK;IACpB,IAAIJ,QAAQ,CAACK,QAAQ,CAACD,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;MAC7B,OAAOA,KAAK,CAAC,CAAC,CAAC;IACnB;IACA,OAAO,CAACD,cAAc,EAAEG,EAAE,EAAEJ,IAAI,EAAE,GAAGE,KAAK,CAAC,CAACG,MAAM,CAACC,OAAO,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC;EACzE,CAAC,EACD,CAACP,IAAI,EAAEC,cAAc,CACzB,CAAC;AACL","ignoreList":[]}
|
package/utils.d.ts
ADDED
package/utils.js
CHANGED
|
@@ -1,54 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var nanoid = (0, _nanoid.customAlphabet)("1234567890abcdef", 6);
|
|
20
|
-
|
|
1
|
+
import { customAlphabet } from "nanoid";
|
|
2
|
+
const nanoid = customAlphabet("1234567890abcdef");
|
|
3
|
+
const sortPropertiesToTheTop = (a, b) => {
|
|
4
|
+
if (a.$isFirst && b.$isFirst) {
|
|
5
|
+
return -1;
|
|
6
|
+
}
|
|
7
|
+
return Number(b.$isFirst) - Number(a.$isFirst);
|
|
8
|
+
};
|
|
9
|
+
const sortPropertiesToTheBottom = (a, b) => {
|
|
10
|
+
if (a.$isLast && b.$isLast) {
|
|
11
|
+
return 1;
|
|
12
|
+
}
|
|
13
|
+
return Number(a.$isLast) - Number(b.$isLast);
|
|
14
|
+
};
|
|
15
|
+
const sortProperties = properties => {
|
|
16
|
+
return properties.sort(sortPropertiesToTheTop).sort(sortPropertiesToTheBottom);
|
|
17
|
+
};
|
|
21
18
|
function buildRoots(roots, properties) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
const sortedRoots = sortProperties(roots);
|
|
20
|
+
const obj = sortedRoots.reduce((acc, item) => {
|
|
21
|
+
const isArray = item.array === true || sortedRoots.filter(r => r.name === item.name).length > 1;
|
|
22
|
+
return {
|
|
23
|
+
...acc,
|
|
24
|
+
[item.name]: isArray ? [] : {}
|
|
25
|
+
};
|
|
27
26
|
}, {});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
sortedRoots.forEach(root => {
|
|
28
|
+
const isArray = root.array === true || Array.isArray(obj[root.name]);
|
|
31
29
|
if (root.value !== undefined) {
|
|
32
|
-
obj[root.name] = isArray ? [
|
|
30
|
+
obj[root.name] = isArray ? [...obj[root.name], root.value] : root.value;
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
39
|
-
var value = buildRoots(nextRoots, properties);
|
|
40
|
-
obj[root.name] = isArray ? [].concat((0, _toConsumableArray2.default)(obj[root.name]), [value]) : value;
|
|
33
|
+
const nextRoots = properties.filter(p => p.parent === root.id);
|
|
34
|
+
const value = buildRoots(nextRoots, properties);
|
|
35
|
+
obj[root.name] = isArray ? [...obj[root.name], value] : value;
|
|
41
36
|
});
|
|
42
37
|
return obj;
|
|
43
38
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
var roots = properties.filter(function (prop) {
|
|
47
|
-
return prop.parent === "";
|
|
48
|
-
});
|
|
39
|
+
export function toObject(properties) {
|
|
40
|
+
const roots = properties.filter(prop => prop.parent === "");
|
|
49
41
|
return buildRoots(roots, properties);
|
|
50
42
|
}
|
|
43
|
+
export function getUniqueId(length = 12) {
|
|
44
|
+
return nanoid(length);
|
|
45
|
+
}
|
|
51
46
|
|
|
52
|
-
|
|
53
|
-
return nanoid();
|
|
54
|
-
}
|
|
47
|
+
//# sourceMappingURL=utils.js.map
|
package/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["nanoid","
|
|
1
|
+
{"version":3,"names":["customAlphabet","nanoid","sortPropertiesToTheTop","a","b","$isFirst","Number","sortPropertiesToTheBottom","$isLast","sortProperties","properties","sort","buildRoots","roots","sortedRoots","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 type { Property } from \"./Properties.js\";\n\nconst sortPropertiesToTheTop = (a: Property, b: Property) => {\n if (a.$isFirst && b.$isFirst) {\n return -1;\n }\n\n return Number(b.$isFirst) - Number(a.$isFirst);\n};\n\nconst sortPropertiesToTheBottom = (a: Property, b: Property) => {\n if (a.$isLast && b.$isLast) {\n return 1;\n }\n\n return Number(a.$isLast) - Number(b.$isLast);\n};\n\nconst sortProperties = (properties: Property[]) => {\n return properties.sort(sortPropertiesToTheTop).sort(sortPropertiesToTheBottom);\n};\n\nfunction buildRoots(roots: Property[], properties: Property[]) {\n const sortedRoots = sortProperties(roots);\n const obj: Record<string, unknown> = sortedRoots.reduce((acc, item) => {\n const isArray =\n item.array === true || sortedRoots.filter(r => r.name === item.name).length > 1;\n return { ...acc, [item.name]: isArray ? [] : {} };\n }, {});\n\n sortedRoots.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,SAASA,cAAc,QAAQ,QAAQ;AACvC,MAAMC,MAAM,GAAGD,cAAc,CAAC,kBAAkB,CAAC;AAGjD,MAAME,sBAAsB,GAAGA,CAACC,CAAW,EAAEC,CAAW,KAAK;EACzD,IAAID,CAAC,CAACE,QAAQ,IAAID,CAAC,CAACC,QAAQ,EAAE;IAC1B,OAAO,CAAC,CAAC;EACb;EAEA,OAAOC,MAAM,CAACF,CAAC,CAACC,QAAQ,CAAC,GAAGC,MAAM,CAACH,CAAC,CAACE,QAAQ,CAAC;AAClD,CAAC;AAED,MAAME,yBAAyB,GAAGA,CAACJ,CAAW,EAAEC,CAAW,KAAK;EAC5D,IAAID,CAAC,CAACK,OAAO,IAAIJ,CAAC,CAACI,OAAO,EAAE;IACxB,OAAO,CAAC;EACZ;EAEA,OAAOF,MAAM,CAACH,CAAC,CAACK,OAAO,CAAC,GAAGF,MAAM,CAACF,CAAC,CAACI,OAAO,CAAC;AAChD,CAAC;AAED,MAAMC,cAAc,GAAIC,UAAsB,IAAK;EAC/C,OAAOA,UAAU,CAACC,IAAI,CAACT,sBAAsB,CAAC,CAACS,IAAI,CAACJ,yBAAyB,CAAC;AAClF,CAAC;AAED,SAASK,UAAUA,CAACC,KAAiB,EAAEH,UAAsB,EAAE;EAC3D,MAAMI,WAAW,GAAGL,cAAc,CAACI,KAAK,CAAC;EACzC,MAAME,GAA4B,GAAGD,WAAW,CAACE,MAAM,CAAC,CAACC,GAAG,EAAEC,IAAI,KAAK;IACnE,MAAMC,OAAO,GACTD,IAAI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AOpB,UAAU,CAACC,KAAK,EAAEH,UAAU,CAAC;AACxC;AAEA,OAAO,SAAS0B,WAAWA,CAACZ,MAAM,GAAG,EAAE,EAAE;EACrC,OAAOvB,MAAM,CAACuB,MAAM,CAAC;AACzB","ignoreList":[]}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { Property } from "../../../src/index";
|
|
3
|
-
interface AddWidgetProps {
|
|
4
|
-
name: string;
|
|
5
|
-
type: string;
|
|
6
|
-
}
|
|
7
|
-
declare const AddWidget: <T extends Record<string, unknown>>(props: T & AddWidgetProps) => JSX.Element;
|
|
8
|
-
export interface CardWidget extends Record<string, unknown> {
|
|
9
|
-
title: string;
|
|
10
|
-
description: string;
|
|
11
|
-
button: React.ReactElement;
|
|
12
|
-
}
|
|
13
|
-
interface DashboardConfig extends React.FC<unknown> {
|
|
14
|
-
AddWidget: typeof AddWidget;
|
|
15
|
-
DashboardRenderer: typeof DashboardRenderer;
|
|
16
|
-
}
|
|
17
|
-
export declare const DashboardConfig: DashboardConfig;
|
|
18
|
-
declare const DashboardRenderer: import("@webiny/react-composition").ComposableFC<unknown>;
|
|
19
|
-
interface DashboardViewProps {
|
|
20
|
-
onProperties(properties: Property[]): void;
|
|
21
|
-
}
|
|
22
|
-
export declare const App: React.FC<DashboardViewProps>;
|
|
23
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
interface SettingsGroupProps {
|
|
3
|
-
name: string;
|
|
4
|
-
title?: string;
|
|
5
|
-
icon?: string;
|
|
6
|
-
remove?: boolean;
|
|
7
|
-
replace?: string;
|
|
8
|
-
}
|
|
9
|
-
interface FormFieldProps extends Record<string, unknown> {
|
|
10
|
-
name: string;
|
|
11
|
-
component?: string;
|
|
12
|
-
after?: string;
|
|
13
|
-
remove?: boolean;
|
|
14
|
-
replace?: string;
|
|
15
|
-
}
|
|
16
|
-
declare const PageSettingsView: React.FC<import("./createConfigurableView").ViewProps>;
|
|
17
|
-
declare const PageSettingsConfig: React.FC<{}> & {
|
|
18
|
-
FormField: React.FC<FormFieldProps>;
|
|
19
|
-
SettingsGroup: React.FC<SettingsGroupProps>;
|
|
20
|
-
};
|
|
21
|
-
export { PageSettingsView, PageSettingsConfig };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { Property } from "../../../src/index";
|
|
3
|
-
export interface ViewProps {
|
|
4
|
-
onProperties?(properties: Property[]): void;
|
|
5
|
-
}
|
|
6
|
-
export declare function createConfigurableView(name: string): {
|
|
7
|
-
View: React.FC<ViewProps>;
|
|
8
|
-
Config: React.FC<{}>;
|
|
9
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/__tests__/setupEnv.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
declare const randomFillSync: any;
|
package/__tests__/utils.d.ts
DELETED
package/src/Properties.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
DELETED
package/src/utils.d.ts
DELETED