Skip to content

Commit 58f724e

Browse files
committed
fix(code-snippet): ensure isForwardRef type guard captures all cases
- ensures forwarded components are properly resolved when checking for the story component instead of being skipped
1 parent 1c784f4 commit 58f724e

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

packages/eui/.storybook/addons/code-snippet/decorators/utils.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,22 @@ export const toPascalCase = (str: string) =>
3333
str.charAt(0).toUpperCase() + str.slice(1);
3434

3535
/* Helpers for React specific checks */
36+
const isReactElement = (el: any): el is ReactElement => el.type !== undefined;
37+
const isExoticComponent = (el: any): el is ExoticComponent =>
38+
el.$$typeof !== undefined;
39+
3640
export const isMemo = (component: ExoticComponent) =>
3741
component.$$typeof === Symbol.for('react.memo');
38-
export const isForwardRef = (component: ExoticComponent) =>
39-
component.$$typeof === Symbol.for('react.forward_ref');
42+
export const isForwardRef = (component: ReactElement | ExoticComponent) => {
43+
// use type guards to ensure keys are available
44+
return isReactElement(component) && isExoticComponent(component.type)
45+
? component.type?.$$typeof === Symbol.for('react.forward_ref')
46+
: isExoticComponent(component)
47+
? component.$$typeof === Symbol.for('react.forward_ref')
48+
: false;
49+
};
4050
export const isFragment = (component: ReactElement | ExoticComponent) => {
4151
// use type guards to ensure keys are available
42-
const isReactElement = (el: any): el is ReactElement => el.type !== undefined;
43-
const isExoticComponent = (el: any): el is ExoticComponent =>
44-
el.$$typeof !== undefined;
45-
4652
return isReactElement(component)
4753
? component.type?.toString().includes('fragment')
4854
: isExoticComponent(component)
@@ -265,7 +271,10 @@ export const getStoryComponent = (
265271
const resolvedDisplayName =
266272
getEmotionComponentDisplayName(resolvedChild);
267273

268-
if (resolvedDisplayName && resolvedDisplayName !== displayName) {
274+
if (
275+
(resolvedDisplayName && resolvedDisplayName !== displayName) ||
276+
isForwardRef(resolvedChild)
277+
) {
269278
resolveChildren(resolvedChild);
270279
} else if (typeof resolvedChild.type !== 'string') {
271280
storyNode = resolvedChild;

packages/eui/src/components/text_truncate/text_truncate.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ enableFunctionToggleControls(ResizeObserver, ['onResize']);
7474
export const StartEndAnchorForSearch: Story = {
7575
parameters: {
7676
controls: { include: ['text', 'calculationDelayMs', 'ellipsis', 'width'] },
77+
codeSnippet: {
78+
resolveStoryElementOnly: true,
79+
},
7780
},
7881
render: function Render(props) {
7982
const [highlight, setHighlight] = useState('');

0 commit comments

Comments
 (0)