@webiny/react-properties 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.7be00a75a9
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/DevToolsSection.d.ts +40 -0
- package/DevToolsSection.js +54 -0
- package/DevToolsSection.js.map +1 -0
- package/Properties.d.ts +60 -0
- package/Properties.js +130 -191
- package/Properties.js.map +1 -1
- package/PropertyPriority.d.ts +8 -0
- package/PropertyPriority.js +11 -0
- package/PropertyPriority.js.map +1 -0
- package/README.md +7 -61
- package/createConfigurableComponent.d.ts +15 -0
- package/createConfigurableComponent.js +67 -0
- package/createConfigurableComponent.js.map +1 -0
- package/domain/PropertyStore.d.ts +51 -0
- package/domain/PropertyStore.js +154 -0
- package/domain/PropertyStore.js.map +1 -0
- package/domain/index.d.ts +1 -0
- package/domain/index.js +1 -0
- package/index.d.ts +7 -0
- package/index.js +7 -31
- package/package.json +19 -17
- package/useDebugConfig.d.ts +32 -0
- package/useDebugConfig.js +45 -0
- package/useDebugConfig.js.map +1 -0
- package/useIdGenerator.d.ts +1 -0
- package/useIdGenerator.js +23 -0
- package/useIdGenerator.js.map +1 -0
- package/utils.d.ts +3 -0
- package/utils.js +42 -48
- 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/index.js.map +0 -1
- package/src/Properties.d.ts +0 -38
- package/src/index.d.ts +0 -2
- package/src/utils.d.ts +0 -3
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type DevToolsView = "browse" | "raw";
|
|
2
|
+
interface DevToolsSectionProps {
|
|
3
|
+
name: string;
|
|
4
|
+
data: unknown;
|
|
5
|
+
/**
|
|
6
|
+
* Group name for the sidebar. Items with the same group appear
|
|
7
|
+
* under the same header. Defaults to "Sections".
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* <DevToolsSection name="Article" group="CMS" data={model} />
|
|
12
|
+
* <DevToolsSection name="Author" group="CMS" data={author} />
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
group?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Which views to show in the detail panel.
|
|
18
|
+
* - `"browse"` — split view with root keys on the left, value tree on the right
|
|
19
|
+
* - `"raw"` — full JSON tree view
|
|
20
|
+
*
|
|
21
|
+
* Accepts a single view or an array. First item is the default tab.
|
|
22
|
+
* @default ["browse", "raw"]
|
|
23
|
+
*/
|
|
24
|
+
views?: DevToolsView | DevToolsView[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Registers a named section in the Webiny DevTools extension.
|
|
28
|
+
* Renders nothing — purely a data registration side-effect.
|
|
29
|
+
*
|
|
30
|
+
* When the component unmounts (e.g., route change), the section
|
|
31
|
+
* is automatically removed from DevTools.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* <DevToolsSection name="CMS Model" data={model} />
|
|
36
|
+
* <DevToolsSection name="Article" group="CMS" data={model} views="raw" />
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function DevToolsSection({ name, data, group, views }: DevToolsSectionProps): null;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { getHook } from "./useDebugConfig.js";
|
|
3
|
+
function DevToolsSection({ name, data, group, views }) {
|
|
4
|
+
const dataKey = safeStringify(data);
|
|
5
|
+
useEffect(()=>{
|
|
6
|
+
if ("development" !== process.env.NODE_ENV) return;
|
|
7
|
+
const normalizedViews = views ? Array.isArray(views) ? views : [
|
|
8
|
+
views
|
|
9
|
+
] : [
|
|
10
|
+
"browse",
|
|
11
|
+
"raw"
|
|
12
|
+
];
|
|
13
|
+
let plainData;
|
|
14
|
+
try {
|
|
15
|
+
plainData = JSON.parse(dataKey);
|
|
16
|
+
} catch {
|
|
17
|
+
plainData = data;
|
|
18
|
+
}
|
|
19
|
+
const hook = getHook();
|
|
20
|
+
hook.sections[name] = {
|
|
21
|
+
data: plainData,
|
|
22
|
+
group: group || "Sections",
|
|
23
|
+
views: normalizedViews,
|
|
24
|
+
updatedAt: Date.now()
|
|
25
|
+
};
|
|
26
|
+
hook.revision++;
|
|
27
|
+
return ()=>{
|
|
28
|
+
if (window.__WEBINY_DEVTOOLS_HOOK__) {
|
|
29
|
+
delete window.__WEBINY_DEVTOOLS_HOOK__.sections[name];
|
|
30
|
+
window.__WEBINY_DEVTOOLS_HOOK__.revision++;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}, [
|
|
34
|
+
name,
|
|
35
|
+
dataKey,
|
|
36
|
+
group,
|
|
37
|
+
Array.isArray(views) ? views.join(",") : views
|
|
38
|
+
]);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function safeStringify(value) {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.stringify(value, (_key, v)=>{
|
|
44
|
+
if ("function" == typeof v) return `[Function: ${v.name || "anonymous"}]`;
|
|
45
|
+
if (void 0 === v) return "[undefined]";
|
|
46
|
+
return v;
|
|
47
|
+
});
|
|
48
|
+
} catch {
|
|
49
|
+
return String(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export { DevToolsSection };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=DevToolsSection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DevToolsSection.js","sources":["../src/DevToolsSection.tsx"],"sourcesContent":["import { useEffect } from \"react\";\nimport { getHook } from \"./useDebugConfig.js\";\n\ntype DevToolsView = \"browse\" | \"raw\";\n\ninterface DevToolsSectionProps {\n name: string;\n data: unknown;\n /**\n * Group name for the sidebar. Items with the same group appear\n * under the same header. Defaults to \"Sections\".\n *\n * @example\n * ```tsx\n * <DevToolsSection name=\"Article\" group=\"CMS\" data={model} />\n * <DevToolsSection name=\"Author\" group=\"CMS\" data={author} />\n * ```\n */\n group?: string;\n /**\n * Which views to show in the detail panel.\n * - `\"browse\"` — split view with root keys on the left, value tree on the right\n * - `\"raw\"` — full JSON tree view\n *\n * Accepts a single view or an array. First item is the default tab.\n * @default [\"browse\", \"raw\"]\n */\n views?: DevToolsView | DevToolsView[];\n}\n\n/**\n * Registers a named section in the Webiny DevTools extension.\n * Renders nothing — purely a data registration side-effect.\n *\n * When the component unmounts (e.g., route change), the section\n * is automatically removed from DevTools.\n *\n * @example\n * ```tsx\n * <DevToolsSection name=\"CMS Model\" data={model} />\n * <DevToolsSection name=\"Article\" group=\"CMS\" data={model} views=\"raw\" />\n * ```\n */\nexport function DevToolsSection({ name, data, group, views }: DevToolsSectionProps) {\n // Snapshot the data on every render to get a stable, plain value.\n // This also ensures that if data is a MobX observable, we capture\n // the current state as a plain object (no proxies on the hook).\n const dataKey = safeStringify(data);\n\n useEffect(() => {\n if (process.env.NODE_ENV !== \"development\") {\n return;\n }\n\n const normalizedViews: DevToolsView[] = views\n ? Array.isArray(views)\n ? views\n : [views]\n : [\"browse\", \"raw\"];\n\n let plainData: unknown;\n try {\n plainData = JSON.parse(dataKey);\n } catch {\n plainData = data;\n }\n\n const hook = getHook();\n hook.sections[name] = {\n data: plainData,\n group: group || \"Sections\",\n views: normalizedViews,\n updatedAt: Date.now()\n };\n hook.revision++;\n\n return () => {\n if (window.__WEBINY_DEVTOOLS_HOOK__) {\n delete window.__WEBINY_DEVTOOLS_HOOK__.sections[name];\n window.__WEBINY_DEVTOOLS_HOOK__.revision++;\n }\n };\n }, [name, dataKey, group, Array.isArray(views) ? views.join(\",\") : views]);\n\n return null;\n}\n\nfunction safeStringify(value: unknown): string {\n try {\n return JSON.stringify(value, (_key, v) => {\n if (typeof v === \"function\") {\n return `[Function: ${v.name || \"anonymous\"}]`;\n }\n if (typeof v === \"undefined\") {\n return \"[undefined]\";\n }\n return v;\n });\n } catch {\n return String(value);\n }\n}\n"],"names":["DevToolsSection","name","data","group","views","dataKey","safeStringify","useEffect","process","normalizedViews","Array","plainData","JSON","hook","getHook","Date","window","value","_key","v","String"],"mappings":";;AA2CO,SAASA,gBAAgB,EAAEC,IAAI,EAAEC,IAAI,EAAEC,KAAK,EAAEC,KAAK,EAAwB;IAI9E,MAAMC,UAAUC,cAAcJ;IAE9BK,UAAU;QACN,IAAIC,AAAyB,kBAAzBA,QAAQ,GAAG,CAAC,QAAQ,EACpB;QAGJ,MAAMC,kBAAkCL,QAClCM,MAAM,OAAO,CAACN,SACVA,QACA;YAACA;SAAM,GACX;YAAC;YAAU;SAAM;QAEvB,IAAIO;QACJ,IAAI;YACAA,YAAYC,KAAK,KAAK,CAACP;QAC3B,EAAE,OAAM;YACJM,YAAYT;QAChB;QAEA,MAAMW,OAAOC;QACbD,KAAK,QAAQ,CAACZ,KAAK,GAAG;YAClB,MAAMU;YACN,OAAOR,SAAS;YAChB,OAAOM;YACP,WAAWM,KAAK,GAAG;QACvB;QACAF,KAAK,QAAQ;QAEb,OAAO;YACH,IAAIG,OAAO,wBAAwB,EAAE;gBACjC,OAAOA,OAAO,wBAAwB,CAAC,QAAQ,CAACf,KAAK;gBACrDe,OAAO,wBAAwB,CAAC,QAAQ;YAC5C;QACJ;IACJ,GAAG;QAACf;QAAMI;QAASF;QAAOO,MAAM,OAAO,CAACN,SAASA,MAAM,IAAI,CAAC,OAAOA;KAAM;IAEzE,OAAO;AACX;AAEA,SAASE,cAAcW,KAAc;IACjC,IAAI;QACA,OAAOL,KAAK,SAAS,CAACK,OAAO,CAACC,MAAMC;YAChC,IAAI,AAAa,cAAb,OAAOA,GACP,OAAO,CAAC,WAAW,EAAEA,EAAE,IAAI,IAAI,YAAY,CAAC,CAAC;YAEjD,IAAI,AAAa,WAANA,GACP,OAAO;YAEX,OAAOA;QACX;IACJ,EAAE,OAAM;QACJ,OAAOC,OAAOH;IAClB;AACJ"}
|
package/Properties.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { PropertyStore } from "./domain/index.js";
|
|
3
|
+
export interface ConnectToPropertiesProps {
|
|
4
|
+
name: string;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare const ConnectToProperties: ({ name, children }: ConnectToPropertiesProps) => React.JSX.Element;
|
|
8
|
+
export interface Property {
|
|
9
|
+
id: string;
|
|
10
|
+
parent: string;
|
|
11
|
+
name: string;
|
|
12
|
+
value?: unknown;
|
|
13
|
+
array?: boolean;
|
|
14
|
+
$isFirst?: boolean;
|
|
15
|
+
$isLast?: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface AddPropertyOptions {
|
|
18
|
+
after?: string;
|
|
19
|
+
before?: string;
|
|
20
|
+
priority?: number;
|
|
21
|
+
}
|
|
22
|
+
interface PropertiesContext {
|
|
23
|
+
name?: string;
|
|
24
|
+
store: PropertyStore;
|
|
25
|
+
getAncestor(name: string): PropertiesContext | undefined;
|
|
26
|
+
getObject<T = unknown>(): T;
|
|
27
|
+
addProperty(property: Property, options?: AddPropertyOptions): void;
|
|
28
|
+
removeProperty(id: string): void;
|
|
29
|
+
replaceProperty(id: string, property: Property): void;
|
|
30
|
+
}
|
|
31
|
+
declare const PropertiesContext: React.Context<PropertiesContext | undefined>;
|
|
32
|
+
interface PropertiesProps {
|
|
33
|
+
name?: string;
|
|
34
|
+
onChange?(properties: Property[]): void;
|
|
35
|
+
children: React.ReactNode;
|
|
36
|
+
}
|
|
37
|
+
export declare const Properties: ({ name, onChange, children }: PropertiesProps) => React.JSX.Element;
|
|
38
|
+
export declare function useProperties(): PropertiesContext;
|
|
39
|
+
export declare function useMaybeProperties(): PropertiesContext | undefined;
|
|
40
|
+
export declare function useAncestorByName(name: string | undefined): PropertiesContext | undefined;
|
|
41
|
+
interface PropertyProps {
|
|
42
|
+
id?: string;
|
|
43
|
+
name: string;
|
|
44
|
+
value?: unknown;
|
|
45
|
+
array?: boolean;
|
|
46
|
+
after?: string;
|
|
47
|
+
before?: string;
|
|
48
|
+
replace?: string;
|
|
49
|
+
remove?: boolean;
|
|
50
|
+
parent?: string;
|
|
51
|
+
root?: boolean;
|
|
52
|
+
children?: React.ReactNode;
|
|
53
|
+
}
|
|
54
|
+
export declare function useParentProperty(): Property | undefined;
|
|
55
|
+
interface AncestorMatch {
|
|
56
|
+
[key: string]: string | boolean | number | null | undefined;
|
|
57
|
+
}
|
|
58
|
+
export declare function useAncestor(params: AncestorMatch): Property | undefined;
|
|
59
|
+
export declare const Property: ({ id, name, value, children, after, before, replace, remove, array, root, parent }: PropertyProps) => React.JSX.Element | null;
|
|
60
|
+
export {};
|
package/Properties.js
CHANGED
|
@@ -1,201 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var _utils = require("./utils");
|
|
23
|
-
|
|
24
|
-
function removeByParent(id, properties) {
|
|
25
|
-
return properties.filter(function (prop) {
|
|
26
|
-
return prop.parent === id;
|
|
27
|
-
}).reduce(function (acc, item) {
|
|
28
|
-
return removeByParent(item.id, acc.filter(function (prop) {
|
|
29
|
-
return prop.id !== item.id;
|
|
30
|
-
}));
|
|
31
|
-
}, properties);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var PropertiesContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
35
|
-
|
|
36
|
-
var Properties = function Properties(_ref) {
|
|
37
|
-
var onChange = _ref.onChange,
|
|
38
|
-
children = _ref.children;
|
|
39
|
-
|
|
40
|
-
var _useState = (0, _react.useState)([]),
|
|
41
|
-
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
42
|
-
properties = _useState2[0],
|
|
43
|
-
setProperties = _useState2[1];
|
|
44
|
-
|
|
45
|
-
(0, _react.useEffect)(function () {
|
|
46
|
-
if (onChange) {
|
|
47
|
-
onChange(properties);
|
|
48
|
-
}
|
|
49
|
-
}, [properties]);
|
|
50
|
-
var context = (0, _react.useMemo)(function () {
|
|
51
|
-
return {
|
|
52
|
-
properties: properties,
|
|
53
|
-
getObject: function getObject() {
|
|
54
|
-
return (0, _utils.toObject)(properties);
|
|
55
|
-
},
|
|
56
|
-
addProperty: function addProperty(property) {
|
|
57
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
58
|
-
setProperties(function (properties) {
|
|
59
|
-
// If a property with this ID already exists, merge the two properties.
|
|
60
|
-
var index = properties.findIndex(function (prop) {
|
|
61
|
-
return prop.id === property.id;
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
if (index > -1) {
|
|
65
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, index)), [(0, _objectSpread2.default)((0, _objectSpread2.default)({}, properties[index]), property)], (0, _toConsumableArray2.default)(properties.slice(index + 1)));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (options.after) {
|
|
69
|
-
var _index = properties.findIndex(function (prop) {
|
|
70
|
-
return prop.id === options.after;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (_index > -1) {
|
|
74
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index + 1)), [property], (0, _toConsumableArray2.default)(properties.slice(_index + 1)));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (options.before) {
|
|
79
|
-
var _index2 = properties.findIndex(function (prop) {
|
|
80
|
-
return prop.id === options.before;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (_index2 > -1) {
|
|
84
|
-
return [].concat((0, _toConsumableArray2.default)(properties.slice(0, _index2)), [property], (0, _toConsumableArray2.default)(properties.slice(_index2)));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return [].concat((0, _toConsumableArray2.default)(properties), [property]);
|
|
89
|
-
});
|
|
90
|
-
},
|
|
91
|
-
removeProperty: function removeProperty(id) {
|
|
92
|
-
setProperties(function (properties) {
|
|
93
|
-
return removeByParent(id, properties.filter(function (prop) {
|
|
94
|
-
return prop.id !== id;
|
|
95
|
-
}));
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
replaceProperty: function replaceProperty(id, property) {
|
|
99
|
-
setProperties(function (properties) {
|
|
100
|
-
var toReplace = properties.findIndex(function (prop) {
|
|
101
|
-
return prop.id === id;
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
if (toReplace > -1) {
|
|
105
|
-
// Replace the property and remove all remaining child properties.
|
|
106
|
-
return removeByParent(id, [].concat((0, _toConsumableArray2.default)(properties.slice(0, toReplace)), [property], (0, _toConsumableArray2.default)(properties.slice(toReplace + 1))));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return properties;
|
|
1
|
+
import react, { createContext, useContext, useEffect, useMemo, useRef } from "react";
|
|
2
|
+
import { getUniqueId, toObject } from "./utils.js";
|
|
3
|
+
import { PropertyStore } from "./domain/index.js";
|
|
4
|
+
import { usePropertyPriority } from "./PropertyPriority.js";
|
|
5
|
+
const PropertiesTargetContext = /*#__PURE__*/ createContext(void 0);
|
|
6
|
+
const ConnectToProperties = ({ name, children })=>/*#__PURE__*/ react.createElement(PropertiesTargetContext.Provider, {
|
|
7
|
+
value: name
|
|
8
|
+
}, children);
|
|
9
|
+
const PropertiesContext = /*#__PURE__*/ createContext(void 0);
|
|
10
|
+
const Properties = ({ name, onChange, children })=>{
|
|
11
|
+
const storeRef = useRef(null);
|
|
12
|
+
if (!storeRef.current) storeRef.current = new PropertyStore();
|
|
13
|
+
const store = storeRef.current;
|
|
14
|
+
let parent;
|
|
15
|
+
try {
|
|
16
|
+
parent = useProperties();
|
|
17
|
+
} catch {}
|
|
18
|
+
useEffect(()=>{
|
|
19
|
+
if (!onChange) return;
|
|
20
|
+
return store.subscribe((properties)=>{
|
|
21
|
+
onChange(properties);
|
|
110
22
|
});
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
23
|
+
}, [
|
|
24
|
+
store,
|
|
25
|
+
onChange
|
|
26
|
+
]);
|
|
27
|
+
const context = useMemo(()=>({
|
|
28
|
+
name,
|
|
29
|
+
store,
|
|
30
|
+
getAncestor (ancestorName) {
|
|
31
|
+
if (!parent) return;
|
|
32
|
+
return parent && parent.name === ancestorName ? parent : parent.getAncestor(ancestorName);
|
|
33
|
+
},
|
|
34
|
+
getObject () {
|
|
35
|
+
return toObject(store.allProperties);
|
|
36
|
+
},
|
|
37
|
+
addProperty (property, options = {}) {
|
|
38
|
+
store.addProperty(property, options);
|
|
39
|
+
},
|
|
40
|
+
removeProperty (id) {
|
|
41
|
+
store.removeProperty(id);
|
|
42
|
+
},
|
|
43
|
+
replaceProperty (id, property) {
|
|
44
|
+
store.replaceProperty(id, property);
|
|
45
|
+
}
|
|
46
|
+
}), [
|
|
47
|
+
store
|
|
48
|
+
]);
|
|
49
|
+
return /*#__PURE__*/ react.createElement(PropertiesContext.Provider, {
|
|
50
|
+
value: context
|
|
51
|
+
}, children);
|
|
117
52
|
};
|
|
118
|
-
|
|
119
|
-
exports.Properties = Properties;
|
|
120
|
-
|
|
121
53
|
function useProperties() {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
throw Error("Properties context provider is missing!");
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return properties;
|
|
54
|
+
const context = useContext(PropertiesContext);
|
|
55
|
+
if (!context) throw Error("Properties context provider is missing!");
|
|
56
|
+
return context;
|
|
129
57
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
58
|
+
function useMaybeProperties() {
|
|
59
|
+
return useContext(PropertiesContext);
|
|
60
|
+
}
|
|
61
|
+
function useAncestorByName(name) {
|
|
62
|
+
const parent = useMaybeProperties();
|
|
63
|
+
return useMemo(()=>{
|
|
64
|
+
if (!name || !parent) return;
|
|
65
|
+
if (parent.name === name) return parent;
|
|
66
|
+
return parent.getAncestor(name);
|
|
67
|
+
}, [
|
|
68
|
+
name
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
const PropertyContext = /*#__PURE__*/ createContext(void 0);
|
|
133
72
|
function useParentProperty() {
|
|
134
|
-
|
|
73
|
+
return useContext(PropertyContext);
|
|
135
74
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
before = _ref2$before === void 0 ? undefined : _ref2$before,
|
|
146
|
-
_ref2$replace = _ref2.replace,
|
|
147
|
-
replace = _ref2$replace === void 0 ? undefined : _ref2$replace,
|
|
148
|
-
_ref2$remove = _ref2.remove,
|
|
149
|
-
remove = _ref2$remove === void 0 ? false : _ref2$remove,
|
|
150
|
-
_ref2$array = _ref2.array,
|
|
151
|
-
array = _ref2$array === void 0 ? false : _ref2$array;
|
|
152
|
-
var uniqueId = (0, _react.useMemo)(function () {
|
|
153
|
-
return id || (0, _utils.getUniqueId)();
|
|
154
|
-
}, []);
|
|
155
|
-
var parent = useParentProperty();
|
|
156
|
-
var properties = useProperties();
|
|
157
|
-
|
|
158
|
-
if (!properties) {
|
|
159
|
-
throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
var addProperty = properties.addProperty,
|
|
163
|
-
removeProperty = properties.removeProperty,
|
|
164
|
-
replaceProperty = properties.replaceProperty;
|
|
165
|
-
var property = {
|
|
166
|
-
id: uniqueId,
|
|
167
|
-
name: name,
|
|
168
|
-
value: value,
|
|
169
|
-
parent: parent ? parent.id : "",
|
|
170
|
-
array: array
|
|
171
|
-
};
|
|
172
|
-
(0, _react.useEffect)(function () {
|
|
173
|
-
if (remove) {
|
|
174
|
-
removeProperty(uniqueId);
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (replace) {
|
|
179
|
-
replaceProperty(replace, property);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
addProperty(property, {
|
|
184
|
-
after: after,
|
|
185
|
-
before: before
|
|
186
|
-
});
|
|
187
|
-
return function () {
|
|
188
|
-
removeProperty(uniqueId);
|
|
75
|
+
function useAncestor(params) {
|
|
76
|
+
const property = useParentProperty();
|
|
77
|
+
const { store } = useProperties();
|
|
78
|
+
const matchOrGetAncestor = (property, params)=>{
|
|
79
|
+
const children = store.getChildrenOf(property.id);
|
|
80
|
+
const matchedProps = children.filter((prop)=>prop.name in params && prop.value === params[prop.name]);
|
|
81
|
+
if (matchedProps.length === Object.keys(params).length) return property;
|
|
82
|
+
const newParent = property.parent ? store.getById(property.parent) : void 0;
|
|
83
|
+
return newParent ? matchOrGetAncestor(newParent, params) : void 0;
|
|
189
84
|
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
85
|
+
return property ? matchOrGetAncestor(property, params) : void 0;
|
|
86
|
+
}
|
|
87
|
+
const Property = ({ id, name, value, children, after, before, replace, remove = false, array = false, root = false, parent })=>{
|
|
88
|
+
const targetName = useContext(PropertiesTargetContext);
|
|
89
|
+
const uniqueId = useMemo(()=>id || getUniqueId(), []);
|
|
90
|
+
const parentProperty = useParentProperty();
|
|
91
|
+
const immediateProperties = useProperties();
|
|
92
|
+
const ancestorByName = useAncestorByName(targetName);
|
|
93
|
+
const previousValue = useRef(value);
|
|
94
|
+
const priority = usePropertyPriority();
|
|
95
|
+
const properties = targetName && ancestorByName ? ancestorByName : immediateProperties;
|
|
96
|
+
if (!properties) throw Error("<Properties> provider is missing higher in the hierarchy!");
|
|
97
|
+
const { addProperty, removeProperty, replaceProperty, store: propertyStore } = properties;
|
|
98
|
+
const parentId = parent ? parent : root ? "" : parentProperty?.id || "";
|
|
99
|
+
const property = {
|
|
100
|
+
id: uniqueId,
|
|
101
|
+
name,
|
|
102
|
+
value,
|
|
103
|
+
parent: parentId,
|
|
104
|
+
array
|
|
105
|
+
};
|
|
106
|
+
if (!remove) propertyStore.registerLookup(property);
|
|
107
|
+
useEffect(()=>{
|
|
108
|
+
if (remove) return void removeProperty(uniqueId);
|
|
109
|
+
if (replace) return void replaceProperty(replace, property);
|
|
110
|
+
const $isFirst = "$first" === before;
|
|
111
|
+
const $isLast = "$last" === after;
|
|
112
|
+
addProperty({
|
|
113
|
+
...property,
|
|
114
|
+
$isFirst,
|
|
115
|
+
$isLast
|
|
116
|
+
}, {
|
|
117
|
+
after,
|
|
118
|
+
before,
|
|
119
|
+
priority
|
|
120
|
+
});
|
|
121
|
+
return ()=>{
|
|
122
|
+
removeProperty(uniqueId);
|
|
123
|
+
};
|
|
124
|
+
}, []);
|
|
125
|
+
useEffect(()=>{
|
|
126
|
+
if (previousValue.current !== value) {
|
|
127
|
+
previousValue.current = value;
|
|
128
|
+
if (!remove && !replace) replaceProperty(uniqueId, property);
|
|
129
|
+
}
|
|
130
|
+
}, [
|
|
131
|
+
value
|
|
132
|
+
]);
|
|
133
|
+
if (children) return /*#__PURE__*/ react.createElement(PropertyContext.Provider, {
|
|
134
|
+
value: property
|
|
195
135
|
}, children);
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return null;
|
|
136
|
+
return null;
|
|
199
137
|
};
|
|
138
|
+
export { ConnectToProperties, Properties, Property, useAncestor, useAncestorByName, useMaybeProperties, useParentProperty, useProperties };
|
|
200
139
|
|
|
201
|
-
|
|
140
|
+
//# 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;IACZ,IAAIJ,QAAJ,EAAc;MACVA,QAAQ,CAACX,UAAD,CAAR;IACH;EACJ,CAJD,EAIG,CAACA,UAAD,CAJH;EAMA,IAAMgB,OAA0B,GAAG,IAAAC,cAAA,EAC/B;IAAA,OAAO;MACHjB,UAAU,EAAVA,UADG;MAEHkB,SAFG,uBAEY;QACX,OAAO,IAAAC,eAAA,EAASnB,UAAT,CAAP;MACH,CAJE;MAKHoB,WALG,uBAKSC,QALT,EAKiC;QAAA,IAAdC,OAAc,uEAAJ,EAAI;QAChCR,aAAa,CAAC,UAAAd,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AAY;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,"file":"Properties.js","sources":["../src/Properties.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo, useRef } from \"react\";\nimport { getUniqueId, toObject } from \"./utils.js\";\nimport { PropertyStore } from \"./domain/index.js\";\nimport { usePropertyPriority } from \"./PropertyPriority.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\ninterface AddPropertyOptions {\n after?: string;\n before?: string;\n priority?: number;\n}\n\ninterface PropertiesContext {\n name?: string;\n store: PropertyStore;\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\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 storeRef = useRef<PropertyStore | null>(null);\n if (!storeRef.current) {\n storeRef.current = new PropertyStore();\n }\n const store = storeRef.current;\n\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 return;\n }\n\n return store.subscribe(properties => {\n onChange(properties);\n });\n }, [store, onChange]);\n\n // Context value is stable — it never changes after mount.\n // Children never re-render due to context changes.\n const context: PropertiesContext = useMemo(\n () => ({\n name,\n store,\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(store.allProperties) as T;\n },\n addProperty(property, options = {}) {\n store.addProperty(property, options);\n },\n removeProperty(id) {\n store.removeProperty(id);\n },\n replaceProperty(id, property) {\n store.replaceProperty(id, property);\n }\n }),\n [store]\n );\n\n return <PropertiesContext.Provider value={context}>{children}</PropertiesContext.Provider>;\n};\n\nexport function useProperties() {\n const context = useContext(PropertiesContext);\n if (!context) {\n throw Error(\"Properties context provider is missing!\");\n }\n\n return context;\n}\n\nexport function useMaybeProperties() {\n return useContext(PropertiesContext);\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 { store } = useProperties();\n\n const matchOrGetAncestor = (\n property: Property,\n params: AncestorMatch\n ): Property | undefined => {\n const children = store.getChildrenOf(property.id);\n const matchedProps = children.filter(\n prop => prop.name in params && prop.value === params[prop.name]\n );\n\n if (matchedProps.length === Object.keys(params).length) {\n return property;\n }\n\n const newParent = property.parent ? store.getById(property.parent) : 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 const priority = usePropertyPriority();\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, store: propertyStore } = properties;\n const parentId = parent ? parent : root ? \"\" : parentProperty?.id || \"\";\n const property = { id: uniqueId, name, value, parent: parentId, array };\n\n // Register in the synchronous lookup during render so useAncestor can find this property.\n if (!remove) {\n propertyStore.registerLookup(property);\n }\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, priority });\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"],"names":["PropertiesTargetContext","createContext","undefined","ConnectToProperties","name","children","PropertiesContext","Properties","onChange","storeRef","useRef","PropertyStore","store","parent","useProperties","useEffect","properties","context","useMemo","ancestorName","toObject","property","options","id","useContext","Error","useMaybeProperties","useAncestorByName","PropertyContext","useParentProperty","useAncestor","params","matchOrGetAncestor","matchedProps","prop","Object","newParent","Property","value","after","before","replace","remove","array","root","targetName","uniqueId","getUniqueId","parentProperty","immediateProperties","ancestorByName","previousValue","priority","usePropertyPriority","addProperty","removeProperty","replaceProperty","propertyStore","parentId","$isFirst","$isLast"],"mappings":";;;;AAKA,MAAMA,0BAA0B,WAAHA,GAAGC,cAAkCC;AAO3D,MAAMC,sBAAsB,CAAC,EAAEC,IAAI,EAAEC,QAAQ,EAA4B,GACrE,WAAP,GACI,oBAACL,wBAAwB,QAAQ;QAAC,OAAOI;OAAOC;AA8BxD,MAAMC,oBAAoB,WAAHA,GAAGL,cAA6CC;AAQhE,MAAMK,aAAa,CAAC,EAAEH,IAAI,EAAEI,QAAQ,EAAEH,QAAQ,EAAmB;IACpE,MAAMI,WAAWC,OAA6B;IAC9C,IAAI,CAACD,SAAS,OAAO,EACjBA,SAAS,OAAO,GAAG,IAAIE;IAE3B,MAAMC,QAAQH,SAAS,OAAO;IAE9B,IAAII;IAEJ,IAAI;QACAA,SAASC;IACb,EAAE,OAAM,CAER;IAEAC,UAAU;QACN,IAAI,CAACP,UACD;QAGJ,OAAOI,MAAM,SAAS,CAACI,CAAAA;YACnBR,SAASQ;QACb;IACJ,GAAG;QAACJ;QAAOJ;KAAS;IAIpB,MAAMS,UAA6BC,QAC/B,IAAO;YACHd;YACAQ;YACA,aAAYO,YAAoB;gBAC5B,IAAI,CAACN,QACD;gBAGJ,OAAOA,UAAUA,OAAO,IAAI,KAAKM,eAC3BN,SACAA,OAAO,WAAW,CAACM;YAC7B;YACA;gBACI,OAAOC,SAASR,MAAM,aAAa;YACvC;YACA,aA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AKb,OAAO;YACjCa,cAAc,OAAO,GAAGb;YACxB,IAAI,CAACI,UAAU,CAACD,SACZe,gBAAgBV,UAAUzB;QAElC;IACJ,GAAG;QAACiB;KAAM;IAEV,IAAIjC,UACA,OAAO,WAAP,GAAO,oBAACuB,gBAAgB,QAAQ;QAAC,OAAOP;OAAWhB;IAGvD,OAAO;AACX"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
interface PropertyPriorityProviderProps {
|
|
3
|
+
priority: number;
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export declare const PropertyPriorityProvider: ({ priority, children }: PropertyPriorityProviderProps) => React.JSX.Element;
|
|
7
|
+
export declare function usePropertyPriority(): number;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import react, { createContext, useContext } from "react";
|
|
2
|
+
const PropertyPriorityContext = /*#__PURE__*/ createContext(0);
|
|
3
|
+
const PropertyPriorityProvider = ({ priority, children })=>/*#__PURE__*/ react.createElement(PropertyPriorityContext.Provider, {
|
|
4
|
+
value: priority
|
|
5
|
+
}, children);
|
|
6
|
+
function usePropertyPriority() {
|
|
7
|
+
return useContext(PropertyPriorityContext);
|
|
8
|
+
}
|
|
9
|
+
export { PropertyPriorityProvider, usePropertyPriority };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=PropertyPriority.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PropertyPriority.js","sources":["../src/PropertyPriority.tsx"],"sourcesContent":["import React, { createContext, useContext } from \"react\";\n\nconst PropertyPriorityContext = createContext(0);\n\ninterface PropertyPriorityProviderProps {\n priority: number;\n children: React.ReactNode;\n}\n\nexport const PropertyPriorityProvider = ({ priority, children }: PropertyPriorityProviderProps) => {\n return (\n <PropertyPriorityContext.Provider value={priority}>\n {children}\n </PropertyPriorityContext.Provider>\n );\n};\n\nexport function usePropertyPriority(): number {\n return useContext(PropertyPriorityContext);\n}\n"],"names":["PropertyPriorityContext","createContext","PropertyPriorityProvider","priority","children","usePropertyPriority","useContext"],"mappings":";AAEA,MAAMA,0BAA0B,WAAHA,GAAGC,cAAc;AAOvC,MAAMC,2BAA2B,CAAC,EAAEC,QAAQ,EAAEC,QAAQ,EAAiC,GACnF,WAAP,GACI,oBAACJ,wBAAwB,QAAQ;QAAC,OAAOG;OACpCC;AAKN,SAASC;IACZ,OAAOC,WAAWN;AACtB"}
|
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._
|