Skip to content

LG-5136: render functional components to get node text content #2908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/common-news-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/lib': patch
---

Additional support for getting node text content from functional components.
71 changes: 71 additions & 0 deletions packages/lib/src/getNodeTextContent/getNodeTextContent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';

import getNodeTextContent from '.';

describe('packages/lib/src/getNodeTextContent', () => {
describe('getNodeTextContent', () => {
test('returns empty string for undefined node', () => {
const result = getNodeTextContent(undefined);
expect(result).toBe('');
});

test('returns correct text content for a string node', () => {
const result = getNodeTextContent('leafy green');
expect(result).toBe('leafy green');
});

test('returns correct text content for a number node', () => {
const result = getNodeTextContent(123);
expect(result).toBe('123');
});

test('returns concatenated text content for an array of nodes', () => {
const result = getNodeTextContent(['leafy', 'green', false]);
expect(result).toBe('leafy green');
});

test('returns text content for intrinsic elements', () => {
const element = React.createElement(
'div',
null,
'leafy',
React.createElement('span', null, 'green'),
);
const result = getNodeTextContent(element);
expect(result).toBe('leafy green');
});

test('returns text content for functional components', () => {
const FunctionalComponent = ({ title }: { title: string }) => {
return React.createElement('h1', null, 'title: ', title);
};

const element = React.createElement(FunctionalComponent, {
title: 'leafy green',
});

const result = getNodeTextContent(element);

expect(result).toBe('title: leafy green');
});

test('returns text content for nested functional components', () => {
const FunctionalComponent = ({ title }: { title: string }) => {
return React.createElement(
'div',
null,
'title: ',
React.createElement('span', null, title),
);
};

const element = React.createElement(FunctionalComponent, {
title: 'leafy green',
});

const result = getNodeTextContent(element);

expect(result).toBe('title: leafy green');
});
});
});
15 changes: 12 additions & 3 deletions packages/lib/src/getNodeTextContent/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement, ReactNode, ReactText } from 'react';
import { FunctionComponent, ReactElement, ReactNode, ReactText } from 'react';

/**
* Returns the text string of a React node
Expand All @@ -12,14 +12,23 @@ export default function getNodeTextContent(node?: ReactNode): string {
return node.map(getNodeTextContent).join(' ').trim();
}

if (hasChildren(node)) {
if (isReactElement(node)) {
if (isFunctionalComponent(node)) {
const Component = node.type as FunctionComponent<any>;
return getNodeTextContent(Component(node.props));
}

return getNodeTextContent(node.props.children);
}

return '';
}

function hasChildren(item?: any): item is ReactElement {
function isFunctionalComponent(item?: any): item is FunctionComponent<any> {
return isReactElement(item) && typeof item.type === 'function';
}

function isReactElement(item?: any): item is ReactElement {
return item && typeof item === 'object' && item.props;
}

Expand Down