Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant design - How do i auto close select ("tags" or "multiple" mode) dropdown after each selection?

I'm using ant.design select component ("tags" or "multiple" mode) in a page, i want dropdown to be automatically closes after each selection. Now it remains open and i should click on other places in the page to close the dropdown list.

import { Select } from 'antd';

const { Option } = Select;

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

ReactDOM.render(
  <Select mode="multiple" placeholder="Select Countries" size="large" onChange={handleChange}>
    <Option value="country1">Country1</Option>
    <Option value="country2">Country2</Option>
    <Option value="country3">Country3</Option>
    <Option value="country4">Country4</Option>
    <Option value="country5">Country5</Option>
    <Option value="country6">Country6</Option>
</Select>,
  mountNode,
);
like image 458
Hossein Haji Mali Avatar asked May 26 '19 14:05

Hossein Haji Mali


People also ask

How do you clear the Select option on an ANTD?

Clear the value of the Selection field by setting it to null programmatically (e.g. via Button). The value will be set to null but the Selection field will still display the most recent selected value.

How do I use ANTD select?

Multiple selection, selecting from existing items. Specify the prop name of Option which will be rendered in select box. Select with tags, transform input to tag (scroll the menu). Coordinating the selection of provinces and cities is a common use case and demonstrates how selection can be coordinated.

How do you select multiple options in a select tag in react?

The first and foremost thing we need to do is change the select element and make it let use select multiple values. The second thing we need to change is the constructor method and add an array so that we can use that array to store multiple selections from the user.


1 Answers

Simply change first "Select" component to this:

<Select 
      mode="multiple" 
      placeholder="Select Countries"
      size="large" 
      ref={(select) => this.countrySelect = select}
      onChange={()=>{ 
          this.countrySelect.blur()
      }}>
    <Option value="country1">Country1</Option>
    <Option value="country2">Country2</Option>
    <Option value="country3">Country3</Option>
    <Option value="country4">Country4</Option>
    <Option value="country5">Country5</Option>
    <Option value="country6">Country6</Option>
</Select>
like image 194
Hossein Haji Mali Avatar answered Oct 17 '22 18:10

Hossein Haji Mali