fix(tags): +n tags for listview (#25603)

This commit is contained in:
Hugh A. Miles II 2023-10-17 23:39:15 -04:00 committed by GitHub
parent 32e37d8cda
commit a27a809f07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 32 deletions

View File

@ -18,6 +18,7 @@
*/
import React from 'react';
import { render } from 'spec/helpers/testing-library';
import { screen } from '@testing-library/react';
import TagType from 'src/types/TagType';
import Tag from './Tag';
@ -33,3 +34,23 @@ test('should render', () => {
const { container } = render(<Tag {...mockedProps} />);
expect(container).toBeInTheDocument();
});
test('should render shortname properly', () => {
const { container } = render(<Tag {...mockedProps} />);
expect(container).toBeInTheDocument();
expect(screen.getByTestId('tag')).toBeInTheDocument();
expect(screen.getByTestId('tag')).toHaveTextContent(mockedProps.name);
});
test('should render longname properly', () => {
const longNameProps = {
...mockedProps,
name: 'very-long-tag-name-that-truncates',
};
const { container } = render(<Tag {...longNameProps} />);
expect(container).toBeInTheDocument();
expect(screen.getByTestId('tag')).toBeInTheDocument();
expect(screen.getByTestId('tag')).toHaveTextContent(
`${longNameProps.name.slice(0, 20)}...`,
);
});

View File

@ -31,6 +31,8 @@ const StyledTag = styled(AntdTag)`
`};
`;
const MAX_DISPLAY_CHAR = 20;
const Tag = ({
name,
id,
@ -38,49 +40,47 @@ const Tag = ({
onDelete = undefined,
editable = false,
onClick = undefined,
toolTipTitle = name,
}: TagType) => {
const isLongTag = useMemo(() => name.length > 20, [name]);
const isLongTag = useMemo(() => name.length > MAX_DISPLAY_CHAR, [name]);
const tagDisplay = isLongTag ? `${name.slice(0, MAX_DISPLAY_CHAR)}...` : name;
const handleClose = () => (index ? onDelete?.(index) : null);
const tagElem = (
<>
{editable ? (
<StyledTag
key={id}
closable={editable}
onClose={handleClose}
color="blue"
>
{isLongTag ? `${name.slice(0, 20)}...` : name}
</StyledTag>
<Tooltip title={toolTipTitle} key={toolTipTitle}>
<StyledTag
key={id}
closable={editable}
onClose={handleClose}
color="blue"
>
{tagDisplay}
</StyledTag>
</Tooltip>
) : (
<StyledTag role="link" key={id} onClick={onClick}>
{id ? (
<a
href={`/superset/all_entities/?id=${id}`}
target="_blank"
rel="noreferrer"
>
{isLongTag ? `${name.slice(0, 20)}...` : name}
</a>
) : isLongTag ? (
`${name.slice(0, 20)}...`
) : (
name
)}
</StyledTag>
<Tooltip title={toolTipTitle} key={toolTipTitle}>
<StyledTag data-test="tag" role="link" key={id} onClick={onClick}>
{id ? (
<a
href={`/superset/all_entities/?id=${id}`}
target="_blank"
rel="noreferrer"
>
{tagDisplay}
</a>
) : (
tagDisplay
)}
</StyledTag>
</Tooltip>
)}
</>
);
return isLongTag ? (
<Tooltip title={name} key={name}>
{tagElem}
</Tooltip>
) : (
tagElem
);
return tagElem;
};
export default Tag;

View File

@ -83,7 +83,11 @@ const TagsList = ({
/>
))}
{tags.length > tempMaxTags ? (
<Tag name={`+${extraTags}...`} onClick={expand} />
<Tag
name={`+${extraTags}...`}
onClick={expand}
toolTipTitle={tags.map(t => t.name).join(', ')}
/>
) : null}
</>
) : (

View File

@ -27,6 +27,7 @@ export interface TagType {
onClick?: MouseEventHandler<HTMLSpanElement>;
name: string;
index?: number | undefined;
toolTipTitle?: string;
}
export default TagType;