Search code examples
javascriptreactjsantdmulti-select

Antd Multi-Select Options, but remove entries from search field


I'm using Ant Design's Select functionality. Especially multiple selections.

I would like to keep my selections intact, but I want to get rid of the entries in the search field.

https://i.sstatic.net/f1Vad.jpg

In the image, I want to highlight the selections in the dropdown, but get rid of the entries highlighted in red.

Here's the code I used.

https://codesandbox.io/s/sad-glade-eunf6?file=/index.js

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Select, Radio } 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>);
}

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

class SelectSizesDemo extends React.Component {
  state = {
    size: "default"
  };

  handleSizeChange = e => {
    this.setState({ size: e.target.value });
  };

  render() {
    const { size } = this.state;
    return (
      <>
        <Select
          mode="multiple"
          size={size}
          placeholder="Please select"
          onChange={handleChange}
          style={{ width: "100%" }}
        >
          {children}
        </Select>
      </>
    );
  }
}

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

I tried setting value={[ ]} inside the Select tag, but it wiped the selections as well.

How do I do this?


Solution

  • You can apply the style simply :

    <Select
        mode="multiple"
        size={size}
        className="dont-show" // <--- apply class to provide style only specific to this select
        placeholder="Please select"
        onChange={handleChange}
        style={{ width: "100%" }}
    >
        {children}
    </Select>
    
    .dont-show .ant-select-selection-item {
      display: none !important;
    }
    

    WORKING DEMO :

    Edit peaceful-wescoff-yvxs1