Skip to content

Commit 6013307

Browse files
authored
move useTag ref from props to param (#4051)
1 parent be0fae9 commit 6013307

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import {AriaButtonProps} from '@react-types/button';
1414
import {chain, filterDOMProps, mergeProps, useId} from '@react-aria/utils';
15-
import {DOMAttributes} from '@react-types/shared';
15+
import {DOMAttributes, FocusableElement} from '@react-types/shared';
1616
// @ts-ignore
1717
import intlMessages from '../intl/*.json';
18-
import {KeyboardEvent} from 'react';
18+
import {KeyboardEvent, RefObject} from 'react';
1919
import type {TagGroupState} from '@react-stately/tag';
2020
import {TagProps} from '@react-types/tag';
2121
import {useGridListItem} from '@react-aria/gridlist';
@@ -34,12 +34,11 @@ export interface TagAria {
3434
* @param props - Props to be applied to the tag.
3535
* @param state - State for the tag group, as returned by `useTagGroupState`.
3636
*/
37-
export function useTag<T>(props: TagProps<T>, state: TagGroupState<T>): TagAria {
37+
export function useTag<T>(props: TagProps<T>, state: TagGroupState<T>, ref: RefObject<FocusableElement>): TagAria {
3838
let {
3939
isFocused,
4040
allowsRemoving,
41-
item,
42-
tagRowRef
41+
item
4342
} = props;
4443
let stringFormatter = useLocalizedStringFormatter(intlMessages);
4544
let removeString = stringFormatter.format('remove');
@@ -48,7 +47,7 @@ export function useTag<T>(props: TagProps<T>, state: TagGroupState<T>): TagAria
4847

4948
let {rowProps, gridCellProps} = useGridListItem({
5049
node: item
51-
}, state, tagRowRef);
50+
}, state, ref);
5251

5352
// We want the group to handle keyboard navigation between tags.
5453
delete rowProps.onKeyDownCapture;

packages/@react-spectrum/tag/src/Tag.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ export function Tag<T>(props: SpectrumTagProps<T>) {
4040
let {styleProps} = useStyleProps(otherProps);
4141
let {hoverProps, isHovered} = useHover({});
4242
let {isFocused, isFocusVisible, focusProps} = useFocusRing({within: true});
43-
let tagRowRef = useRef();
43+
let ref = useRef();
4444
let {clearButtonProps, labelProps, tagProps, tagRowProps} = useTag({
4545
...props,
4646
isFocused,
4747
allowsRemoving,
4848
item,
49-
onRemove,
50-
tagRowRef
51-
}, state);
49+
onRemove
50+
}, state, ref);
5251

5352
return (
5453
<div
@@ -64,7 +63,7 @@ export function Tag<T>(props: SpectrumTagProps<T>) {
6463
},
6564
styleProps.className
6665
)}
67-
ref={tagRowRef}>
66+
ref={ref}>
6867
<div
6968
className={classNames(styles, 'spectrum-Tag-cell')}
7069
{...tagProps}>

packages/@react-types/tag/src/index.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import {CollectionBase, ItemProps, Node} from '@react-types/shared';
14-
import {Key, RefObject} from 'react';
14+
import {Key} from 'react';
1515

1616
export interface TagGroupProps<T> extends Omit<CollectionBase<T>, 'disabledKeys'> {
1717
/** Whether the TagGroup allows removal of tags. */
@@ -26,6 +26,5 @@ export interface TagProps<T> extends ItemProps<any> {
2626
isFocused: boolean,
2727
allowsRemoving?: boolean,
2828
item: Node<T>,
29-
onRemove?: (key: Key) => void,
30-
tagRowRef: RefObject<HTMLElement>
29+
onRemove?: (key: Key) => void
3130
}

0 commit comments

Comments
 (0)