-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for toggle buttons in button group
- Loading branch information
Showing
9 changed files
with
422 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React, { useContext, useState } from 'react'; | ||
|
||
import { | ||
Box, | ||
ButtonGroup, | ||
ButtonGroupProps, | ||
Checkbox, | ||
ExpandableSection, | ||
Header, | ||
SegmentedControl, | ||
SpaceBetween, | ||
StatusIndicator, | ||
} from '~components'; | ||
|
||
import AppContext, { AppContextType } from '../app/app-context'; | ||
import ScreenshotArea from '../utils/screenshot-area'; | ||
|
||
type PageContext = React.Context< | ||
AppContextType<{ | ||
tooltipToggle: boolean; | ||
usePressedText: boolean; | ||
feedbackType: 'none' | 'text' | 'status'; | ||
}> | ||
>; | ||
|
||
const feedbackGroup: ButtonGroupProps.Group = { | ||
type: 'group', | ||
text: 'Vote', | ||
items: [ | ||
{ | ||
type: 'icon-toggle-button', | ||
id: 'like', | ||
iconName: 'thumbs-up', | ||
pressedIconName: 'thumbs-up-filled', | ||
text: 'Like', | ||
pressedText: 'Liked', | ||
pressed: false, | ||
}, | ||
{ | ||
type: 'icon-toggle-button', | ||
id: 'dislike', | ||
iconName: 'thumbs-down', | ||
pressedIconName: 'thumbs-down-filled', | ||
text: 'Dislike', | ||
pressedText: 'Disliked', | ||
pressed: false, | ||
}, | ||
], | ||
}; | ||
|
||
const copy: ButtonGroupProps.Item = { | ||
type: 'icon-button', | ||
id: 'copy', | ||
iconName: 'copy', | ||
text: 'Copy', | ||
popoverFeedback: <StatusIndicator>Copied</StatusIndicator>, | ||
}; | ||
|
||
const add: ButtonGroupProps.Item = { | ||
type: 'icon-button', | ||
id: 'add', | ||
iconName: 'add-plus', | ||
text: 'Add', | ||
}; | ||
|
||
const remove: ButtonGroupProps.Item = { | ||
type: 'icon-button', | ||
id: 'remove', | ||
iconName: 'remove', | ||
text: 'Remove', | ||
}; | ||
|
||
const moreActionsMenu: ButtonGroupProps.MenuDropdown = { | ||
type: 'menu-dropdown', | ||
id: 'more-actions', | ||
text: 'More actions', | ||
items: [remove], | ||
}; | ||
|
||
const actionsGroupsWithMenu: ButtonGroupProps.Group = { | ||
type: 'group', | ||
text: 'Actions', | ||
items: [add, moreActionsMenu], | ||
}; | ||
|
||
export default function () { | ||
const { | ||
urlParams: { tooltipToggle = false, usePressedText = false, feedbackType = 'none' }, | ||
setUrlParams, | ||
} = useContext(AppContext as PageContext); | ||
|
||
return ( | ||
<ScreenshotArea disableAnimations={true}> | ||
<SpaceBetween size="m"> | ||
<Header variant="h1">ButtonGroup interaction variants</Header> | ||
|
||
<ExpandableSection headerText="Settings" variant="container" headingTagOverride="h2" defaultExpanded={true}> | ||
<SpaceBetween size="s" direction="horizontal" alignItems="center"> | ||
<Checkbox | ||
checked={tooltipToggle} | ||
onChange={({ detail }) => setUrlParams({ tooltipToggle: detail.checked })} | ||
> | ||
Toggle tooltip on interaction | ||
</Checkbox> | ||
|
||
<Checkbox | ||
checked={usePressedText} | ||
onChange={({ detail }) => setUrlParams({ usePressedText: detail.checked })} | ||
> | ||
Use different pressed text | ||
</Checkbox> | ||
|
||
<SpaceBetween size="xs" direction="horizontal" alignItems="center"> | ||
<SegmentedControl | ||
label="Popover feedback type" | ||
options={[ | ||
{ id: 'none', text: 'None' }, | ||
{ id: 'text', text: 'Text' }, | ||
{ id: 'status', text: 'Status' }, | ||
]} | ||
selectedId={feedbackType} | ||
onChange={e => setUrlParams({ feedbackType: e.detail.selectedId as any })} | ||
/> | ||
<label htmlFor="min-size-input">Popover feedback type</label> | ||
</SpaceBetween> | ||
</SpaceBetween> | ||
</ExpandableSection> | ||
|
||
<Header variant="h2">Demo</Header> | ||
|
||
<StatefulButtonGroup | ||
variant="icon" | ||
ariaLabel="Chat actions" | ||
items={[feedbackGroup, copy, actionsGroupsWithMenu]} | ||
tooltipToggle={tooltipToggle} | ||
/> | ||
|
||
<Box> | ||
<div id="log"></div> | ||
</Box> | ||
</SpaceBetween> | ||
</ScreenshotArea> | ||
); | ||
} | ||
|
||
function StatefulButtonGroup(props: ButtonGroupProps) { | ||
const [feedback, setFeedback] = useState<'none' | 'like' | 'dislike'>('none'); | ||
const { | ||
urlParams: { usePressedText = false, feedbackType = 'none' }, | ||
} = useContext(AppContext as PageContext); | ||
|
||
const items = traverseItems(props.items, item => { | ||
if (item.type !== 'icon-toggle-button') { | ||
return item; | ||
} | ||
const pressed = item.id === feedback; | ||
const text = usePressedText && pressed ? item.pressedText : item.text; | ||
const renderFeedback = (feedback: string) => | ||
feedbackType === 'status' ? <StatusIndicator>{feedback}</StatusIndicator> : feedback; | ||
const popoverFeedback = feedbackType !== 'none' ? renderFeedback(item.text + 'd') : undefined; | ||
const pressedPopoverFeedback = feedbackType !== 'none' ? renderFeedback('Removed ' + item.text) : undefined; | ||
return { ...item, pressed, text, popoverFeedback, pressedPopoverFeedback }; | ||
}); | ||
|
||
function addLog(text: string) { | ||
const entry = document.createElement('div'); | ||
entry.textContent = text; | ||
document.querySelector('#log')!.append(entry); | ||
} | ||
|
||
return ( | ||
<ButtonGroup | ||
{...props} | ||
items={items} | ||
onItemClick={({ detail }) => { | ||
switch (detail.id) { | ||
case 'like': | ||
return setFeedback(detail.pressed ? 'like' : 'none'); | ||
case 'dislike': | ||
return setFeedback(detail.pressed ? 'dislike' : 'none'); | ||
case 'copy': | ||
return addLog('Copied'); | ||
case 'add': | ||
return addLog('Added'); | ||
case 'remove': | ||
return addLog('Removed'); | ||
default: | ||
// not implemented | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
function traverseItems( | ||
source: readonly ButtonGroupProps.ItemOrGroup[], | ||
onItem: (item: ButtonGroupProps.Item) => ButtonGroupProps.Item | ||
): ButtonGroupProps.ItemOrGroup[] { | ||
return source.map(itemOrGroup => { | ||
if (itemOrGroup.type === 'group') { | ||
return { ...itemOrGroup, items: traverseItems(itemOrGroup.items, onItem) } as ButtonGroupProps.Group; | ||
} else { | ||
return onItem(itemOrGroup); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.