Skip to content

Commit

Permalink
Fix link sometimes removing children (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Aug 14, 2024
1 parent 6120fde commit 49cbe9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-needles-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@jpmorganchase/mosaic-components': patch
---

Fix Link sometimes removing text children.
15 changes: 5 additions & 10 deletions packages/components/src/LinkText/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, isValidElement, ReactNode, Ref } from 'react';
import React, { ComponentPropsWithRef, forwardRef, isValidElement, ReactNode, Ref } from 'react';
import classnames from 'clsx';
import { Icon, IconProps } from '../Icon';

Expand Down Expand Up @@ -90,26 +90,21 @@ export const LinkText = forwardRef<HTMLSpanElement, LinkTextProps>(
className: styles.icon
});

let enhancedChildren: ReactNode = null;
let enhancedChildren: ReactNode = children;
const childrenClassName = classnames(className, styles.root, {
[styles.document]: variant === 'document',
[styles.regular]: variant === 'regular',
[styles.disabled]: disabled
});
if (typeof children === 'string') {
enhancedChildren = children;
} else if (isValidElement(children)) {
const child = React.Children.only(children) as React.ReactElement<{
className: string;
ref: Ref<HTMLSpanElement>;
}>;
if (isValidElement<ComponentPropsWithRef<'span'>>(children)) {
const child = React.Children.only(children);
enhancedChildren = React.cloneElement(child, {
className: classnames(child.props.className, childrenClassName),
ref
});
}
return (
<span className={classnames(className)} ref={ref} {...rest}>
<span className={className} ref={ref} {...rest}>
{startIconAdornment}
<span className={childrenClassName} data-dp-hover={!disabled && hovered}>
{enhancedChildren}
Expand Down

0 comments on commit 49cbe9f

Please sign in to comment.