@wordpress/components
Version:
UI components for WordPress.
31 lines (28 loc) • 548 B
text/typescript
const getNodeText = ( node: React.ReactNode ): string => {
if ( node === null ) {
return '';
}
switch ( typeof node ) {
case 'string':
case 'number':
return node.toString();
case 'object': {
if ( node instanceof Array ) {
return node.map( getNodeText ).join( '' );
}
if ( 'props' in node ) {
return getNodeText(
(
node as React.ReactElement<
React.PropsWithChildren< unknown >
>
).props.children
);
}
return '';
}
default:
return '';
}
};
export default getNodeText;