最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to allow any value to be entered in input in React-Select - Stack Overflow

programmeradmin2浏览0评论

I'm using react select to render a select ponent:

const options = [...]

<Select
 ...
 options={options}
/>

The problem is that I any value that I type that is not inside options won't get selected (when the select ponent is not focused the value will disappear).

Is there any way to make <Select/> more of a suggestions ponent that provides autoplete for options but also allows any value to be entered?

I'm using react select to render a select ponent:

const options = [...]

<Select
 ...
 options={options}
/>

The problem is that I any value that I type that is not inside options won't get selected (when the select ponent is not focused the value will disappear).

Is there any way to make <Select/> more of a suggestions ponent that provides autoplete for options but also allows any value to be entered?

Share asked Sep 13, 2021 at 9:52 nilnil 5591 gold badge6 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use the creatable Component to achieve that:

Example from react-select

import React, { Component } from 'react';

import CreatableSelect from 'react-select/creatable';
import { colourOptions } from '../data';

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={colourOptions}
      />
    );
  }
}
发布评论

评论列表(0)

  1. 暂无评论