@react-aria/tag 3.0.0-nightly.3175 → 3.0.0-rc.0

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.
@@ -10,41 +10,68 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {DOMProps} from '@react-types/shared';
13
+ import {AriaLabelingProps, DOMAttributes, DOMProps, Validation} from '@react-types/shared';
14
14
  import {filterDOMProps, mergeProps} from '@react-aria/utils';
15
- import {HTMLAttributes, Key, ReactNode, useState} from 'react';
15
+ import {RefObject, useState} from 'react';
16
+ import {TagGroupProps} from '@react-types/tag';
17
+ import type {TagGroupState} from '@react-stately/tag';
18
+ import {TagKeyboardDelegate} from './TagKeyboardDelegate';
19
+ import {useField} from '@react-aria/label';
16
20
  import {useFocusWithin} from '@react-aria/interactions';
21
+ import {useGridList} from '@react-aria/gridlist';
22
+ import {useLocale} from '@react-aria/i18n';
17
23
 
18
- interface AriaTagGroupProps extends DOMProps {
19
- children: ReactNode,
20
- disabledKeys?: Iterable<Key>,
21
- isDisabled?: boolean,
22
- isReadOnly?: boolean, // removes close button
23
- validationState?: 'valid' | 'invalid'
24
+ export interface TagGroupAria {
25
+ /** Props for the tag grouping element. */
26
+ gridProps: DOMAttributes,
27
+ /** Props for the tag group's visible label (if any). */
28
+ labelProps: DOMAttributes,
29
+ /** Props for the tag group description element, if any. */
30
+ descriptionProps: DOMAttributes,
31
+ /** Props for the tag group error message element, if any. */
32
+ errorMessageProps: DOMAttributes
24
33
  }
25
34
 
26
- interface TagGroupAria {
27
- tagGroupProps: HTMLAttributes<HTMLElement>
35
+ export interface AriaTagGroupProps<T> extends TagGroupProps<T>, DOMProps, AriaLabelingProps, Validation {
36
+ /**
37
+ * An optional keyboard delegate to handle arrow key navigation,
38
+ * to override the default.
39
+ */
40
+ keyboardDelegate?: TagKeyboardDelegate<T>
28
41
  }
29
42
 
30
- export function useTagGroup(props: AriaTagGroupProps, listState): TagGroupAria {
31
- let {isDisabled} = props;
43
+ /**
44
+ * Provides the behavior and accessibility implementation for a tag group component.
45
+ * A tag group is a focusable list of labels, categories, keywords, or other items, with support for keyboard navigation and removal.
46
+ * @param props - Props to be applied to the tag group.
47
+ * @param state - State for the tag group, as returned by `useTagGroupState`.
48
+ * @param ref - A ref to a DOM element for the tag group.
49
+ */
50
+ export function useTagGroup<T>(props: AriaTagGroupProps<T>, state: TagGroupState<T>, ref: RefObject<HTMLElement>): TagGroupAria {
51
+ let {direction} = useLocale();
52
+ let keyboardDelegate = props.keyboardDelegate || new TagKeyboardDelegate(state.collection, direction);
53
+ let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField(props);
54
+ let {gridProps} = useGridList({...props, ...fieldProps, keyboardDelegate}, state, ref);
55
+
56
+ // Don't want the grid to be focusable or accessible via keyboard
57
+ delete gridProps.tabIndex;
58
+
32
59
  let [isFocusWithin, setFocusWithin] = useState(false);
33
60
  let {focusWithinProps} = useFocusWithin({
34
61
  onFocusWithinChange: setFocusWithin
35
62
  });
36
- let allKeys = [...listState.collection.getKeys()];
37
- if (!allKeys.some(key => !listState.disabledKeys.has(key))) {
38
- isDisabled = true;
39
- }
40
63
  let domProps = filterDOMProps(props);
41
64
  return {
42
- tagGroupProps: mergeProps(domProps, {
65
+ gridProps: mergeProps(gridProps, domProps, {
66
+ role: state.collection.size ? 'grid' : null,
43
67
  'aria-atomic': false,
44
68
  'aria-relevant': 'additions',
45
69
  'aria-live': isFocusWithin ? 'polite' : 'off',
46
- 'aria-disabled': isDisabled === true,
47
- ...focusWithinProps
48
- } as HTMLAttributes<HTMLElement>)
70
+ ...focusWithinProps,
71
+ ...fieldProps
72
+ }),
73
+ labelProps,
74
+ descriptionProps,
75
+ errorMessageProps
49
76
  };
50
77
  }