Skip to content
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

fix: (Typography) renderCopy function has bug when child element is r… #688

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 2 additions & 22 deletions packages/semi-ui/typography/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IconSize as Size } from '../icons/index';
import { isUndefined, omit, merge, isString } from 'lodash';
import Tooltip from '../tooltip/index';
import Popover from '../popover/index';
import getRenderText from './util';
import getRenderText, { mergedToString } from './util';
import warning from '@douyinfe/semi-foundation/utils/warning';
import isEnterPress from '@douyinfe/semi-foundation/utils/isEnterPress';
import LocaleConsumer from '../locale/localeConsumer';
Expand Down Expand Up @@ -490,27 +490,7 @@ export default class Base extends Component<BaseTypographyProps, BaseTypographyS
if (!copyable) {
return null;
}
let copyContent: string;
let hasObject = false;
if (Array.isArray(children)) {
copyContent = '';
children.forEach(value => {
if (typeof value === 'object') {
hasObject = true;
}
copyContent += String(value);
});
} else if (typeof children !== 'object') {
copyContent = String(children);
} else {
hasObject = true;
copyContent = String(children);
}

warning(
hasObject,
'Children in Typography is a object, it will case a [object Object] mistake when copy to clipboard.'
);
const copyContent: string = mergedToString(children);
const copyConfig = {
content: copyContent,
duration: 3,
Expand Down
22 changes: 22 additions & 0 deletions packages/semi-ui/typography/util.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ReactDOM from 'react-dom';
import React from 'react';
import { isString, isNumber } from 'lodash';

/**
* The logic of JS for text truncation is referenced from antd typography
Expand Down Expand Up @@ -137,4 +138,25 @@ const getRenderText = (
return resText;
};

const isSingleNode = (child: React.ReactNode) => {
return isString(child) || isNumber(child);
};


export function mergedToString(children: any): string {
const mergedResult = [''];
React.Children.forEach(children, (child) => {
const prevIndex = mergedResult.length - 1;
const prevChild = mergedResult[prevIndex];

if (isSingleNode(child) && isSingleNode(prevChild)) {
mergedResult[prevIndex] = `${prevChild}${child}`;
} else if (child?.props?.children) {
mergedResult.push(mergedToString(child.props.children));
}
});

return mergedResult.join('');
}

export default getRenderText;