Skip to content

Commit 5c37911

Browse files
committed
feat: add setDomFocus argument to focusItem methods
1 parent 3cab9ae commit 5c37911

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

next-release-notes.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<!--
2-
### Breaking Changes
31
### Features
4-
### Bug Fixes and Improvements
5-
### Other Changes
6-
-->
2+
3+
- Add `setDomFocus` argument to focus-item methods to provide an escape hatch to set the focus state of an item in RCT
4+
without updating the DOM focus. This defaults to true in all existing methods to maintain the current behavior if
5+
it is absent.

packages/core/src/controlledEnvironment/useControlledTreeEnvironmentProps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export const useControlledTreeEnvironmentProps = ({
4545
const onFocusItemHandler = useCallback<
4646
Required<TreeChangeHandlers>['onFocusItem']
4747
>(
48-
(item, treeId) => {
49-
if (autoFocus ?? true) {
48+
(item, treeId, setDomFocus = true) => {
49+
if ((autoFocus ?? true) && setDomFocus) {
5050
const newItem = getDocument()?.querySelector(
5151
`[data-rct-tree="${treeId}"] [data-rct-item-id="${item.index}"]`
5252
);

packages/core/src/environmentActions/EnvironmentActionsProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ export const EnvironmentActionsProvider = React.forwardRef<
7676
);
7777

7878
const focusItem = useCallback(
79-
(itemId: TreeItemIndex, treeId: string) => {
80-
onFocusItem?.(items[itemId], treeId);
79+
(itemId: TreeItemIndex, treeId: string, setDomFocus = true) => {
80+
onFocusItem?.(items[itemId], treeId, setDomFocus);
8181
},
8282
[items, onFocusItem]
8383
);

packages/core/src/treeActions/TreeActionsProvider.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { PropsWithChildren, useMemo } from 'react';
33
import {
4+
TreeChangeActions,
45
TreeChangeActionsContextProps,
56
TreeItemIndex,
67
TreeRef,
@@ -26,7 +27,7 @@ export const TreeActionsProvider = React.forwardRef<
2627

2728
// TODO change tree childs to use actions rather than output events where possible
2829
// TODO maybe replace with stable handlers
29-
const actions = useMemo(
30+
const actions = useMemo<TreeChangeActions>(
3031
() => ({
3132
abortRenamingItem(): void {
3233
tree.setRenamingItem(null);
@@ -43,8 +44,8 @@ export const TreeActionsProvider = React.forwardRef<
4344
expandItem(itemId: TreeItemIndex): void {
4445
envActions.expandItem(itemId, tree.treeId);
4546
},
46-
focusItem(itemId: TreeItemIndex): void {
47-
envActions.focusItem(itemId, tree.treeId);
47+
focusItem(itemId: TreeItemIndex, setDomFocus = true): void {
48+
envActions.focusItem(itemId, tree.treeId, setDomFocus);
4849
},
4950
focusTree(autoFocus = true): void {
5051
envActions.focusTree(tree.treeId, autoFocus);

packages/core/src/treeItem/useTreeItemRenderContext.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ export const useTreeItemRenderContext = (item?: TreeItem) => {
119119
stopRenamingItem: () => {
120120
setRenamingItem(null);
121121
},
122-
focusItem: () => {
123-
environment.onFocusItem?.(item, treeId);
122+
focusItem: (setDomFocus = true) => {
123+
environment.onFocusItem?.(item, treeId, setDomFocus);
124124
},
125125
startDragging: () => {
126126
let selectedItems = viewState?.selectedItems ?? [];

packages/core/src/types.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export interface TreeItemActions {
3535
addToSelectedItems: () => void;
3636
selectUpTo: (overrideOldSelection?: boolean) => void;
3737
startDragging: () => void;
38-
focusItem: () => void;
38+
39+
/** @param setDomFocus - Defaults to true. */
40+
focusItem: (setDomFocus?: boolean) => void;
3941
// toggleSelectedState: () => void;
4042
}
4143

@@ -216,7 +218,11 @@ export interface TreeChangeHandlers<T = any> {
216218
onCollapseItem?: (item: TreeItem<T>, treeId: string) => void;
217219
onExpandItem?: (item: TreeItem<T>, treeId: string) => void;
218220
onSelectItems?: (items: TreeItemIndex[], treeId: string) => void; // TODO TreeItem instead of just index
219-
onFocusItem?: (item: TreeItem<T>, treeId: string) => void;
221+
onFocusItem?: (
222+
item: TreeItem<T>,
223+
treeId: string,
224+
setDomFocus?: boolean
225+
) => void;
220226
onDrop?: (items: TreeItem<T>[], target: DraggingPosition) => void;
221227
onPrimaryAction?: (items: TreeItem<T>, treeId: string) => void;
222228
onRegisterTree?: (tree: TreeConfiguration) => void;
@@ -234,7 +240,14 @@ export interface TreeEnvironmentChangeActions {
234240
selectItems: (itemsIds: TreeItemIndex[], treeId: string) => void;
235241
toggleItemSelectStatus: (itemId: TreeItemIndex, treeId: string) => void;
236242
invokePrimaryAction: (itemId: TreeItemIndex, treeID: string) => void;
237-
focusItem: (itemId: TreeItemIndex, treeId: string) => void;
243+
244+
/** @param setDomFocus - Defaults to true. */
245+
focusItem: (
246+
itemId: TreeItemIndex,
247+
treeId: string,
248+
setDomFocus?: boolean
249+
) => void;
250+
238251
moveFocusUp: (treeId: string) => void;
239252
moveFocusDown: (treeId: string) => void;
240253
startProgrammaticDrag: () => void;
@@ -396,7 +409,10 @@ export interface TreeChangeActions {
396409
toggleItemExpandedState: (itemId: TreeItemIndex) => void;
397410
selectItems: (itemsIds: TreeItemIndex[]) => void;
398411
toggleItemSelectStatus: (itemId: TreeItemIndex) => void;
399-
focusItem: (itemId: TreeItemIndex) => void;
412+
413+
/** @param setDomFocus - Defaults to true. */
414+
focusItem: (itemId: TreeItemIndex, setDomFocus?: boolean) => void;
415+
400416
moveFocusUp: () => void;
401417
moveFocusDown: () => void;
402418
invokePrimaryAction: (itemId: TreeItemIndex) => void;

0 commit comments

Comments
 (0)