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

[Cascader] React18.2.0, 受控 Cascader,异步加载数据时,展开不符合预期 #2212

Open
1 task done
YyumeiZhang opened this issue Apr 29, 2024 · 0 comments
Open
1 task done
Assignees

Comments

@YyumeiZhang
Copy link
Collaborator

YyumeiZhang commented Apr 29, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Which Component

Cascader

Semi Version

2.57.0

Current Behavior

20240429170726_rec_

Expected Behavior

20240429170829_rec_

Steps To Reproduce

react/react-dom 版本需使用 18.2.0
复现 codesanbox

ReproducibleCode

import { Button } from '@douyinfe/semi-ui';
import React, { useState, useEffect,  useRef }  from "react";
import { Modal, Form, Cascader } from "@douyinfe/semi-ui";

const initialData = [
  {
    label: "Node1",
    value: "0-0",
  },
  {
    label: "Node2",
    value: "0-1",
  },
  {
    label: "Node3",
    value: "0-2",
    isLeaf: true,
  },
];
 
export default function App() {
  const [value, setValue] = useState([]);
  const onChange = useEffect((value) => {
    setValue(value);
  },[]);

  const [data, setData] = useState(initialData);
  const updateTreeDataC = (list, value, children) => {
    return list.map((node) => {
      if (node.value === value) {
        return { ...node, children };
      }
      if (node.children) {
        return {
          ...node,
          children: updateTreeDataC(node.children, value, children),
        };
      }
      return node;
    });
  };

  const onLoadDataC = (selectedOpt) => {
    const targetOpt = selectedOpt[selectedOpt.length - 1];
    const { label, value } = targetOpt;
    return new Promise((resolve) => {
      if (targetOpt.children) {
        resolve();
        return;
      }
      setTimeout(() => {
        setData((origin) =>
          updateTreeDataC(origin, value, [
            {
              label: `${label} - 1`,
              value: `${label}-1`,
              isLeaf: selectedOpt.length > 1,
            },
            {
              label: `${label} - 2`,
              value: `${label}-2`,
              isLeaf: selectedOpt.length > 1,
            },
          ])
        );
        resolve();
      }, 1000);
    });
  };


  return (
    <>
      <div>
        <Cascader
           value={value}
           onChange={onChange}
           style={{ width: 300 }}
           treeData={data}
           loadData={onLoadDataC}
           placeholder="Please select"
        />
      </div>
    </>
  );
}

Environment

- OS:
- browser:

Anything else?

No response

@YyumeiZhang YyumeiZhang self-assigned this May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant