Skip to content

Commit

Permalink
feedback - flavien
Browse files Browse the repository at this point in the history
  • Loading branch information
noraleonte committed Feb 28, 2024
1 parent 73f0f22 commit cd966cf
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 16 deletions.
2 changes: 0 additions & 2 deletions docs/data/tree-view/rich-tree-view/focus/focus.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ return <RichTreeView apiRef={apiRef} items={ITEMS}>;
`apiRef` will be undefined during the first render and will then contain methods allowing you to imperatively interact with the Tree View.
:::

The `focusNode` receives two parameters: `event` and `nodeId`.

:::info
This method only works with nodes that are currently visible.
Calling `apiRef.focusNode` on a node whose parent is collapsed will do nothing.
Expand Down
2 changes: 0 additions & 2 deletions docs/data/tree-view/simple-tree-view/focus/focus.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ return <SimpleTreeView apiRef={apiRef}>{children}</SimpleTreeView>;
`apiRef` will be undefined during the first render and will then contain methods allowing you to imperatively interact with the Tree View.
:::

The `focusNode` receives two parameters: `event` and `nodeId`.

:::info
This method only works with nodes that are currently visible.
Calling `apiRef.focusNode` on a node whose parent is collapsed will do nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ describe('<SimpleTreeView />', () => {

expect(onNodeFocus.lastCall.lastArg).to.equal('1');
});

it('should focus specific node using `apiRef`', () => {
let apiRef: SimpleTreeViewApiRef;
const onNodeFocus = spy();
Expand All @@ -558,6 +559,7 @@ describe('<SimpleTreeView />', () => {
expect(getByRole('tree')).toHaveFocus();
expect(onNodeFocus.lastCall.lastArg).to.equal('2');
});

it('should not focus node if parent is collapsed', () => {
let apiRef: SimpleTreeViewApiRef;
const onNodeFocus = spy();
Expand Down
2 changes: 1 addition & 1 deletion packages/x-tree-view/src/hooks/useTreeViewApiRef.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { TreeViewAnyPluginSignature, TreeViewUsedPublicAPI } from '../internals/models';

/**
* Hook that instantiate a [[TreeViewApiRef]].
* Hook that instantiates a [[TreeViewApiRef]].
*/
export const useTreeViewApiRef = <
T extends TreeViewAnyPluginSignature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export const useTreeViewFocus: TreeViewPlugin<UseTreeViewFocusSignature> = ({
models,
rootRef,
}) => {
const focusedNodeId = state.focusedNodeId;
const setFocusedNodeId = useEventCallback((nodeId: React.SetStateAction<string | null>) => {
const cleanNodeId = typeof nodeId === 'function' ? nodeId(focusedNodeId) : nodeId;
if (focusedNodeId !== cleanNodeId) {
const cleanNodeId = typeof nodeId === 'function' ? nodeId(state.focusedNodeId) : nodeId;
if (state.focusedNodeId !== cleanNodeId) {
setState((prevState) => ({ ...prevState, focusedNodeId: cleanNodeId }));
}
});
Expand All @@ -31,8 +30,8 @@ export const useTreeViewFocus: TreeViewPlugin<UseTreeViewFocusSignature> = ({
);

const isNodeFocused = React.useCallback(
(nodeId: string) => focusedNodeId === nodeId && isTreeViewFocused(),
[focusedNodeId, isTreeViewFocused],
(nodeId: string) => state.focusedNodeId === nodeId && isTreeViewFocused(),
[state.focusedNodeId, isTreeViewFocused],
);

const isNodeVisible = (nodeId: string) => {
Expand Down Expand Up @@ -113,7 +112,7 @@ export const useTreeViewFocus: TreeViewPlugin<UseTreeViewFocusSignature> = ({
setFocusedNodeId(null);
};

const focusedNode = instance.getNode(focusedNodeId!);
const focusedNode = instance.getNode(state.focusedNodeId!);
const activeDescendant = focusedNode
? instance.getTreeItemId(focusedNode.id, focusedNode.idAttribute)
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type UseTreeViewParameters<
export interface UseTreeViewBaseParameters<
TPlugins extends readonly TreeViewPlugin<TreeViewAnyPluginSignature>[],
> {
apiRef?:
apiRef:
| React.MutableRefObject<TreeViewPublicAPI<ConvertPluginsIntoSignatures<TPlugins>>>
| undefined;
rootRef?: React.Ref<HTMLUListElement> | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,5 @@ export const populatePublicAPI = <T extends TreeViewAnyPluginSignature>(
publicAPI: TreeViewUsedPublicAPI<T>,
methods: T['publicAPI'],
) => {
if (!publicAPI) {
publicAPI = {} as TreeViewUsedPublicAPI<T>;
}

Object.assign(publicAPI, methods);
};

0 comments on commit cd966cf

Please sign in to comment.