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
5 changes: 5 additions & 0 deletions src/organisms/ToggleGroup/ToggleGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { colors } from 'src/atoms/style';
interface StoryComponentProps extends ToggleGroupProps {
withIcons?: boolean;
onlyIcons?: boolean;
withTooltips?: boolean;
Comment thread
mariush2 marked this conversation as resolved.
disabled?: boolean;
}

Expand All @@ -19,6 +20,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
matchParentWidth,
withIcons = false,
onlyIcons = false,
withTooltips = false,
disabled = false,
}) => {
const [recentlyPublished, setRecentlyPublished] = useState(false);
Expand Down Expand Up @@ -50,6 +52,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
{...(onlyIcons
? {
icon: new_label,
tooltip: withTooltips ? 'Recently published' : undefined,
}
: {
label: 'Recently published',
Expand All @@ -62,6 +65,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
{...(onlyIcons
? {
icon: person,
tooltip: withTooltips ? 'My files' : undefined,
}
: {
label: 'My files',
Expand All @@ -74,6 +78,7 @@ const ToggleGroup: FC<StoryComponentProps> = ({
{...(onlyIcons
? {
icon: star_outlined,
tooltip: withTooltips ? 'Favourites' : undefined,
}
: {
label: 'Favourites',
Expand Down
28 changes: 28 additions & 0 deletions src/organisms/ToggleGroup/ToggleGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ test('Works with icons only', async () => {
}
});

test('Works with icons and tooltip', async () => {
const options = new Array(faker.number.int({ min: 2, max: 3 }))
.fill(null)
.map(() => faker.vehicle.vehicle());
const handlers = options.map(() => vi.fn());
render(
<ToggleGroup>
{options.map((option, index) => (
<ToggleGroup.Option
key={option}
onToggle={handlers[index]}
icon={car}
tooltip="Car icon"
tooltipPlacement="top"
checked={false}
/>
))}
</ToggleGroup>
);
const user = userEvent.setup();

const icon = screen.getAllByTestId('eds-icon-path')[0];

await user.hover(icon);

expect(await screen.findByRole('tooltip')).toHaveTextContent('Car icon');
});

test('Match parent height works as expected', async () => {
const options = new Array(faker.number.int({ min: 2, max: 3 }))
.fill(null)
Expand Down
14 changes: 13 additions & 1 deletion src/organisms/ToggleGroup/ToggleGroup.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReactElement } from 'react';

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

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

interface ToggleGroupOptionOnlyIcon {
icon: IconData;
tooltip?: undefined;
}

interface ToggleGroupOptionOnlyIconWithTooltip {
icon: IconData;
tooltip: string;
tooltipPlacement?: TooltipProps['placement'];
}

export type ToggleGroupOption = {
onToggle: (newValue: boolean) => void;
checked: boolean;
disabled?: boolean;
} & (ToggleGroupOptionWithLabel | ToggleGroupOptionOnlyIcon);
} & (
| ToggleGroupOptionWithLabel
| ToggleGroupOptionOnlyIcon
| ToggleGroupOptionOnlyIconWithTooltip
);
Comment thread
mariush2 marked this conversation as resolved.

/**
* @param variant - Defaults to 'filled'
Expand Down
26 changes: 24 additions & 2 deletions src/organisms/ToggleGroup/ToggleGroupOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Icon, Typography } from '@equinor/eds-core-react';

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

import styled from 'styled-components';

Expand Down Expand Up @@ -73,6 +74,16 @@ export const ToggleGroupOption = forwardRef<
HTMLButtonElement,
ToggleGroupOptionProps
>(({ checked, onToggle, icon, disabled, ...rest }, ref) => {
const buttonProps =
'tooltip' in rest
? {
...rest,
tooltip: undefined,
toolTipPlacement: undefined,
}
: {
...rest,
};
const handleOnClick = () => {
onToggle(!checked);
};
Expand All @@ -83,9 +94,20 @@ export const ToggleGroupOption = forwardRef<
aria-checked={checked}
onClick={handleOnClick}
disabled={disabled}
{...rest}
{...buttonProps}
>
{icon ? <Icon data={icon} size={24} /> : null}
{icon ? (
'tooltip' in rest && rest.tooltip ? (
<OptionalTooltip
title={rest.tooltip}
placement={rest.tooltipPlacement}
Comment thread
mariush2 marked this conversation as resolved.
>
<Icon data={icon} size={24} />
</OptionalTooltip>
Comment thread
mariush2 marked this conversation as resolved.
) : (
<Icon data={icon} size={24} />
)
) : null}
Comment thread
mariush2 marked this conversation as resolved.
{'label' in rest ? (
<Typography as="span" variant="button" group="navigation">
{rest.label}
Expand Down
Loading