Skip to content

Commit fe5205a

Browse files
authored
feat: add escape key behavior to tags (#8399)
* feat: add escape key behavior to tags * fix lint
1 parent da50cd0 commit fe5205a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/@react-aria/tag/src/useTagGroup.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {AriaGridListProps, useGridList} from '@react-aria/gridlist';
1314
import {AriaLabelingProps, CollectionBase, DOMAttributes, DOMProps, HelpTextProps, Key, KeyboardDelegate, LabelableProps, MultipleSelection, RefObject, SelectionBehavior} from '@react-types/shared';
1415
import {filterDOMProps, mergeProps} from '@react-aria/utils';
1516
import {ListKeyboardDelegate} from '@react-aria/selection';
1617
import type {ListState} from '@react-stately/list';
1718
import {ReactNode, useEffect, useRef, useState} from 'react';
1819
import {useField} from '@react-aria/label';
1920
import {useFocusWithin} from '@react-aria/interactions';
20-
import {useGridList} from '@react-aria/gridlist';
2121
import {useLocale} from '@react-aria/i18n';
2222

2323
export interface TagGroupAria {
@@ -31,15 +31,24 @@ export interface TagGroupAria {
3131
errorMessageProps: DOMAttributes
3232
}
3333

34-
export interface AriaTagGroupProps<T> extends CollectionBase<T>, MultipleSelection, DOMProps, LabelableProps, AriaLabelingProps, Omit<HelpTextProps, 'errorMessage'> {
34+
export interface AriaTagGroupProps<T> extends CollectionBase<T>, MultipleSelection, Pick<AriaGridListProps<T>, 'escapeKeyBehavior'>, DOMProps, LabelableProps, AriaLabelingProps, Omit<HelpTextProps, 'errorMessage'> {
3535
/** How multiple selection should behave in the collection. */
3636
selectionBehavior?: SelectionBehavior,
3737
/** Whether selection should occur on press up instead of press down. */
3838
shouldSelectOnPressUp?: boolean,
3939
/** Handler that is called when a user deletes a tag. */
4040
onRemove?: (keys: Set<Key>) => void,
4141
/** An error message for the field. */
42-
errorMessage?: ReactNode
42+
errorMessage?: ReactNode,
43+
/**
44+
* Whether pressing the escape key should clear selection in the TagGroup or not.
45+
*
46+
* Most experiences should not modify this option as it eliminates a keyboard user's ability to
47+
* easily clear selection. Only use if the escape key is being handled externally or should not
48+
* trigger selection clearing contextually.
49+
* @default 'clearSelection'
50+
*/
51+
escapeKeyBehavior?: 'clearSelection' | 'none'
4352
}
4453

4554
export interface AriaTagGroupOptions<T> extends Omit<AriaTagGroupProps<T>, 'children'> {

packages/dev/docs/pages/react-aria/home/ExampleApp.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,16 @@ export function ExampleApp(): React.ReactNode {
188188
{filters > 0 && <Button onPress={clearFilters} variant="secondary" className="absolute top-4 right-4 py-1 px-2 text-xs">Clear</Button>}
189189
<div className="flex flex-col gap-4">
190190
<Checkbox isSelected={favorite} onChange={setFavorite}>Favorite</Checkbox>
191-
<TagGroup label="Cycle" selectionMode="multiple" selectedKeys={cycles} onSelectionChange={setCycles}>
191+
<TagGroup label="Cycle" selectionMode="multiple" selectedKeys={cycles} onSelectionChange={setCycles} escapeKeyBehavior="none">
192192
<Tag id="Annual" color="green" textValue="Annual"><RefreshCw className="w-4 h-4 shrink-0" /> Annual</Tag>
193193
<Tag id="Perennial" color="green" textValue="Perennial"><RefreshCw className="w-4 h-4 shrink-0" /> Perennial</Tag>
194194
</TagGroup>
195-
<TagGroup label="Sunlight" selectionMode="multiple" selectedKeys={sunlight} onSelectionChange={setSunlight}>
195+
<TagGroup label="Sunlight" selectionMode="multiple" selectedKeys={sunlight} onSelectionChange={setSunlight} escapeKeyBehavior="none">
196196
<Tag id="full sun" color="yellow" textValue="Full Sun">{sunIcons['full sun']} Full Sun</Tag>
197197
<Tag id="part sun" color="yellow" textValue="Part Sun">{sunIcons['part sun']} Part Sun</Tag>
198198
<Tag id="part shade" color="yellow" textValue="Part Shade">{sunIcons['part shade']} Part Shade</Tag>
199199
</TagGroup>
200-
<TagGroup label="Watering" selectionMode="multiple" selectedKeys={watering} onSelectionChange={setWatering}>
200+
<TagGroup label="Watering" selectionMode="multiple" selectedKeys={watering} onSelectionChange={setWatering} escapeKeyBehavior="none">
201201
<Tag id="Frequent" color="blue" textValue="Frequent">{wateringIcons['Frequent']} Frequent</Tag>
202202
<Tag id="Average" color="blue" textValue="Average">{wateringIcons['Average']} Average</Tag>
203203
<Tag id="Minimum" color="blue" textValue="Minimum">{wateringIcons['Minimum']} Minimum</Tag>

0 commit comments

Comments
 (0)