Skip to content

Commit ee3e90b

Browse files
useTab: adds support for shouldSelectOnPressUp (#4342)
1 parent f6e686f commit ee3e90b

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

packages/@react-aria/tabs/src/useTab.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function useTab<T>(
3737
state: TabListState<T>,
3838
ref: RefObject<FocusableElement>
3939
): TabAria {
40-
let {key, isDisabled: propsDisabled} = props;
40+
let {key, isDisabled: propsDisabled, shouldSelectOnPressUp} = props;
4141
let {selectionManager: manager, selectedKey} = state;
4242

4343
let isSelected = key === selectedKey;
@@ -47,7 +47,8 @@ export function useTab<T>(
4747
selectionManager: manager,
4848
key,
4949
ref,
50-
isDisabled
50+
isDisabled,
51+
shouldSelectOnPressUp
5152
});
5253

5354
let tabId = generateId(state, key, 'tab');
@@ -69,4 +70,3 @@ export function useTab<T>(
6970
isPressed
7071
};
7172
}
72-

packages/@react-aria/tabs/stories/example.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,40 @@ import React from 'react';
1414
import {useTab, useTabList, useTabPanel} from '@react-aria/tabs';
1515
import {useTabListState} from '@react-stately/tabs';
1616

17-
export function Tabs(props) {
17+
export function Tabs({shouldSelectOnPressUp, ...props}) {
1818
let state = useTabListState(props);
1919
let ref = React.useRef();
2020
let {tabListProps} = useTabList(props, state, ref);
2121
return (
2222
<div style={{height: '150px'}}>
23-
<div {...tabListProps} ref={ref} style={{display: 'flex', borderBottom: '1px solid grey', borderLeft: '10px solid grey', borderRight: '20px solid grey', maxWidth: '400px', overflow: 'auto'}}>
23+
<div
24+
{...tabListProps}
25+
ref={ref}
26+
style={{
27+
display: 'flex',
28+
borderBottom: '1px solid grey',
29+
borderLeft: '10px solid grey',
30+
borderRight: '20px solid grey',
31+
maxWidth: '400px',
32+
overflow: 'auto'
33+
}}>
2434
{[...state.collection].map((item) => (
25-
<Tab key={item.key} item={item} state={state} />
35+
<Tab
36+
key={item.key}
37+
item={item}
38+
state={state}
39+
shouldSelectOnPressUp={shouldSelectOnPressUp} />
2640
))}
2741
</div>
2842
<TabPanel key={state.selectedItem?.key} state={state} />
2943
</div>
3044
);
3145
}
3246

33-
function Tab({item, state}) {
47+
function Tab({shouldSelectOnPressUp, item, state}) {
3448
let {key, rendered} = item;
3549
let ref = React.useRef();
36-
let {tabProps} = useTab({key}, state, ref);
50+
let {tabProps} = useTab({key, shouldSelectOnPressUp}, state, ref);
3751
let isSelected = state.selectedKey === key;
3852
let isDisabled = state.disabledKeys.has(key);
3953
return (

packages/@react-aria/tabs/stories/useTabList.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ for (let i = 0; i < 50; i++) {
2525
lotsOfItems.push({name: 'Item ' + i, contents: 'Contents ' + i});
2626
}
2727

28-
const Template = () => () => (
29-
<Tabs aria-label="example" items={lotsOfItems}>
28+
const Template = () => (props) => (
29+
<Tabs aria-label="example" items={lotsOfItems} {...props}>
3030
{(item) => (
3131
<Item key={item.name} title={item.name}>
3232
{item.contents}
@@ -37,3 +37,6 @@ const Template = () => () => (
3737

3838
export const ScrollTesting = Template().bind({});
3939
ScrollTesting.args = {};
40+
41+
export const OnPressEndSelection = Template().bind({});
42+
OnPressEndSelection.args = {shouldSelectOnPressUp: true};

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export interface AriaTabProps extends AriaLabelingProps {
2525
/** The key of the tab. */
2626
key: Key,
2727
/** Whether the tab should be disabled. */
28-
isDisabled?: boolean
28+
isDisabled?: boolean,
29+
/** Whether the tab selection should occur on press up instead of press down. */
30+
shouldSelectOnPressUp?: boolean
2931
}
3032

3133
export interface TabListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'disallowEmptySelection'> {

0 commit comments

Comments
 (0)