React-select: Removing completely the create new option label

Created on 9 Feb 2017  路  7Comments  路  Source: JedWatson/react-select

The library is very cool and very customisable. I am having a hard time removing completely the "Create option 'x'" label. I can use promptTextCreator={(label) => ''} but there is still empty tag remain. I want to completely remove the label but still able to add new option via key down.

Most helpful comment

You can just use isValidNewOption={() => false}

All 7 comments

How about:

promptTextCreator={(label) => undefined}

@sandinosaso It didn't work to. Still a "empty" space in the selects

problema_select

Hey @joaoeffting and @junibrosas,

I have found a solution for this problem, it's a workaround.

<Select.Creatable
  onChange={this.onChange}
  promptTextCreator={() => false}
  newOptionCreator={({ label, labelKey, valueKey }) => {
    const show = !label

    const option = {}
    option[valueKey] = label
    option[labelKey] = label
    option.className = classnames(
      'Select-create-option-placeholder',
      [styles.hide_placeholder]: show,
    )
    return option
  }}
  value={this.props}
  options={this.props.options}
  isLoading={this.props.fetching}
  labelKey="name"
  valueKey="id"
/>
.hide_placeholder {
  display: none !important;
}

The main trick is:

  • First set the promptTextCreator to return false:
  promptTextCreator={() => false}
  • And after, define a newOptionCreator to handle that option, like so:
  newOptionCreator={({ label, labelKey, valueKey }) => {
    const show = !label

    const option = {}
    option[valueKey] = label
    option[labelKey] = label
    option.className = classnames(
      'Select-create-option-placeholder',
      [styles.hide_placeholder]: show,
    )
    return option
  }}

It's still a thick line, but someone could improve this. My css skills are limited.

@brunoarueira I simply used this to disable (not show) the promptTextCreator:

.Select-create-option-placeholder {
  display: none;
}

Not sure why you need to use newOptionCreator for this because the promptTextCreator has a uniq css class with .Select-create-option-placeholder that can be used for this.

@jnachtigall Great, when I return to the feature which uses the react-select I'll try your suggestion! Thanks, btw

You can just use isValidNewOption={() => false}

You can just use isValidNewOption={() => false}

I'm really confused about why this solution is so well received. This doesn't merely hide the create label, it completely disables the create functionality....if you don't want the create functionality, then why would you even be using Creatable in the first place?

I'm still struggling to find a way to hide the create label in the dropdown without completely breaking all functionality.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

coder-guy22296 picture coder-guy22296  路  3Comments

mjuopperi picture mjuopperi  路  3Comments

yrabinov picture yrabinov  路  3Comments

geraldfullam picture geraldfullam  路  3Comments

batusai513 picture batusai513  路  3Comments