Skip to content

Commit

Permalink
feat: Support for toggle buttons in button group
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Oct 30, 2024
1 parent 6ba69fa commit 1d96218
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 16 deletions.
46 changes: 42 additions & 4 deletions pages/button-group/permutations.page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import React, { useState } from 'react';
import cloneDeep from 'lodash/cloneDeep';

import ButtonGroup, { ButtonGroupProps } from '~components/button-group';

Expand All @@ -14,16 +15,22 @@ const feedbackGroup: ButtonGroupProps.Group = {
text: 'Vote',
items: [
{
type: 'icon-button',
type: 'icon-toggle-button',
id: 'like',
iconName: 'thumbs-up',
pressedIconName: 'thumbs-up-filled',
text: 'Like',
pressedText: 'Like',
pressed: true,
},
{
type: 'icon-button',
type: 'icon-toggle-button',
id: 'dislike',
iconName: 'thumbs-down',
pressedIconName: 'thumbs-down-filled',
text: 'Dislike',
pressedText: 'Dislike',
pressed: false,
},
],
};
Expand Down Expand Up @@ -98,9 +105,40 @@ export default function () {
<h1>ButtonGroup permutations</h1>
<PermutationsView
permutations={buttonGroupPermutations}
render={permutation => <div>{<ButtonGroup {...permutation} />}</div>}
render={permutation => <div>{<StatefulButtonGroup {...permutation} />}</div>}
/>
</article>
</ScreenshotArea>
);
}

function StatefulButtonGroup(props: ButtonGroupProps) {
const [feedback, setFeedback] = useState<'like' | 'dislike'>('like');
return (
<ButtonGroup
{...props}
items={withFeedbackState(props.items, feedback)}
onItemClick={({ detail }) => {
switch (detail.id) {
case 'like':
return setFeedback(detail.pressed ? 'like' : 'dislike');
case 'dislike':
return setFeedback(detail.pressed ? 'dislike' : 'like');
default:
// not implemented
}
}}
/>
);
}

