你可以通过设置 'treeCheckStrictly' 属性为 'true' 来实现单选且不允许选择父节点的功能。这样,当你选择一个节点时,其父节点会被自动取消选择。

下面是一个示例代码:

import { TreeSelect } from 'antd';

const treeData = [
  {
    title: 'Parent 1',
    value: 'parent1',
    children: [
      {
        title: 'Child 1',
        value: 'child1',
      },
      {
        title: 'Child 2',
        value: 'child2',
      },
    ],
  },
  {
    title: 'Parent 2',
    value: 'parent2',
    children: [
      {
        title: 'Child 3',
        value: 'child3',
      },
      {
        title: 'Child 4',
        value: 'child4',
      },
    ],
  },
];

function onChange(value) {
  console.log(value);
}

ReactDOM.render(
  <TreeSelect
    treeData={treeData}
    treeCheckable={true}
    treeCheckStrictly={true}
    onChange={onChange}
  />,
  mountNode,
);

在上面的示例中,'treeCheckable' 设置为 'true',表示树节点可选中。'treeCheckStrictly' 设置为 'true',表示严格模式下的选择,即选择一个节点时会自动取消其父节点的选择。

希望能帮到你!

Ant Design TreeSelect 单选且不允许选择父节点

原文地址: https://www.cveoy.top/t/topic/qrmd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录