Debian Codegraph @

node/@patternfly/react-core/dist/esm/helpers/OUIA/ouia.js

raw
1 import { useSSRSafeId } from '../useSSRSafeId';
2 const ouiaPrefix = 'OUIA-Generated-';
3 /**
4 * Get props to conform to OUIA spec.
5 * For functional components, use the useOUIAProps hook instead.
6 *
7 * @param {string} componentType OUIA component type
8 * @param {number|string} id OUIA component id
9 * @param {boolean} ouiaSafe false if in animation
10 */
11 export function getOUIAProps(componentType, id, ouiaSafe = true) {
12 return {
13 'data-ouia-component-type': `PF6/${componentType}`,
14 'data-ouia-safe': ouiaSafe,
15 'data-ouia-component-id': id
16 };
17 }
18 /**
19 * Hooks version of the getOUIAProps function with SSR-safe ID generation.
20 * Can only be used in functional components.
21 *
22 * @param {string} componentType OUIA component type
23 * @param {number|string} id OUIA component id
24 * @param {boolean} ouiaSafe false if in animation
25 * @param {string} variant Optional variant to add to the generated ID
26 */
27 export const useOUIAProps = (componentType, id, ouiaSafe = true, variant) => ({
28 'data-ouia-component-type': `PF6/${componentType}`,
29 'data-ouia-safe': ouiaSafe,
30 'data-ouia-component-id': useOUIAId(componentType, id, variant)
31 });
32 /**
33 * Returns the provided ID or a deterministic SSR-safe generated OUIA ID.
34 * Uses React.useId() under the hood for consistent server/client rendering.
35 *
36 * @param {string} componentType OUIA component type
37 * @param {number|string} id OUIA component id
38 * @param {string} variant Optional variant to add to the generated ID
39 */
40 export const useOUIAId = (componentType, id, variant) => {
41 const generatedId = useSSRSafeId(`${ouiaPrefix}${componentType}-${variant ? `${variant}-` : ''}`);
42 return id !== null && id !== void 0 ? id : generatedId;
43 };
44 let uid = 0;
45 /**
46 * Legacy counter-based OUIA ID generator for class components that cannot use hooks.
47 * Prefer useOUIAId or useOUIAProps for functional components.
48 *
49 * @deprecated Use useOUIAId in functional components instead.
50 */
51 export function getDefaultOUIAId(componentType, variant) {
52 return `${ouiaPrefix}${componentType}-${variant ? `${variant}-` : ''}${++uid}`;
53 }
54 //# sourceMappingURL=ouia.js.map