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