简体   繁体   English

Antd Multi Select 清除按钮

[英]Antd Multi Select Clear Button

Below is the basic code from the antd website about creating a multi select option.下面是来自 antd 网站的关于创建多 select 选项的基本代码。 What I want to achieve is create a 'clear' button.我想要实现的是创建一个“清除”按钮。 When clicking clear it will remove all the boxes with the 'x' that say a10, b12, etc. How do I clear out the box?单击清除时,它将删除所有带有“x”的框,例如 a10、b12 等。如何清除框?

I don't want to use allowClear, I want to tie this to my own button我不想使用allowClear,我想把它绑定到我自己的按钮上

https://codesandbox.io/s/g0dec https://codesandbox.io/s/g0dec

const { Select } = antd;

const { Option } = Select;

const children = [];
for (let i = 10; i < 36; i++) {
  children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}

function handleChange(value) {
  console.log(`selected ${value}`);
}

ReactDOM.render(
  <Select
    mode="multiple"
    style={{ width: '100%' }}
    placeholder="Please select"
    defaultValue={['a10', 'c12']}
    onChange={handleChange}
  >
    {children}
  </Select>,
  mountNode,
);

You can do this by making Select controlled component.您可以通过制作Select受控组件来做到这一点。 This is how you can do this by using value prop of Select and useState hook.这就是你可以通过使用SelectuseState钩子的value属性来做到这一点的方法。

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Select } from "antd";

const { Option } = Select;

const children = [];
for (let i = 10; i < 36; i++) {
  children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}

const App = () => {
  const defaultValues = ["a10", "c12"];
  const [selectedValues, setSelectedValues] = useState(defaultValues);

  function handleChange(value) {
    console.log(`selected ${value}`);
    setSelectedValues(value);
  }

  const handleClear = () => {
    setSelectedValues([]);
  };

  return (
    <div>
      <Select
        mode="multiple"
        style={{ width: "100%" }}
        placeholder="Please select"
        defaultValue={selectedValues}
        onChange={handleChange}
        value={selectedValues}
      >
        {children}
      </Select>
      <span>
        <button onClick={handleClear}>Clear</button>
      </span>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("container"));  

Working example here: https://codesandbox.io/s/inspiring-cherry-z6zh0此处的工作示例: https://codesandbox.io/s/inspiring-cherry-z6zh0

You can add the allowClear prop like so:您可以像这样添加allowClear道具:

<Select
    mode="multiple"
    style={{ width: '100%' }}
    placeholder="Please select"
    defaultValue={['a10', 'c12']}
    onChange={handleChange}
    allowClear
  >
    {children}
  </Select>

It will show up a clear button at the right side of the input when you hover it.当你 hover 时,它会在输入的右侧显示一个清除按钮。

You can find it in the antd docs of the Select input您可以在 Select 输入的antd 文档中找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM