@gravity-ui/uikit
Version:
Gravity UI base styling and components
50 lines (49 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
exports.getReactNodeHash = getReactNodeHash;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const cn_1 = require("../utils/cn.js");
exports.b = (0, cn_1.block)('breadcrumbs');
/**
* This is not a full hash, but a stable string that will change when:
* - the text changes at any level of nesting
* - the component type changes
* - primitive props change (className, title, href, etc.)
* - elements are added, removed, or reordered
*/
function getReactNodeHash(children) {
const parts = [];
React.Children.forEach(children, (child) => {
if (child === null || child === undefined || typeof child === 'boolean') {
return;
}
if (typeof child === 'string' || typeof child === 'number') {
parts.push(String(child));
return;
}
if (React.isValidElement(child)) {
const type = child.type;
const typeName = typeof type === 'string'
? type
: type?.displayName ||
type?.name ||
'';
parts.push(typeName);
if (child.key !== null && child.key !== undefined)
parts.push(String(child.key));
const { children: nested, ...rest } = child.props;
for (const [k, v] of Object.entries(rest)) {
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {
parts.push(`${k}:${v}`);
}
}
if (nested) {
parts.push(getReactNodeHash(nested));
}
}
});
return parts.join('|');
}
//# sourceMappingURL=utils.js.map