Skip to content

Commit c0bb30d

Browse files
committed
fix
1 parent 8e49f83 commit c0bb30d

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

packages/gamut/src/Tip/InfoTip/index.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isValidElement,
44
useCallback,
55
useEffect,
6+
useMemo,
67
useRef,
78
useState,
89
} from 'react';
@@ -179,31 +180,32 @@ export const InfoTip: React.FC<InfoTipProps> = ({
179180
...rest,
180181
};
181182

182-
// Helper function to recursively extract text content from React elements
183-
// Converts everything to plain text for screenreader announcements
184183
const extractTextContent = (children: React.ReactNode): string => {
185-
if (!children) return '';
186-
187184
if (typeof children === 'string' || typeof children === 'number') {
188185
return String(children);
189186
}
190187

191-
if (isValidElement(children)) {
192-
const props = children.props as Record<string, unknown>;
193-
if (props.children) {
194-
return extractTextContent(props.children as React.ReactNode);
195-
}
196-
return '';
197-
}
198-
199-
// Children.toArray normalizes arrays and fragments automatically
200188
return Children.toArray(children)
201-
.map((child) => extractTextContent(child))
189+
.map((child) => {
190+
if (typeof child === 'string' || typeof child === 'number') {
191+
return String(child);
192+
}
193+
if (typeof child === 'boolean' || child == null) {
194+
return '';
195+
}
196+
if (isValidElement(child)) {
197+
return extractTextContent(child.props.children);
198+
}
199+
return '';
200+
})
201+
.filter(Boolean)
202202
.join(' ');
203203
};
204204

205+
const extractedTextContent = useMemo(() => extractTextContent(info), [info]);
206+
205207
const screenreaderInfo =
206-
shouldAnnounce && !isTipHidden ? extractTextContent(info) : `\xa0`;
208+
shouldAnnounce && !isTipHidden ? extractedTextContent : `\xa0`;
207209

208210
const text = (
209211
<ScreenreaderNavigableText

0 commit comments

Comments
 (0)