antd design select placeholder issues

17,038

Solution 1

set this.state.company to be undefined instead of null.

Solution 2

you should update as below:

<Select
     showSearch
     optionFilterProp = "children"
     placeholder = "Select Company"
     value = {this.state.company || undefined} ---- update this line
     name = "company"
     onSelect = "{this.handleCompanyChange}"
    >

Solution 3

It should be set to undefined instead of null or "" empty string.

this.props.form.setFieldsValue({
    myFieldName: undefined
})

Solution 4

I have faced the the same issue, heres the solution: Code snippet for ant design select

<Select key="1" value={this.getStateValue()} showSearch allowClear placeholder='Select Weight' onChange={onWeightChange}>
  {options}
</Select>

where getStateValue will be this:

getStateValue = () => {
    const { value } = this.state;
    if (value) {
      return value;
    }
  }
Share:
17,038

Related videos on Youtube

FE_Addict
Author by

FE_Addict

Updated on June 27, 2022

Comments

  • FE_Addict
    FE_Addict almost 2 years

    I am using antd design in my React app.

    Here's a code snippet where I am facing the issues :

    <Select
         showSearch
         optionFilterProp = "children"
         placeholder = "Select Company"
         value = "{this.state.company}"
         name = "company"
         onSelect = "{this.handleCompanyChange}"
        >
    

    Now it shows the correct value selected if this.state.company is not null. But if this.state.company is empty or null, placeholder doesn't shows up.

    How can I solve this issue so that the placeholder appears if value is null?

  • FE_Addict
    FE_Addict almost 7 years
    Do you mean like this : this.state.company=undefined ?
  • NSjonas
    NSjonas almost 6 years
    value = {this.state.company?this.state.company:undefined}
  • 0xh8h
    0xh8h over 4 years
    You can also simplify the above statement with value={this.state.company || undefined}
  • devuxer
    devuxer over 4 years
    @afc163, Can you guys fix this? It should show the placeholder for null too.
  • blueseal
    blueseal about 4 years
    shortcut value = { this.state.company || undefined }
  • Jyoti Duhan
    Jyoti Duhan over 3 years
    hi, It's is not working for me: <Select showSearch placeholder="Select category" value={undefined}>
  • Kelwin Tantono
    Kelwin Tantono over 2 years
    you should put something to the value instead of just undefined @JyotiDuhan, but can you explain why should it be undefined?