rsuite
Version:
A suite of react components
108 lines • 3.13 kB
JavaScript
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import React, { useMemo } from 'react';
import useTreeValue from "./hooks/useTreeValue.js";
import CheckTreeView from "./CheckTreeView.js";
import useFlattenTree from "../Tree/hooks/useFlattenTree.js";
import useTreeWithChildren from "../Tree/hooks/useTreeWithChildren.js";
import useExpandTree from "../Tree/hooks/useExpandTree.js";
import { forwardRef } from "../internals/utils/index.js";
import { useEventCallback, useCustom } from "../internals/hooks/index.js";
import { TreeProvider } from "../internals/Tree/TreeProvider.js";
/**
* The `CheckTree` component is used for selecting multiple options which are organized in a tree structure.
* @see https://rsuitejs.com/components/check-tree
*/
const CheckTree = forwardRef((props, ref) => {
const {
propsWithDefaults
} = useCustom('CheckTree', props);
const {
value: controlledValue,
data,
defaultValue,
defaultExpandAll = false,
defaultExpandItemValues = [],
uncheckableItemValues,
disabledItemValues,
expandItemValues: controlledExpandItemValues,
childrenKey = 'children',
labelKey = 'label',
valueKey = 'value',
virtualized,
cascade = true,
scrollShadow,
renderTreeIcon,
renderTreeNode,
getChildren,
onExpand,
onChange,
...rest
} = propsWithDefaults;
const [value, setValue] = useTreeValue(controlledValue, {
defaultValue,
uncheckableItemValues
});
const itemDataKeys = {
childrenKey,
labelKey,
valueKey
};
const {
treeData,
loadingNodeValues,
appendChild
} = useTreeWithChildren(data, itemDataKeys);
const {
expandItemValues,
handleExpandTreeNode
} = useExpandTree(data, {
...itemDataKeys,
defaultExpandAll,
defaultExpandItemValues,
controlledExpandItemValues,
onExpand,
getChildren,
appendChild
});
const flattenedNodes = useFlattenTree(treeData, {
...itemDataKeys,
uncheckableItemValues,
disabledItemValues,
multiple: true,
cascade,
value
});
const handleChange = useEventCallback((nextValue, event) => {
setValue(nextValue);
onChange === null || onChange === void 0 || onChange(nextValue, event);
});
const treeContext = useMemo(() => ({
props: {
labelKey,
valueKey,
childrenKey,
virtualized,
scrollShadow,
renderTreeIcon,
renderTreeNode
}
}), [childrenKey, labelKey, valueKey, virtualized, scrollShadow, renderTreeIcon, renderTreeNode]);
return /*#__PURE__*/React.createElement(TreeProvider, {
value: treeContext
}, /*#__PURE__*/React.createElement(CheckTreeView, _extends({}, rest, {
ref: ref,
value: value,
cascade: cascade,
data: treeData,
loadingNodeValues: loadingNodeValues,
flattenedNodes: flattenedNodes,
uncheckableItemValues: uncheckableItemValues,
disabledItemValues: disabledItemValues,
expandItemValues: expandItemValues,
onChange: handleChange,
onExpand: handleExpandTreeNode
})));
});
CheckTree.displayName = 'CheckTree';
export default CheckTree;