function withFeedbackState(source: readonly ButtonGroupProps.ItemOrGroup[], feedback: 'like' | 'dislike') {
const clone = cloneDeep(source);
for (const itemOrGroup of clone) {
if (itemOrGroup.type === 'group' && itemOrGroup.text === 'Vote') {
itemOrGroup.items[0].type === 'icon-toggle-button' && (itemOrGroup.items[0].pressed = feedback === 'like');
itemOrGroup.items[1].type === 'icon-toggle-button' && (itemOrGroup.items[1].pressed = feedback === 'dislike');
}
}
return clone;
}
26 changes: 17 additions & 9 deletions pages/button-group/test.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ export default function ButtonGroupPage() {
text: 'Vote',
items: [
{
type: 'icon-button',
type: 'icon-toggle-button',
id: 'like',
iconName: feedback === 'like' ? 'thumbs-up-filled' : 'thumbs-up',
iconName: 'thumbs-up',
pressedIconName: 'thumbs-up-filled',
text: 'Like',
pressedText: 'Liked',
pressed: feedback === 'like',
},
{
type: 'icon-button',
type: 'icon-toggle-button',
id: 'dislike',
iconName: feedback === 'dislike' ? 'thumbs-down-filled' : 'thumbs-down',
iconName: 'thumbs-down',
pressedIconName: 'thumbs-down-filled',
text: 'Dislike',
pressedText: 'Disliked',
pressed: feedback === 'dislike',
},
],
};
Expand All @@ -55,12 +61,14 @@ export default function ButtonGroupPage() {
text: 'Favorite',
items: [
{
type: 'icon-button',
type: 'icon-toggle-button',
id: 'favorite',
iconName: isFavorite ? 'star-filled' : 'star',
iconName: 'star',
pressedIconName: 'star-filled',
text: 'Add to favorites',
pressedText: 'Added to favorites',
loading: loadingId === 'favorite',
popoverFeedback: loadingId === 'favorite' ? '...' : isFavorite ? 'Set as favorite' : 'Removed',
pressed: isFavorite,
},
],
};
Expand Down Expand Up @@ -191,9 +199,9 @@ export default function ButtonGroupPage() {
switch (detail.id) {
case 'like':
case 'dislike':
return syncAction(() => setFeedback(prev => (prev !== detail.id ? (detail.id as 'like' | 'dislike') : 'none')));
return syncAction(() => setFeedback(detail.pressed ? (detail.id as 'like' | 'dislike') : 'none'));
case 'favorite':
return asyncAction(() => setFavorite(prev => !prev));
return asyncAction(() => setFavorite(!!detail.pressed));
case 'send':
return syncAction(() => setCanSend(false));
case 'redo':
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/__snapshots__/documenter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3795,6 +3795,11 @@ exports[`Documenter definition for button-group matches the snapshot: button-gro
"optional": false,
"type": "string",
},
{
"name": "pressed",
"optional": true,
"type": "false | true",
},
],
"type": "object",
},
Expand Down Expand Up @@ -3871,6 +3876,21 @@ use the \`id\` attribute, consider setting it on a parent element instead.",
* \`iconSvg\` (optional, ReactNode) - Custom SVG icon. Equivalent to the \`svg\` slot of the [icon component](/components/icon/).
* \`popoverFeedback\` (optional, string) - Text that appears when the user clicks the button. Use to provide feedback to the user.

* ### icon-toggle-button

* \`id\` (string) - The unique identifier of the button, used as detail in \`onItemClick\` handler and to focus the button using \`ref.focus(id)\`.
* \`text\` (string) - The name shown as a tooltip or menu text for this button.
* \`pressed\` (boolean) - The toggle button pressed state.
* \`disabled\` (optional, boolean) - The disabled state indication for the button.
* \`loading\` (optional, boolean) - The loading state indication for the button.
* \`loadingText\` (optional, string) - The loading text announced to screen readers.
* \`iconName\` (optional, string) - Specifies the name of the icon, used with the [icon component](/components/icon/).
* \`iconUrl\` (optional, string) - Specifies the URL of a custom icon.
* \`iconSvg\` (optional, ReactNode) - Custom SVG icon. Equivalent to the \`svg\` slot of the [icon component](/components/icon/).
* \`pressedIconName\` (optional, string) - Specifies the name of the icon in pressed state, used with the [icon component](/components/icon/).
* \`pressedIconUrl\` (optional, string) - Specifies the URL of a custom icon in pressed state.
* \`pressedIconSvg\` (optional, ReactNode) - Custom SVG icon in pressed state. Equivalent to the \`svg\` slot of the [icon component](/components/icon/).

### menu-dropdown

* \`id\` (string) - The unique identifier of the button, used as detail in \`onItemClick\`.
Expand All @@ -3889,6 +3909,11 @@ group
"optional": false,
"type": "ReadonlyArray<ButtonGroupProps.ItemOrGroup>",
},
{
"name": "tooltipToggle",
"optional": true,
"type": "boolean",
},
{
"description": "Determines the general styling of the button dropdown.
* \`icon\` for icon buttons.",
Expand Down
87 changes: 87 additions & 0 deletions src/button-group/icon-toggle-button-item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { forwardRef } from 'react';
import clsx from 'clsx';

import { warnOnce } from '@cloudscape-design/component-toolkit/internal';

import { ButtonProps } from '../button/interfaces.js';
import Tooltip from '../internal/components/tooltip/index.js';
import { CancelableEventHandler, fireCancelableEvent } from '../internal/events/index.js';
import InternalLiveRegion from '../live-region/internal.js';
import { InternalToggleButton } from '../toggle-button/internal.js';
import { ButtonGroupProps } from './interfaces.js';

import testUtilStyles from './test-classes/styles.css.js';

const IconToggleButtonItem = forwardRef(
(
{
item,
showTooltip,
showFeedback,
onItemClick,
}: {
item: ButtonGroupProps.IconToggleButton;
showTooltip: boolean;
showFeedback: boolean;
onItemClick?: CancelableEventHandler<ButtonGroupProps.ItemClickDetails>;
},
ref: React.Ref<ButtonProps.Ref>
) => {
const containerRef = React.useRef<HTMLDivElement>(null);
const hasIcon = item.iconName || item.iconUrl || item.iconSvg;
const hasPressedIcon = item.pressedIconName || item.pressedIconUrl || item.pressedIconSvg;

if (!hasIcon) {
warnOnce('ButtonGroup', `Missing icon for item with id: ${item.id}`);
}
if (!hasPressedIcon) {
warnOnce('ButtonGroup', `Missing pressed icon for item with id: ${item.id}`);
}

const tooltipContent = item.pressed ? item.pressedText : item.text;
const feedbackContent = item.pressed ? item.pressedPopoverFeedback ?? item.popoverFeedback : item.popoverFeedback;
const canShowTooltip = showTooltip && !item.disabled && !item.loading;
const canShowFeedback = showTooltip && showFeedback && feedbackContent;
return (
<div ref={containerRef}>
<InternalToggleButton
variant="icon"
pressed={item.pressed}
loading={item.loading}
loadingText={item.loadingText}
disabled={item.disabled}
iconName={hasIcon ? item.iconName : 'close'}
iconUrl={item.iconUrl}
iconSvg={item.iconSvg}
pressedIconName={hasIcon ? item.pressedIconName : 'close'}
pressedIconUrl={item.pressedIconUrl}
pressedIconSvg={item.pressedIconUrl}
ariaLabel={tooltipContent}
onChange={event => fireCancelableEvent(onItemClick, { id: item.id, pressed: event.detail.pressed })}
ref={ref}
data-testid={item.id}
data-itemid={item.id}
className={clsx(testUtilStyles.item, testUtilStyles['button-group-item'])}
__title=""
>
{tooltipContent}
</InternalToggleButton>
{(canShowTooltip || canShowFeedback) && (
<Tooltip
trackRef={containerRef}
trackKey={item.id}
value={
(showFeedback && <InternalLiveRegion tagName="span">{feedbackContent}</InternalLiveRegion>) ||
tooltipContent
}
className={clsx(testUtilStyles.tooltip, testUtilStyles['button-group-tooltip'])}
/>
)}
</div>
);
}
);

export default IconToggleButtonItem;
39 changes: 38 additions & 1 deletion src/button-group/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ export interface ButtonGroupProps extends BaseComponentProps {
* * `iconSvg` (optional, ReactNode) - Custom SVG icon. Equivalent to the `svg` slot of the [icon component](/components/icon/).
* * `popoverFeedback` (optional, string) - Text that appears when the user clicks the button. Use to provide feedback to the user.
*
* * ### icon-toggle-button
*
* * `id` (string) - The unique identifier of the button, used as detail in `onItemClick` handler and to focus the button using `ref.focus(id)`.
* * `text` (string) - The name shown as a tooltip or menu text for this button.
* * `pressed` (boolean) - The toggle button pressed state.
* * `disabled` (optional, boolean) - The disabled state indication for the button.
* * `loading` (optional, boolean) - The loading state indication for the button.
* * `loadingText` (optional, string) - The loading text announced to screen readers.
* * `iconName` (optional, string) - Specifies the name of the icon, used with the [icon component](/components/icon/).
* * `iconUrl` (optional, string) - Specifies the URL of a custom icon.
* * `iconSvg` (optional, ReactNode) - Custom SVG icon. Equivalent to the `svg` slot of the [icon component](/components/icon/).
* * `pressedIconName` (optional, string) - Specifies the name of the icon in pressed state, used with the [icon component](/components/icon/).
* * `pressedIconUrl` (optional, string) - Specifies the URL of a custom icon in pressed state.
* * `pressedIconSvg` (optional, ReactNode) - Custom SVG icon in pressed state. Equivalent to the `svg` slot of the [icon component](/components/icon/).
*
* ### menu-dropdown
*
* * `id` (string) - The unique identifier of the button, used as detail in `onItemClick`.
Expand All @@ -67,6 +82,8 @@ export interface ButtonGroupProps extends BaseComponentProps {
* Called when the user clicks on an item, and the item is not disabled. The event detail object contains the id of the clicked item.
*/
onItemClick?: NonCancelableEventHandler<ButtonGroupProps.ItemClickDetails>;

tooltipToggle?: boolean;
}

export interface InternalButtonGroupProps extends ButtonGroupProps, InternalBaseComponentProps {}
Expand All @@ -75,7 +92,7 @@ export namespace ButtonGroupProps {
export type Variant = 'icon';

export type ItemOrGroup = Item | Group;
export type Item = IconButton | MenuDropdown;
export type Item = IconButton | IconToggleButton | MenuDropdown;

export interface IconButton {
type: 'icon-button';
Expand All @@ -91,6 +108,25 @@ export namespace ButtonGroupProps {
popoverFeedback?: React.ReactNode;
}

export interface IconToggleButton {
type: 'icon-toggle-button';
id: string;
text: string;
pressed: boolean;
pressedText: string;
disabled?: boolean;
loading?: boolean;
loadingText?: string;
iconName?: IconProps.Name;
iconUrl?: string;
iconSvg?: React.ReactNode;
pressedIconName?: IconProps.Name;
pressedIconUrl?: string;
pressedIconSvg?: React.ReactNode;
popoverFeedback?: React.ReactNode;
pressedPopoverFeedback?: React.ReactNode;
}

export interface MenuDropdown {
type: 'menu-dropdown';
id: string;
Expand All @@ -109,6 +145,7 @@ export namespace ButtonGroupProps {

export interface ItemClickDetails {
id: string;
pressed?: boolean;
}

export interface Ref {
Expand Down
2 changes: 2 additions & 0 deletions src/button-group/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const InternalButtonGroup = forwardRef(
onItemClick,
ariaLabel,
dropdownExpandToViewport,
tooltipToggle = false,
__internalRootRef = null,
...props
}: InternalButtonGroupProps,
Expand Down Expand Up @@ -158,6 +159,7 @@ const InternalButtonGroup = forwardRef(
setTooltip={setTooltip}
onItemClick={onItemClick}
ref={element => (itemsRef.current[item.id] = element)}
tooltipToggle={tooltipToggle}
/>
);

Expand Down
Loading

0 comments on commit 1d96218

Please sign in to comment.