@react-aria/tag 3.0.0-nightly.2199 → 3.0.0-nightly.2206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/import.mjs +83 -119
- package/dist/main.js +82 -119
- package/dist/main.js.map +1 -1
- package/dist/module.js +83 -119
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +33 -37
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/index.ts +0 -2
- package/src/useTag.ts +36 -32
- package/src/useTagGroup.ts +34 -9
- package/src/TagKeyboardDelegate.ts +0 -79
package/src/useTag.ts
CHANGED
|
@@ -11,30 +11,34 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {AriaButtonProps} from '@react-types/button';
|
|
14
|
-
import {
|
|
14
|
+
import {DOMAttributes, FocusableElement, Node} from '@react-types/shared';
|
|
15
15
|
import {filterDOMProps, mergeProps, useDescription, useId} from '@react-aria/utils';
|
|
16
|
+
import {hookData} from './useTagGroup';
|
|
16
17
|
// @ts-ignore
|
|
17
18
|
import intlMessages from '../intl/*.json';
|
|
18
19
|
import {KeyboardEvent, RefObject} from 'react';
|
|
19
20
|
import type {ListState} from '@react-stately/list';
|
|
20
|
-
import {
|
|
21
|
+
import {SelectableItemStates} from '@react-aria/selection';
|
|
21
22
|
import {useGridListItem} from '@react-aria/gridlist';
|
|
22
23
|
import {useInteractionModality} from '@react-aria/interactions';
|
|
23
24
|
import {useLocalizedStringFormatter} from '@react-aria/i18n';
|
|
24
25
|
|
|
25
26
|
|
|
26
|
-
export interface TagAria {
|
|
27
|
-
/** Props for the tag visible label (if any). */
|
|
28
|
-
labelProps: DOMAttributes,
|
|
29
|
-
/** Props for the tag cell element. */
|
|
30
|
-
gridCellProps: DOMAttributes,
|
|
27
|
+
export interface TagAria extends Omit<SelectableItemStates, 'hasAction'> {
|
|
31
28
|
/** Props for the tag row element. */
|
|
32
29
|
rowProps: DOMAttributes,
|
|
30
|
+
/** Props for the tag cell element. */
|
|
31
|
+
gridCellProps: DOMAttributes,
|
|
33
32
|
/** Props for the tag remove button. */
|
|
34
|
-
removeButtonProps: AriaButtonProps
|
|
33
|
+
removeButtonProps: AriaButtonProps,
|
|
34
|
+
/** Whether the tag can be removed. */
|
|
35
|
+
allowsRemoving: boolean
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
export interface AriaTagProps<T>
|
|
38
|
+
export interface AriaTagProps<T> {
|
|
39
|
+
/** An object representing the tag. Contains all the relevant information that makes up the tag. */
|
|
40
|
+
item: Node<T>
|
|
41
|
+
}
|
|
38
42
|
|
|
39
43
|
/**
|
|
40
44
|
* Provides the behavior and accessibility implementation for a tag component.
|
|
@@ -43,58 +47,58 @@ export interface AriaTagProps<T> extends TagProps<T>, DOMProps, AriaLabelingProp
|
|
|
43
47
|
* @param ref - A ref to a DOM element for the tag.
|
|
44
48
|
*/
|
|
45
49
|
export function useTag<T>(props: AriaTagProps<T>, state: ListState<T>, ref: RefObject<FocusableElement>): TagAria {
|
|
46
|
-
let {
|
|
47
|
-
allowsRemoving,
|
|
48
|
-
item,
|
|
49
|
-
onRemove
|
|
50
|
-
} = props;
|
|
50
|
+
let {item} = props;
|
|
51
51
|
let stringFormatter = useLocalizedStringFormatter(intlMessages);
|
|
52
|
-
let labelId = useId();
|
|
53
52
|
let buttonId = useId();
|
|
54
53
|
|
|
55
|
-
let {
|
|
54
|
+
let {onRemove} = hookData.get(state) || {};
|
|
55
|
+
let {rowProps, gridCellProps, ...states} = useGridListItem({
|
|
56
56
|
node: item
|
|
57
57
|
}, state, ref);
|
|
58
58
|
|
|
59
59
|
// We want the group to handle keyboard navigation between tags.
|
|
60
60
|
delete rowProps.onKeyDownCapture;
|
|
61
|
+
delete states.descriptionProps;
|
|
61
62
|
|
|
62
63
|
let onKeyDown = (e: KeyboardEvent) => {
|
|
63
64
|
if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
64
|
-
onRemove(item.key);
|
|
65
65
|
e.preventDefault();
|
|
66
|
+
if (state.selectionManager.isSelected(item.key)) {
|
|
67
|
+
onRemove(new Set(state.selectionManager.selectedKeys));
|
|
68
|
+
} else {
|
|
69
|
+
onRemove(new Set([item.key]));
|
|
70
|
+
}
|
|
66
71
|
}
|
|
67
72
|
};
|
|
68
|
-
|
|
73
|
+
|
|
69
74
|
let modality: string = useInteractionModality();
|
|
70
75
|
if (modality === 'virtual' && (typeof window !== 'undefined' && 'ontouchstart' in window)) {
|
|
71
76
|
modality = 'touch';
|
|
72
77
|
}
|
|
73
|
-
let description =
|
|
78
|
+
let description = onRemove && (modality === 'keyboard' || modality === 'virtual') ? stringFormatter.format('removeDescription') : '';
|
|
74
79
|
let descProps = useDescription(description);
|
|
75
80
|
|
|
76
|
-
let domProps = filterDOMProps(props);
|
|
77
81
|
let isFocused = item.key === state.selectionManager.focusedKey;
|
|
82
|
+
// @ts-ignore - data attributes are ok but TS doesn't know about them.
|
|
83
|
+
let domProps = filterDOMProps(props);
|
|
78
84
|
return {
|
|
79
85
|
removeButtonProps: {
|
|
80
|
-
'aria-label': stringFormatter.format('removeButtonLabel'
|
|
81
|
-
'aria-labelledby': `${buttonId} ${
|
|
86
|
+
'aria-label': stringFormatter.format('removeButtonLabel'),
|
|
87
|
+
'aria-labelledby': `${buttonId} ${rowProps.id}`,
|
|
82
88
|
id: buttonId,
|
|
83
|
-
onPress: () =>
|
|
89
|
+
onPress: () => onRemove ? onRemove(new Set([item.key])) : null,
|
|
84
90
|
excludeFromTabOrder: true
|
|
85
91
|
},
|
|
86
|
-
|
|
87
|
-
id: labelId
|
|
88
|
-
},
|
|
89
|
-
rowProps: {
|
|
90
|
-
...rowProps,
|
|
92
|
+
rowProps: mergeProps(rowProps, domProps, {
|
|
91
93
|
tabIndex: (isFocused || state.selectionManager.focusedKey == null) ? 0 : -1,
|
|
92
|
-
onKeyDown:
|
|
94
|
+
onKeyDown: onRemove ? onKeyDown : undefined,
|
|
93
95
|
'aria-describedby': descProps['aria-describedby']
|
|
94
|
-
},
|
|
95
|
-
gridCellProps: mergeProps(
|
|
96
|
+
}),
|
|
97
|
+
gridCellProps: mergeProps(gridCellProps, {
|
|
96
98
|
'aria-errormessage': props['aria-errormessage'],
|
|
97
99
|
'aria-label': props['aria-label']
|
|
98
|
-
})
|
|
100
|
+
}),
|
|
101
|
+
...states,
|
|
102
|
+
allowsRemoving: !!onRemove
|
|
99
103
|
};
|
|
100
104
|
}
|
package/src/useTagGroup.ts
CHANGED
|
@@ -10,12 +10,11 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {AriaLabelingProps, DOMAttributes, DOMProps,
|
|
13
|
+
import {AriaLabelingProps, CollectionBase, DOMAttributes, DOMProps, HelpTextProps, KeyboardDelegate, LabelableProps, MultipleSelection, SelectionBehavior} from '@react-types/shared';
|
|
14
14
|
import {filterDOMProps, mergeProps} from '@react-aria/utils';
|
|
15
|
+
import {Key, RefObject, useEffect, useRef, useState} from 'react';
|
|
16
|
+
import {ListKeyboardDelegate} from '@react-aria/selection';
|
|
15
17
|
import type {ListState} from '@react-stately/list';
|
|
16
|
-
import {RefObject, useEffect, useRef, useState} from 'react';
|
|
17
|
-
import {TagGroupProps} from '@react-types/tag';
|
|
18
|
-
import {TagKeyboardDelegate} from './TagKeyboardDelegate';
|
|
19
18
|
import {useField} from '@react-aria/label';
|
|
20
19
|
import {useFocusWithin} from '@react-aria/interactions';
|
|
21
20
|
import {useGridList} from '@react-aria/gridlist';
|
|
@@ -32,14 +31,27 @@ export interface TagGroupAria {
|
|
|
32
31
|
errorMessageProps: DOMAttributes
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
export interface AriaTagGroupProps<T> extends
|
|
34
|
+
export interface AriaTagGroupProps<T> extends CollectionBase<T>, MultipleSelection, DOMProps, LabelableProps, AriaLabelingProps, HelpTextProps {
|
|
35
|
+
/** How multiple selection should behave in the collection. */
|
|
36
|
+
selectionBehavior?: SelectionBehavior,
|
|
37
|
+
/** Handler that is called when a user deletes a tag. */
|
|
38
|
+
onRemove?: (key: Set<Key>) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface AriaTagGroupOptions<T> extends Omit<AriaTagGroupProps<T>, 'children'> {
|
|
36
42
|
/**
|
|
37
43
|
* An optional keyboard delegate to handle arrow key navigation,
|
|
38
44
|
* to override the default.
|
|
39
45
|
*/
|
|
40
|
-
keyboardDelegate?:
|
|
46
|
+
keyboardDelegate?: KeyboardDelegate
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface HookData {
|
|
50
|
+
onRemove?: (keys: Set<Key>) => void
|
|
41
51
|
}
|
|
42
52
|
|
|
53
|
+
export const hookData = new WeakMap<ListState<any>, HookData>();
|
|
54
|
+
|
|
43
55
|
/**
|
|
44
56
|
* Provides the behavior and accessibility implementation for a tag group component.
|
|
45
57
|
* A tag group is a focusable list of labels, categories, keywords, or other items, with support for keyboard navigation and removal.
|
|
@@ -47,11 +59,22 @@ export interface AriaTagGroupProps<T> extends TagGroupProps<T>, DOMProps, AriaLa
|
|
|
47
59
|
* @param state - State for the tag group, as returned by `useListState`.
|
|
48
60
|
* @param ref - A ref to a DOM element for the tag group.
|
|
49
61
|
*/
|
|
50
|
-
export function useTagGroup<T>(props:
|
|
62
|
+
export function useTagGroup<T>(props: AriaTagGroupOptions<T>, state: ListState<T>, ref: RefObject<HTMLElement>): TagGroupAria {
|
|
51
63
|
let {direction} = useLocale();
|
|
52
|
-
let keyboardDelegate = props.keyboardDelegate || new
|
|
64
|
+
let keyboardDelegate = props.keyboardDelegate || new ListKeyboardDelegate({
|
|
65
|
+
collection: state.collection,
|
|
66
|
+
ref,
|
|
67
|
+
orientation: 'horizontal',
|
|
68
|
+
direction,
|
|
69
|
+
disabledKeys: state.disabledKeys
|
|
70
|
+
});
|
|
53
71
|
let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField(props);
|
|
54
|
-
let {gridProps} = useGridList({
|
|
72
|
+
let {gridProps} = useGridList({
|
|
73
|
+
...props,
|
|
74
|
+
...fieldProps,
|
|
75
|
+
keyboardDelegate,
|
|
76
|
+
shouldFocusWrap: true
|
|
77
|
+
}, state, ref);
|
|
55
78
|
|
|
56
79
|
let [isFocusWithin, setFocusWithin] = useState(false);
|
|
57
80
|
let {focusWithinProps} = useFocusWithin({
|
|
@@ -68,6 +91,8 @@ export function useTagGroup<T>(props: AriaTagGroupProps<T>, state: ListState<T>,
|
|
|
68
91
|
prevCount.current = state.collection.size;
|
|
69
92
|
}, [state.collection.size, isFocusWithin, ref]);
|
|
70
93
|
|
|
94
|
+
hookData.set(state, {onRemove: props.onRemove});
|
|
95
|
+
|
|
71
96
|
return {
|
|
72
97
|
gridProps: mergeProps(gridProps, domProps, {
|
|
73
98
|
role: state.collection.size ? 'grid' : null,
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {Collection, Direction, KeyboardDelegate} from '@react-types/shared';
|
|
14
|
-
import {Key} from 'react';
|
|
15
|
-
|
|
16
|
-
export class TagKeyboardDelegate<T> implements KeyboardDelegate {
|
|
17
|
-
private collection: Collection<T>;
|
|
18
|
-
private direction: Direction;
|
|
19
|
-
|
|
20
|
-
constructor(collection: Collection<T>, direction: Direction) {
|
|
21
|
-
this.collection = collection;
|
|
22
|
-
this.direction = direction;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
getFirstKey() {
|
|
26
|
-
return this.collection.getFirstKey();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
getLastKey() {
|
|
30
|
-
return this.collection.getLastKey();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getKeyRightOf(key: Key) {
|
|
34
|
-
return this.direction === 'rtl' ? this.getKeyAbove(key) : this.getKeyBelow(key);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
getKeyLeftOf(key: Key) {
|
|
38
|
-
return this.direction === 'rtl' ? this.getKeyBelow(key) : this.getKeyAbove(key);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
getKeyBelow(key) {
|
|
42
|
-
let startItem = this.collection.getItem(key);
|
|
43
|
-
if (!startItem) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Find the next item
|
|
48
|
-
key = this.collection.getKeyAfter(key);
|
|
49
|
-
|
|
50
|
-
if (key != null) {
|
|
51
|
-
return key;
|
|
52
|
-
} else {
|
|
53
|
-
return this.collection.getFirstKey();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
getKeyAbove(key) {
|
|
58
|
-
let startItem = this.collection.getItem(key);
|
|
59
|
-
if (!startItem) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Find the previous item
|
|
64
|
-
key = this.collection.getKeyBefore(key);
|
|
65
|
-
if (key != null) {
|
|
66
|
-
return key;
|
|
67
|
-
} else {
|
|
68
|
-
return this.collection.getLastKey();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
getKeyPageAbove(key) {
|
|
73
|
-
return this.getKeyAbove(key);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
getKeyPageBelow(key) {
|
|
77
|
-
return this.getKeyBelow(key);
|
|
78
|
-
}
|
|
79
|
-
}
|