@yahoo/uds-create-config 5.89.5 → 5.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import { isRef, pathOf, splitRef } from "../framework/utils/refs.js";
2
+ import { isPlainObject } from "../framework/utils/field-path.js";
3
+ import { createSliceMemo } from "../framework/memo.js";
4
+ import { resolveComponentProps } from "../framework/projections.js";
5
+ //#region src/spec/id-props.ts
6
+ const NONE = [];
7
+ const idPropsCache = createSliceMemo({
8
+ kinds: ["component", "source"],
9
+ compute: (_config) => /* @__PURE__ */ new Map()
10
+ });
11
+ /** The prop a `uniqueIdRef` member ref names (`value` in `component:Tabs/Tab#props/value`). */
12
+ function referencedPropName(target) {
13
+ const member = splitRef(target).member;
14
+ if (!member?.startsWith("props/")) return void 0;
15
+ const name = member.slice(6);
16
+ return name && !name.includes("/") ? name : void 0;
17
+ }
18
+ function spaceOf(component, prop) {
19
+ return `${component}#props/${prop}`;
20
+ }
21
+ /** The id props of the component at `path`, a bare path or a spec element's `type`. */
22
+ function idProps({ config, path }) {
23
+ const parts = splitRef(path);
24
+ if (parts.marked && parts.kind !== "component") return NONE;
25
+ const component = pathOf(path);
26
+ const cache = idPropsCache(config);
27
+ const hit = cache.get(component);
28
+ if (hit) return hit;
29
+ const found = [];
30
+ for (const [prop, decl] of Object.entries(resolveComponentProps(config, component))) {
31
+ if (!isPlainObject(decl) || decl.type !== "string") continue;
32
+ const unique = decl.uniqueId === true;
33
+ const target = isRef(decl.uniqueIdRef) ? decl.uniqueIdRef : void 0;
34
+ const referenced = target ? referencedPropName(target) : void 0;
35
+ if (target && referenced) found.push({
36
+ prop,
37
+ space: spaceOf(pathOf(target), referenced),
38
+ unique,
39
+ ref: true
40
+ });
41
+ else if (unique) found.push({
42
+ prop,
43
+ space: spaceOf(component, prop),
44
+ unique,
45
+ ref: false
46
+ });
47
+ }
48
+ const answer = found.length > 0 ? found : NONE;
49
+ cache.set(component, answer);
50
+ return answer;
51
+ }
52
+ //#endregion
53
+ export { idProps, referencedPropName };