Skip to content

Commit 6fccfb2

Browse files
mariush2Copilot
andauthored
✨ Added tooltip props to ToggleGroupOption (#1182)
* ✨ Added tooltip props to ToggleGroupOption * 🏷️ Make types stricter * 🏷️ Enhance tooltip handling in ToggleGroupOption * ✅ Add test for tooltip * Update src/organisms/ToggleGroup/ToggleGroupOption.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/organisms/ToggleGroup/ToggleGroup.types.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ♻️ Copilot feedback --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6900442 commit 6fccfb2

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/organisms/ToggleGroup/ToggleGroup.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { colors } from 'src/atoms/style';
1010
interface StoryComponentProps extends ToggleGroupProps {
1111
withIcons?: boolean;
1212
onlyIcons?: boolean;
13+
withTooltips?: boolean;
1314
disabled?: boolean;
1415
}
1516

@@ -19,6 +20,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
1920
matchParentWidth,
2021
withIcons = false,
2122
onlyIcons = false,
23+
withTooltips = false,
2224
disabled = false,
2325
}) => {
2426
const [recentlyPublished, setRecentlyPublished] = useState(false);
@@ -50,6 +52,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
5052
{...(onlyIcons
5153
? {
5254
icon: new_label,
55+
tooltip: withTooltips ? 'Recently published' : undefined,
5356
}
5457
: {
5558
label: 'Recently published',
@@ -62,6 +65,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
6265
{...(onlyIcons
6366
? {
6467
icon: person,
68+
tooltip: withTooltips ? 'My files' : undefined,
6569
}
6670
: {
6771
label: 'My files',
@@ -74,6 +78,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
7478
{...(onlyIcons
7579
? {
7680
icon: star_outlined,
81+
tooltip: withTooltips ? 'Favourites' : undefined,
7782
}
7883
: {
7984
label: 'Favourites',

src/organisms/ToggleGroup/ToggleGroup.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ test('Works with icons only', async () => {
8181
}
8282
});
8383

84+
test('Works with icons and tooltip', async () => {
85+
const options = new Array(faker.number.int({ min: 2, max: 3 }))
86+
.fill(null)
87+
.map(() => faker.vehicle.vehicle());
88+
const handlers = options.map(() => vi.fn());
89+
render(
90+
<ToggleGroup>
91+
{options.map((option, index) => (
92+
<ToggleGroup.Option
93+
key={option}
94+
onToggle={handlers[index]}
95+
icon={car}
96+
tooltip="Car icon"
97+
tooltipPlacement="top"
98+
checked={false}
99+
/>
100+
))}
101+
</ToggleGroup>
102+
);
103+
const user = userEvent.setup();
104+
105+
const icon = screen.getAllByTestId('eds-icon-path')[0];
106+
107+
await user.hover(icon);
108+
109+
expect(await screen.findByRole('tooltip')).toHaveTextContent('Car icon');
110+
});
111+
84112
test('Match parent height works as expected', async () => {
85113
const options = new Array(faker.number.int({ min: 2, max: 3 }))
86114
.fill(null)

src/organisms/ToggleGroup/ToggleGroup.types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReactElement } from 'react';
22

3+
import { TooltipProps } from '@equinor/eds-core-react';
34
import { IconData } from '@equinor/eds-icons';
45

56
import { ToggleGroupOption } from './ToggleGroupOption';
@@ -11,13 +12,24 @@ interface ToggleGroupOptionWithLabel {
1112

1213
interface ToggleGroupOptionOnlyIcon {
1314
icon: IconData;
15+
tooltip?: undefined;
16+
}
17+
18+
interface ToggleGroupOptionOnlyIconWithTooltip {
19+
icon: IconData;
20+
tooltip: string;
21+
tooltipPlacement?: TooltipProps['placement'];
1422
}
1523

1624
export type ToggleGroupOption = {
1725
onToggle: (newValue: boolean) => void;
1826
checked: boolean;
1927
disabled?: boolean;
20-
} & (ToggleGroupOptionWithLabel | ToggleGroupOptionOnlyIcon);
28+
} & (
29+
| ToggleGroupOptionWithLabel
30+
| ToggleGroupOptionOnlyIcon
31+
| ToggleGroupOptionOnlyIconWithTooltip
32+
);
2133

2234
/**
2335
* @param variant - Defaults to 'filled'

src/organisms/ToggleGroup/ToggleGroupOption.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Icon, Typography } from '@equinor/eds-core-react';
44

55
import { ToggleGroupOption as ToggleGroupOptionType } from './ToggleGroup.types';
66
import { colors, shape, spacings } from 'src/atoms/style';
7+
import { OptionalTooltip } from 'src/molecules';
78

89
import styled from 'styled-components';
910

@@ -73,6 +74,16 @@ export const ToggleGroupOption = forwardRef<
7374
HTMLButtonElement,
7475
ToggleGroupOptionProps
7576
>(({ checked, onToggle, icon, disabled, ...rest }, ref) => {
77+
const buttonProps =
78+
'tooltip' in rest
79+
? {
80+
...rest,
81+
tooltip: undefined,
82+
toolTipPlacement: undefined,
83+
}
84+
: {
85+
...rest,
86+
};
7687
const handleOnClick = () => {
7788
onToggle(!checked);
7889
};
@@ -83,9 +94,20 @@ export const ToggleGroupOption = forwardRef<
8394
aria-checked={checked}
8495
onClick={handleOnClick}
8596
disabled={disabled}
86-
{...rest}
97+
{...buttonProps}
8798
>
88-
{icon ? <Icon data={icon} size={24} /> : null}
99+
{icon ? (
100+
'tooltip' in rest && rest.tooltip ? (
101+
<OptionalTooltip
102+
title={rest.tooltip}
103+
placement={rest.tooltipPlacement}
104+
>
105+
<Icon data={icon} size={24} />
106+
</OptionalTooltip>
107+
) : (
108+
<Icon data={icon} size={24} />
109+
)
110+
) : null}
89111
{'label' in rest ? (
90112
<Typography as="span" variant="button" group="navigation">
91113
{rest.label}

0 commit comments

Comments
 (0)