Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Tree: Add optional initialCheckedState property #6697

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/mantine.dev/src/pages/core/tree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export interface UseTreeInput {
/** Initial selected state of nodes */
initialSelectedState?: string[];

/** Initial checked state of nodes */
initialCheckedState?: string[];

/** Determines whether multiple node can be selected at a time */
multiple?: boolean;
}
Expand Down
23 changes: 18 additions & 5 deletions packages/@mantine/core/src/components/Tree/use-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { TreeNodeData } from './Tree';

export type TreeExpandedState = Record<string, boolean>;

function getInitialState(
function getInitialExpandedState(
initialState: TreeExpandedState,
data: TreeNodeData[],
value: string | string[] | undefined,
Expand All @@ -20,20 +20,31 @@ function getInitialState(
acc[node.value] = node.value in initialState ? initialState[node.value] : node.value === value;

if (Array.isArray(node.children)) {
getInitialState(initialState, node.children, value, acc);
getInitialExpandedState(initialState, node.children, value, acc);
}
});

return acc;
}

function getInitialCheckedState(initialState: string[], data: TreeNodeData[]) {
const acc: string[] = [];

initialState.forEach((node) => acc.push(...getChildrenNodesValues(node, data)));

return acc;
}

export interface UseTreeInput {
/** Initial expanded state of all nodes */
initialExpandedState?: TreeExpandedState;

/** Initial selected state of nodes */
initialSelectedState?: string[];

/** Initial checked state of nodes */
initialCheckedState?: string[];

/** Determines whether multiple node can be selected at a time */
multiple?: boolean;
}
Expand Down Expand Up @@ -116,22 +127,24 @@ export interface UseTreeReturnType {

export function useTree({
initialSelectedState = [],
initialCheckedState = [],
initialExpandedState = {},
multiple = false,
}: UseTreeInput = {}): UseTreeReturnType {
const [data, setData] = useState<TreeNodeData[]>([]);
const [expandedState, setExpandedState] = useState(initialExpandedState);
const [selectedState, setSelectedState] = useState(initialSelectedState);
const [checkedState, setCheckedState] = useState<string[]>([]);
const [checkedState, setCheckedState] = useState(initialCheckedState);
const [anchorNode, setAnchorNode] = useState<string | null>(null);
const [hoveredNode, setHoveredNode] = useState<string | null>(null);

const initialize = useCallback(
(_data: TreeNodeData[]) => {
setExpandedState((current) => getInitialState(current, _data, selectedState));
setExpandedState((current) => getInitialExpandedState(current, _data, selectedState));
setCheckedState((current) => getInitialCheckedState(current, _data));
setData(_data);
},
[selectedState]
[selectedState, checkedState]
);

const toggleExpanded = useCallback((value: string) => {
Expand Down
Loading