question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

when adding isValidNewOption the 'create ""' option is always on

See original GitHub issue

Are you reporting a bug or runtime error?

i want to add validation on new options but it seems to change the default behavior when I’m clicking on the input

image

import React, { Component } from 'react';
 import CreatableSelect from 'react-select/creatable';
const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]

export default class CreatableSingle extends Component<*, State> {
  handleChange = (newValue: any, actionMeta: any) => {
    console.group('Value Changed');
    console.log(newValue);
    console.log(`action: ${actionMeta.action}`);
    console.groupEnd();
  };
  handleInputChange = (inputValue: any, actionMeta: any) => {
    console.group('Input Changed');
    console.log(inputValue);
    console.log(`action: ${actionMeta.action}`);
    console.groupEnd();
  };
  render() {
    return (
      <CreatableSelect
        isClearable
        onChange={this.handleChange}
        onInputChange={this.handleInputChange}
        options={options}
        isValidNewOption={() => true}
      />
    );
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
ebonowcommented, Oct 27, 2021

@praveen7557 is this what you are looking for?

https://codesandbox.io/s/brave-moon-7y0vb?file=/src/App.js

  const isValidNewOption = () => true;

  const createLabel = () => `Create New Option`;

  const onCreateOption = () =>
    alert("Replace this with a modal or do something else...");

  return (
      <CreatableSelect
        isValidNewOption={isValidNewOption}
        formatCreateLabel={createLabel}
        options={basicOptions}
        onCreateOption={onCreateOption}
      />
  );
0reactions
ebonowcommented, Oct 28, 2021

@praveen7557 version 4 introduced new lifecycle methods to make it work with React v17, so you would need to change the version of React to v16 to make it work in the codesandbox. Here is the same example working against version react-select v3.1

https://codesandbox.io/s/brave-moon-7y0vb?file=/src/App.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creatable grouped options show "create" option ...
When there is no grouping, no option is shown to create "Ocean" if it already exists, which is the behaviour I would expect....
Read more >
Adding labels to react-select breaks functionally
I managed to dive into the issue. The solution was to write your own isValidNewOption to replace the builtin and pass it as...
Read more >
API - React Select
isValidNewOption. Determines whether the "create new ..." option should be displayed based on the current input value, select value and options array.
Read more >
Getting started with react-select - YouTube
In this video, we will learn how to create select components for React.0:00 Introduction1:10 Installing React Select2:22 Jumping into ...
Read more >
API
Sets the position of the createOption element in your options list. ... isValidNewOption function = (inputValue, selectValue, selectOptions) => undefined.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found