Skip to content

Commit

Permalink
fix(tags): +n tags for listview (apache#25603)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh authored Oct 18, 2023
1 parent 32e37d8 commit a27a809
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
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;
};

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;

0 comments on commit a27a809

Please sign in to comment.