Skip to content
Merged
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
21 changes: 21 additions & 0 deletions superset-frontend/src/components/Tags/Tag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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)}...`,
);
});
62 changes: 31 additions & 31 deletions superset-frontend/src/components/Tags/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,56 @@ const StyledTag = styled(AntdTag)`
`};
`;

const MAX_DISPLAY_CHAR = 20;

const Tag = ({
name,
id,
index = undefined,
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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test asserting we are rendering the "short" version of the tag when needed?

};

export default Tag;
6 changes: 5 additions & 1 deletion superset-frontend/src/components/Tags/TagsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
</>
) : (
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/types/TagType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface TagType {
onClick?: MouseEventHandler<HTMLSpanElement>;
name: string;
index?: number | undefined;
toolTipTitle?: string;
}

export default TagType;