Skip to content

Commit 1503f63

Browse files
authored
Fix disabled item behavior in collections (#3746)
1 parent bfe89ed commit 1503f63

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

packages/@react-aria/selection/src/useSelectableItem.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
147147
// eslint-disable-next-line react-hooks/exhaustive-deps
148148
}, [ref, key, manager.focusedKey, manager.childFocusStrategy, manager.isFocused, shouldUseVirtualFocus]);
149149

150+
isDisabled = isDisabled || manager.isDisabled(key);
150151
// Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
151152
// item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
152153
// on iOS 14 doesn't try to move real DOM focus to the item anyway.
153154
let itemProps: SelectableItemAria['itemProps'] = {};
154-
if (!shouldUseVirtualFocus) {
155+
if (!shouldUseVirtualFocus && !isDisabled) {
155156
itemProps = {
156157
tabIndex: key === manager.focusedKey ? 0 : -1,
157158
onFocus(e) {
@@ -160,14 +161,17 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
160161
}
161162
}
162163
};
164+
} else if (isDisabled) {
165+
itemProps.onMouseDown = (e) => {
166+
// Prevent focus going to the body when clicking on a disabled item.
167+
e.preventDefault();
168+
};
163169
}
164170

165-
166171
// With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
167172
// Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
168173
// With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
169174
// With touch, onAction occurs on single tap, and long press enters selection mode.
170-
isDisabled = isDisabled || manager.isDisabled(key);
171175
let allowsSelection = !isDisabled && manager.canSelectItem(key);
172176
let allowsActions = onAction && !isDisabled;
173177
let hasPrimaryAction = allowsActions && (

packages/@react-spectrum/picker/stories/Picker.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ storiesOf('Picker', module)
6666
</Picker>
6767
)
6868
)
69+
.add(
70+
'disabled keys',
71+
() => (
72+
<Picker label="Test" onSelectionChange={action('selectionChange')} disabledKeys={['Short']}>
73+
<Item key="Short">Short</Item>
74+
<Item key="Normal">Normal</Item>
75+
<Item key="This item is very long and word wraps poorly">This item is very long and word wraps poorly</Item>
76+
</Picker>
77+
)
78+
)
6979
.add(
7080
'sections',
7181
() => (

packages/@react-stately/table/src/useTableState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function useTableState<T extends object>(props: TableStateProps<T>): Tabl
6969
(nodes, prev) => new TableCollection(nodes, prev, context),
7070
context
7171
);
72-
let {disabledKeys, selectionManager} = useGridState({...props, collection});
72+
let {disabledKeys, selectionManager} = useGridState({...props, collection, disabledBehavior: 'selection'});
7373

7474
return {
7575
collection,

0 commit comments

Comments
 (0)