Repository navigation
feat: Add OptionList component #5180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amanabiy
wants to merge
32
commits into
refactor/move-shared-listbox-parts
from
feat/add-option-list
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
872248a
feat: Add OptionList component
amanabiy 26cddd8
fix: Prevent native page scroll on Home and End in OptionList
amanabiy 57da752
chore: Clean up OptionList tests and dev pages
amanabiy a3cce34
fix: Use own i18n namespace in OptionList
amanabiy 6c1dba6
test: Cover OptionList disabled reason tooltip Escape
amanabiy 5df3cbd
chore: Use page templates for OptionList dev pages
amanabiy 035338c
test: Drop duplicate root scroll container from OptionList scroll page
amanabiy 783b509
feat: Add OptionList renderOption item types
amanabiy d6ef19a
fix: Ignore non-matching group headers in OptionList type-ahead
amanabiy 0ad3a86
fix: Ignore handled keys in OptionList type-ahead
amanabiy c1fcdce
fix: Pass the group state as parent in OptionList renderOption
amanabiy 5b6653d
fix: Keep disabled reason description with custom OptionList options
amanabiy 3ade7cd
fix: Scroll OptionList highlight on repeated keyboard navigation
amanabiy 8bff9a9
fix: Keep OptionList highlight in sync when options change
amanabiy 10d9aa3
fix: Wrap OptionList arrow keys at the first and last highlightable o…
amanabiy e8ebcf5
fix: Start OptionList keyboard focus on the selected option despite h…
amanabiy 644bb97
fix: Require option values in OptionList
amanabiy 1ab18b4
feat: Add renderHighlightedAriaLive to OptionList
amanabiy 3f03952
fix: Skip OptionList scroll when nothing is highlighted
amanabiy e346cf0
test: Split OptionList unit tests by feature
amanabiy ac341c8
test: Prune redundant OptionList unit tests
amanabiy 02f88da
test: Remove OptionList tests covered by listbox tests
amanabiy f69db35
fix: Add gap between icon and content in OptionList options
amanabiy 5104dda
revert: Restore Select and Multiselect type-ahead and renderOption be…
amanabiy 2c824c8
refactor: Use PlainList in OptionList
amanabiy 4175db0
chore: Remove OptionList import restriction from ESLint config
amanabiy 3cd1c53
refactor: Use shared listbox helpers in OptionList
amanabiy f06a65f
refactor: Use shared keyboard navigation in OptionList
amanabiy e8517a3
fix: Require group values in OptionList
amanabiy 55a4d34
fix: Pass group headers to renderHighlightedAriaLive as the consumer'…
amanabiy da97918
chore: Split OptionList permutations into single and multi pages
amanabiy 240cf49
chore: Simplify OptionList options documentation
amanabiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,62 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import React from 'react'; | ||
|
|
||
| import Icon from '~components/icon'; | ||
| import OptionList, { OptionListProps } from '~components/option-list'; | ||
|
|
||
| import { PermutationsPage } from '../app/templates'; | ||
| import PermutationsView from '../utils/permutations-view'; | ||
|
|
||
| export const plain: OptionListProps.Option[] = [ | ||
| { value: '1', label: 'Reduce cost' }, | ||
| { value: '2', label: 'Improve performance', description: 'Prioritize latency and throughput.' }, | ||
| { | ||
| value: '3', | ||
| label: 'Increase reliability across every Region and Availability Zone that hosts the workload', | ||
| description: 'A long label and a long description that wrap onto multiple lines on narrow containers.', | ||
| }, | ||
| { value: '4', label: 'Strengthen security' }, | ||
| ]; | ||
|
|
||
| const withIcons = plain.map(option => ({ ...option, icon: <Icon name="settings" /> })); | ||
|
|
||
| const withTags = plain.map(option => ({ ...option, labelTag: '128 GB', tags: ['CPU-v2', '2 GB RAM'] })); | ||
|
|
||
| const withDisabled = plain.map(option => | ||
| option.value === '2' || option.value === '4' | ||
| ? { ...option, disabled: true, disabledReason: option.value === '4' ? 'Requires Enterprise support' : undefined } | ||
| : option | ||
| ); | ||
|
|
||
| export const grouped: OptionListProps.Options = [ | ||
| { value: 'group-1', label: 'Goals', options: plain.slice(0, 2) }, | ||
| { value: 'group-2', label: 'More goals', options: plain.slice(2) }, | ||
| ]; | ||
|
|
||
| export const allOptions = [plain, withIcons, withTags, withDisabled, grouped]; | ||
|
|
||
| export const renderOption: OptionListProps['renderOption'] = ({ item }) => <strong>{item.option.label}</strong>; | ||
|
|
||
| export type OptionListPermutation = OptionListProps & { width: number }; | ||
|
|
||
| export function OptionListPermutations({ | ||
| title, | ||
| permutations, | ||
| }: { | ||
| title: string; | ||
| permutations: ReadonlyArray<OptionListPermutation>; | ||
| }) { | ||
| return ( | ||
| <PermutationsPage title={title} screenshotArea={{ disableAnimations: true }}> | ||
| <PermutationsView | ||
| permutations={permutations} | ||
| render={({ width, ...props }) => ( | ||
| <div style={{ inlineSize: width }}> | ||
| <OptionList {...props} ariaLabel="Goals" onChange={() => {}} /> | ||
| </div> | ||
| )} | ||
| /> | ||
| </PermutationsPage> | ||
| ); | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import React from 'react'; | ||
|
|
||
| import createPermutations from '../utils/permutations'; | ||
| import { | ||
| allOptions, | ||
| grouped, | ||
| OptionListPermutation, | ||
| OptionListPermutations, | ||
| plain, | ||
| renderOption, | ||
| } from './permutations-common'; | ||
|
|
||
| const permutations = createPermutations<OptionListPermutation>([ | ||
| { | ||
| width: [240, 480], | ||
| options: allOptions, | ||
| selectionType: ['multi'], | ||
| selectedOptions: [[], [plain[1]], [plain[1], plain[2]]], | ||
| }, | ||
| { | ||
| width: [240], | ||
| options: [grouped], | ||
| selectionType: ['multi'], | ||
| selectedOptions: [[plain[1]]], | ||
| renderOption: [renderOption], | ||
| }, | ||
| ]); | ||
|
|
||
| export default function OptionListMultiPermutationsPage() { | ||
| return <OptionListPermutations title="OptionList permutations: multi selection" permutations={permutations} />; | ||
| } |
This file contains hidden or 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,33 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import React from 'react'; | ||
|
|
||
| import createPermutations from '../utils/permutations'; | ||
| import { | ||
| allOptions, | ||
| grouped, | ||
| OptionListPermutation, | ||
| OptionListPermutations, | ||
| plain, | ||
| renderOption, | ||
| } from './permutations-common'; | ||
|
|
||
| const permutations = createPermutations<OptionListPermutation>([ | ||
| { | ||
| width: [240, 480], | ||
| options: allOptions, | ||
| selectionType: ['single'], | ||
| selectedOptions: [[], [plain[1]]], | ||
| }, | ||
| { | ||
| width: [240], | ||
| options: [grouped], | ||
| selectionType: ['single'], | ||
| selectedOptions: [[plain[1]]], | ||
| renderOption: [renderOption], | ||
| }, | ||
| ]); | ||
|
|
||
| export default function OptionListSinglePermutationsPage() { | ||
| return <OptionListPermutations title="OptionList permutations: single selection" permutations={permutations} />; | ||
| } |
This file contains hidden or 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,216 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import React, { useContext, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import Button, { ButtonProps } from '~components/button'; | ||
| import Dropdown from '~components/dropdown'; | ||
| import FormField from '~components/form-field'; | ||
| import Header from '~components/header'; | ||
| import OptionList, { OptionListProps } from '~components/option-list'; | ||
| import SegmentedControl from '~components/segmented-control'; | ||
| import SpaceBetween from '~components/space-between'; | ||
|
|
||
| import AppContext, { AppContextType } from '../app/app-context'; | ||
| import { SimplePage } from '../app/templates'; | ||
|
|
||
| import styles from './scroll.scss'; | ||
|
|
||
| /** | ||
| * Where the scrolling happens: | ||
| * - `wrapper`: a wrapper div with a fixed height and `overflow: auto`. | ||
| * - `page`: no container, the page scrolls. | ||
| * - `dropdown`: the list is the content of a Dropdown opened by a button. | ||
| */ | ||
| type Container = 'wrapper' | 'page' | 'dropdown'; | ||
| type Preselected = 'none' | 'near-end'; | ||
|
|
||
| type PageContext = React.Context< | ||
| AppContextType<{ | ||
| container?: Container; | ||
| expandToViewport?: boolean; | ||
| selectionType?: OptionListProps.SelectionType; | ||
| preselected?: Preselected; | ||
| }> | ||
| >; | ||
|
|
||
| const options: OptionListProps.Option[] = Array.from({ length: 100 }, (_, index) => ({ | ||
| value: `option-${index + 1}`, | ||
| label: `Option ${index + 1}`, | ||
| })); | ||
|
|
||
| function DropdownOptionList({ | ||
| expandToViewport, | ||
| list, | ||
| listRef, | ||
| }: { | ||
| expandToViewport: boolean; | ||
| list: (props: { onChange: () => void }) => React.ReactNode; | ||
| listRef: React.RefObject<OptionListProps.Ref>; | ||
| }) { | ||
| const [open, setOpen] = useState(false); | ||
| const triggerRef = useRef<ButtonProps.Ref>(null); | ||
|
|
||
| // Move focus into the list once the Dropdown has rendered its content. | ||
| useEffect(() => { | ||
| if (open) { | ||
| listRef.current?.focus(); | ||
| } | ||
| }, [open, listRef]); | ||
|
|
||
| const close = (returnFocus: boolean) => { | ||
| setOpen(false); | ||
| if (returnFocus) { | ||
| triggerRef.current?.focus(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dropdown | ||
| open={open} | ||
| onOutsideClick={() => close(false)} | ||
| onEscape={() => close(true)} | ||
| minWidth={320} | ||
| expandToViewport={expandToViewport} | ||
| trigger={ | ||
| <Button | ||
| id="dropdown-trigger" | ||
| ref={triggerRef} | ||
| ariaExpanded={open} | ||
| ariaHaspopup="listbox" | ||
| iconName="caret-down-filled" | ||
| iconAlign="right" | ||
| onClick={() => setOpen(!open)} | ||
| > | ||
| Choose an option | ||
| </Button> | ||
| } | ||
| content={<div className={styles['dropdown-content']}>{list({ onChange: () => close(true) })}</div>} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function ConfiguredOptionList({ | ||
| container, | ||
| expandToViewport, | ||
| selectionType, | ||
| preselected, | ||
| }: { | ||
| container: Container; | ||
| expandToViewport: boolean; | ||
| selectionType: OptionListProps.SelectionType; | ||
| preselected: Preselected; | ||
| }) { | ||
| const [selectedOptions, setSelectedOptions] = useState<ReadonlyArray<OptionListProps.Option>>( | ||
| preselected === 'near-end' ? [options[80]] : [] | ||
| ); | ||
| const listRef = useRef<OptionListProps.Ref>(null); | ||
|
|
||
| const list = ({ onChange }: { onChange?: () => void } = {}) => ( | ||
| <OptionList | ||
| ref={listRef} | ||
| data-testid="option-list" | ||
| ariaLabelledby="option-list-heading" | ||
| selectionType={selectionType} | ||
| options={options} | ||
| selectedOptions={selectedOptions} | ||
| onChange={({ detail }) => { | ||
| setSelectedOptions(detail.selectedOptions); | ||
| onChange?.(); | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| return ( | ||
| <SpaceBetween size="xs"> | ||
| {/* Integ tests click the heading, then press Tab to move keyboard focus into the list. */} | ||
| <Header variant="h2" id="option-list-heading"> | ||
| Long list | ||
| </Header> | ||
| {container === 'wrapper' && ( | ||
| <div id="scroll-container" className={styles['fixed-height']}> | ||
| {list()} | ||
| </div> | ||
| )} | ||
| {container === 'page' && list()} | ||
| {container === 'dropdown' && ( | ||
| <DropdownOptionList expandToViewport={expandToViewport} list={list} listRef={listRef} /> | ||
| )} | ||
| <div data-testid="selected-value"> | ||
| Selected: {selectedOptions.map(option => option.value).join(', ') || 'none'} | ||
| </div> | ||
| </SpaceBetween> | ||
| ); | ||
| } | ||
|
|
||
| export default function OptionListScrollPage() { | ||
| const { urlParams, setUrlParams } = useContext(AppContext as PageContext); | ||
| const container = urlParams.container ?? 'wrapper'; | ||
| const expandToViewport = urlParams.expandToViewport ?? false; | ||
| const selectionType = urlParams.selectionType ?? 'single'; | ||
| const preselected = urlParams.preselected ?? 'none'; | ||
|
|
||
| return ( | ||
| <SimplePage | ||
| title="Option list: scrolling long lists" | ||
| settings={ | ||
| <SpaceBetween size="m"> | ||
| <FormField label="Scroll container"> | ||
| <SegmentedControl | ||
| selectedId={container} | ||
| onChange={({ detail }) => setUrlParams({ container: detail.selectedId as Container })} | ||
| options={[ | ||
| { id: 'wrapper', text: 'Wrapper' }, | ||
| { id: 'page', text: 'Page' }, | ||
| { id: 'dropdown', text: 'Dropdown' }, | ||
| ]} | ||
| /> | ||
| </FormField> | ||
| <FormField label="Expand to viewport (Dropdown only)"> | ||
| <SegmentedControl | ||
| selectedId={`${expandToViewport}`} | ||
| onChange={({ detail }) => setUrlParams({ expandToViewport: detail.selectedId === 'true' })} | ||
| options={[ | ||
| { id: 'false', text: 'No' }, | ||
| { id: 'true', text: 'Yes' }, | ||
| ]} | ||
| /> | ||
| </FormField> | ||
| <FormField label="Selection type"> | ||
| <SegmentedControl | ||
| selectedId={selectionType} | ||
| onChange={({ detail }) => | ||
| setUrlParams({ selectionType: detail.selectedId as OptionListProps.SelectionType }) | ||
| } | ||
| options={[ | ||
| { id: 'single', text: 'Single' }, | ||
| { id: 'multi', text: 'Multi' }, | ||
| ]} | ||
| /> | ||
| </FormField> | ||
| <FormField label="Already selected"> | ||
| <SegmentedControl | ||
| selectedId={preselected} | ||
| onChange={({ detail }) => setUrlParams({ preselected: detail.selectedId as Preselected })} | ||
| options={[ | ||
| { id: 'none', text: 'None' }, | ||
| { id: 'near-end', text: 'Option 81' }, | ||
| ]} | ||
| /> | ||
| </FormField> | ||
| </SpaceBetween> | ||
| } | ||
| screenshotArea={{}} | ||
| > | ||
| <div style={{ maxInlineSize: 520 }}> | ||
| {/* Remount on every configuration change so the initial selection follows the settings. */} | ||
| <ConfiguredOptionList | ||
| key={`${container}-${expandToViewport}-${selectionType}-${preselected}`} | ||
| container={container} | ||
| expandToViewport={expandToViewport} | ||
| selectionType={selectionType} | ||
| preselected={preselected} | ||
| /> | ||
| </div> | ||
| </SimplePage> | ||
| ); | ||
| } |
This file contains hidden or 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,15 @@ | ||
| /* | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| // A scroll container for the list. Applied to a wrapper div. | ||
| .fixed-height { | ||
| block-size: 240px; | ||
| overflow: auto; | ||
| } | ||
|
|
||
| // Lets the list scroll inside the Dropdown content instead of overflowing it. | ||
| .dropdown-content { | ||
| overflow: auto; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The permutations page is very long and can exceed our limit if a more cases are added. I recommend to split it in two pages, e.g. permutations-single and permutations-multi.