@gravity-ui/uikit
Version:
Gravity UI base styling and components
45 lines (44 loc) • 1.61 kB
JavaScript
import * as React from 'react';
import { block } from "../utils/cn.js";
export const b = 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
*/
export 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