@zidian-primitive/cascader 0.3.1 → 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,18 +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 === "", [pathOption?.showValue]);
49
+ const isControlled = useMemo(() => pathOption?.showPath || pathOption?.showPath === 0 || pathOption?.showPath === "", [pathOption?.showPath]);
44
50
  const initialPath = useMemo(() => {
45
51
  return getPathByOption(
46
52
  sourceNodeData,
47
- pathOption?.showValue ?? pathOption?.defaultShowValue
53
+ pathOption?.showPath ?? pathOption?.defaultShowPath
48
54
  );
49
- }, [sourceNodeData, pathOption?.defaultShowValue, pathOption?.showValue]);
55
+ }, [sourceNodeData, pathOption?.defaultShowPath, pathOption?.showPath]);
50
56
  const initialSelected = useMemo(() => new Set(
51
57
  selectOption?.selected ?? selectOption?.defaultSelected
52
58
  ), [selectOption?.selected, selectOption?.defaultSelected]);
@@ -60,8 +66,12 @@ function useCascader(options) {
60
66
  const path = useMemo(() => isControlled ? initialPath : internalPath, [internalPath, isControlled]);
61
67
  const columns = useMemo(() => getCascadeColumns(sourceNodeData, path), [sourceNodeData, path]);
62
68
  const handleChangePath = useCallback((depth, index) => {
63
- if (pathOption?.onShowValueChange && depth === showConfig.depth) {
64
- 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
+ }
65
75
  }
66
76
  if (!isControlled) {
67
77
  setInternalPath(changeShowPath(
@@ -69,7 +79,7 @@ function useCascader(options) {
69
79
  { oldPath: path, action: { depth, nodeIdx: index } }
70
80
  ));
71
81
  }
72
- }, [pathOption, sourceNodeData, isControlled]);
82
+ }, [pathOption, sourceNodeData, isControlled, showConfig]);
73
83
  const handleChangeCheckStatus = useCallback((nodeIdx, checkStatus) => {
74
84
  if (selectOption) {
75
85
  if (selectOption?.selected) {
@@ -108,10 +118,10 @@ function useCascader(options) {
108
118
  }
109
119
  }, [selectOption?.selected]);
110
120
  useEffect(() => {
111
- if (pathOption?.defaultShowValue) {
121
+ if (pathOption?.defaultShowPath) {
112
122
  setInternalPath(initialPath);
113
123
  }
114
- }, [pathOption?.defaultShowValue]);
124
+ }, [pathOption?.defaultShowPath]);
115
125
  useEffect(() => {
116
126
  if (selectOption?.defaultSelected) {
117
127
  setInternalSelected(initialSelected);
@@ -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.1",
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";