@zidian-primitive/cascader 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,9 +8,10 @@ const CascaderNode = memo((props) => {
8
8
  node,
9
9
  isLeaf,
10
10
  isActive,
11
+ scrollMethod = "smooth",
12
+ render,
11
13
  onClick,
12
14
  onTrigger,
13
- render,
14
15
  ...elementProps
15
16
  } = props;
16
17
  const { setNodeCheckStatus } = useContext(CascaderContext);
@@ -40,15 +41,15 @@ const CascaderNode = memo((props) => {
40
41
  if (nodeTop < viewTop) {
41
42
  parent.scrollTo({
42
43
  top: nodeTop,
43
- behavior: "smooth"
44
+ behavior: scrollMethod
44
45
  });
45
46
  } else if (nodeBottom > viewBottom) {
46
47
  parent.scrollTo({
47
48
  top: nodeBottom - parent.clientHeight,
48
- behavior: "smooth"
49
+ behavior: scrollMethod
49
50
  });
50
51
  }
51
- }, [isActive]);
52
+ }, [isActive, scrollMethod]);
52
53
  return /* @__PURE__ */ jsx("div", { ref: handleRef, onClick: handleClick, "data-active": isActive ? isActive : void 0, ...elementProps, children: render ? render({
53
54
  node,
54
55
  handleToggleSelect: (node?.checkStatus || node.checkStatus === 0) && setNodeCheckStatus ? (nodeIdx) => setNodeCheckStatus(nodeIdx, getNextStatus(node.checkStatus)) : void 0
@@ -17,7 +17,7 @@ function useCascader(options) {
17
17
  return buildCascaderEngine(sortedData, handleDataConfig);
18
18
  }, [sortedData, handleDataConfig]);
19
19
  const showConfig = useMemo(() => {
20
- return pathOption?.showConfig ? pathOption.showConfig : { depth: handleDataConfig.levels.length - 1, showType: "label" };
20
+ return pathOption?.showConfig ? pathOption.showConfig : { depth: handleDataConfig.levels.length - 1, showType: "label", pathType: "value" };
21
21
  }, [pathOption?.showConfig]);
22
22
  const selectValueType = useMemo(() => selectOption?.valueType ?? "label", [selectOption?.valueType]);
23
23
  const getStatusByOption = useCallback((sourceNodeData2, selected) => {
@@ -35,16 +35,24 @@ function useCascader(options) {
35
35
  return statusArray;
36
36
  }, [selectValueType, handleDataConfig]);
37
37
  const getPathByOption = useCallback((sourceNodeData2, value) => {
38
+ let targetNode;
39
+ if (showConfig.pathType === "value") {
40
+ targetNode = sourceNodeData2.find((node) => showConfig.depth === node.depth && node[showConfig.showType] === value);
41
+ } else {
42
+ targetNode = sourceNodeData2.find((node) => node.nodeId === value);
43
+ }
38
44
  return getShowPath(
39
45
  sourceNodeData2,
40
- sourceNodeData2.find((node) => showConfig.depth === node.depth && node[showConfig.showType] === value)
46
+ targetNode
41
47
  );
42
48
  }, [showConfig]);
43
- const isControlled = useMemo(() => pathOption?.showValue || pathOption?.showValue === 0, [pathOption?.showValue]);
44
- const initialPath = useMemo(() => getPathByOption(
45
- sourceNodeData,
46
- pathOption?.showValue ?? pathOption?.defaultShowValue
47
- ), [sourceNodeData, pathOption?.defaultShowValue, pathOption?.showValue]);
49
+ const isControlled = useMemo(() => pathOption?.showPath || pathOption?.showPath === 0 || pathOption?.showPath === "", [pathOption?.showPath]);
50
+ const initialPath = useMemo(() => {
51
+ return getPathByOption(
52
+ sourceNodeData,
53
+ pathOption?.showPath ?? pathOption?.defaultShowPath
54
+ );
55
+ }, [sourceNodeData, pathOption?.defaultShowPath, pathOption?.showPath]);
48
56
  const initialSelected = useMemo(() => new Set(
49
57
  selectOption?.selected ?? selectOption?.defaultSelected
50
58
  ), [selectOption?.selected, selectOption?.defaultSelected]);
@@ -55,19 +63,23 @@ function useCascader(options) {
55
63
  const [internalPath, setInternalPath] = useState(initialPath);
56
64
  const [internalSelected, setInternalSelected] = useState(initialSelected);
57
65
  const [checkStatusArray, setCheckStatusArray] = useState(initialStatusArray);
58
- const path = useMemo(() => isControlled ? initialPath : internalPath, [sourceNodeData, internalPath, isControlled]);
59
- const columns = useMemo(() => getCascadeColumns(sourceNodeData, path), [sourceNodeData, initialPath, path]);
66
+ const path = useMemo(() => isControlled ? initialPath : internalPath, [internalPath, isControlled]);
67
+ const columns = useMemo(() => getCascadeColumns(sourceNodeData, path), [sourceNodeData, path]);
60
68
  const handleChangePath = useCallback((depth, index) => {
61
- if (pathOption?.onShowValueChange && depth === showConfig.depth) {
62
- pathOption.onShowValueChange(sourceNodeData[index][showConfig.showType], depth);
69
+ if (pathOption?.onShowValueChange) {
70
+ if (showConfig.pathType === "value" && depth === showConfig.depth) {
71
+ pathOption.onShowValueChange(sourceNodeData[index][showConfig.showType], depth);
72
+ } else {
73
+ pathOption.onShowValueChange(sourceNodeData[index]["nodeId"], depth);
74
+ }
63
75
  }
64
- if (pathOption?.showValue !== 0 && !pathOption?.showValue) {
76
+ if (!isControlled) {
65
77
  setInternalPath(changeShowPath(
66
78
  sourceNodeData,
67
79
  { oldPath: path, action: { depth, nodeIdx: index } }
68
80
  ));
69
81
  }
70
- }, [pathOption, sourceNodeData]);
82
+ }, [pathOption, sourceNodeData, isControlled, showConfig]);
71
83
  const handleChangeCheckStatus = useCallback((nodeIdx, checkStatus) => {
72
84
  if (selectOption) {
73
85
  if (selectOption?.selected) {
@@ -106,10 +118,10 @@ function useCascader(options) {
106
118
  }
107
119
  }, [selectOption?.selected]);
108
120
  useEffect(() => {
109
- if (pathOption?.defaultShowValue) {
121
+ if (pathOption?.defaultShowPath) {
110
122
  setInternalPath(initialPath);
111
123
  }
112
- }, [pathOption?.defaultShowValue]);
124
+ }, [pathOption?.defaultShowPath]);
113
125
  useEffect(() => {
114
126
  if (selectOption?.defaultSelected) {
115
127
  setInternalSelected(initialSelected);
package/esm/utils/data.js CHANGED
@@ -89,7 +89,7 @@ function getShowPath(nodes, targetNode) {
89
89
  }
90
90
  while (childNode) {
91
91
  showPath[childNode.depth] = childNode.index;
92
- childNode = childNode.childrenIndices[0] ? childNode : null;
92
+ childNode = childNode.childrenIndices[0] ? nodes[childNode.childrenIndices[0]] : null;
93
93
  }
94
94
  return showPath;
95
95
  }
@@ -4,8 +4,9 @@ export interface CascaderNodeProps extends Omit<ComponentProps<'div'>, "onClick"
4
4
  node: CascaderItem<any>;
5
5
  isLeaf: boolean;
6
6
  isActive: boolean;
7
+ scrollMethod?: "instant" | "smooth";
8
+ render?: NodeSlotType;
7
9
  onClick?: (node: CascaderItem<any>) => void;
8
10
  onTrigger?: (depth: number, nodeIdx: number) => void;
9
- render?: NodeSlotType;
10
11
  }
11
12
  export declare const CascaderNode: React.FC<CascaderNodeProps>;
@@ -43,12 +43,13 @@ export type GetCascaderColumnsOption = {
43
43
  * Other
44
44
  *---------------------------------------------**/
45
45
  export type CascaderPathOption = {
46
+ showPath?: string | number;
46
47
  showConfig?: {
47
48
  depth: number;
48
49
  showType: "label" | "value";
50
+ pathType: "value" | "nodeId";
49
51
  };
50
- showValue?: string | number;
51
- defaultShowValue?: string | number;
52
+ defaultShowPath?: string | number;
52
53
  onShowValueChange?: (value: string | number, depth: number) => void;
53
54
  };
54
55
  export type CascaderSelectOption = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zidian-primitive/cascader",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "main": "./src/index.ts",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,7 +0,0 @@
1
- import { jsx } from 'react/jsx-runtime';
2
-
3
- const CascaderNodeCheck = () => {
4
- return /* @__PURE__ */ jsx("div", {});
5
- };
6
-
7
- export { CascaderNodeCheck };
@@ -1,20 +0,0 @@
1
- import { jsxs, jsx } from 'react/jsx-runtime';
2
- import { useContext } from 'react';
3
- import { CascaderContext } from '../../context/index.js';
4
-
5
- const CascaderPanelHeader = (props) => {
6
- const {
7
- column,
8
- showNode,
9
- render,
10
- ...elementProps
11
- } = props;
12
- const { selectType } = useContext(CascaderContext);
13
- if (render) render({ column, showNode });
14
- else return /* @__PURE__ */ jsxs("div", { ...elementProps, children: [
15
- /* @__PURE__ */ jsx("span", { children: showNode?.level }),
16
- /* @__PURE__ */ jsx("span", { children: column.length })
17
- ] });
18
- };
19
-
20
- export { CascaderPanelHeader };
@@ -1 +0,0 @@
1
- export declare const CascaderNodeCheck: () => import("react/jsx-runtime").JSX.Element;
@@ -1,8 +0,0 @@
1
- import { ComponentProps, FC } from "react";
2
- import { CascaderItem, CascaderNodeType, PanelHeaderSlot } from "../../types";
3
- export interface CascaderPanelHeaderProps extends ComponentProps<'div'> {
4
- column: CascaderNodeType<any>[];
5
- showNode: CascaderItem<any> | null;
6
- render?: PanelHeaderSlot;
7
- }
8
- export declare const CascaderPanelHeader: FC<CascaderPanelHeaderProps>;
@@ -1,2 +0,0 @@
1
- export * from "./data";
2
- export * from "./logic";