简体   繁体   English

在 react-select 选项下拉菜单中需要一个输入框

[英]Need an Input box inside react-select option dropdown

I am in a situation where I need an input box inside my dropdown option menu.我的情况是我的下拉选项菜单中需要一个输入框。 The scenario is that, I need to create a new option with the dropdown but I am not allowed to use the custom tag creator feature of react select.场景是,我需要使用下拉菜单创建一个新选项,但我不允许使用 react select 的自定义标签创建器功能。

I found in the docs that we can pass a custom option renderer component and I passed the same and when I tried to put an input box inside my option component the I can't get the control of my input box.我在文档中发现我们可以传递自定义选项渲染器组件,并且我传递了相同的内容,当我尝试将输入框放入我的选项组件时,我无法控制输入框。

在此处输入图片说明

The attached picture shows the input inside my option but when I click on the input box I am not getting the control over it.附图显示了我的选项中的输入,但是当我点击输入框时,我无法控制它。

My DeleteComponent.tsx ->我的 DeleteComponent.tsx ->

import { showDeleteConcernModal } from '../../../actions/modalActions';
import { deleteConcernType } from '../../../actions/summaryActions';
import { ISelectOptions } from '../../../interfaces';
import * as React from 'react';
import { FormControl } from 'react-bootstrap';

export interface IDeleteOptionsProps {
    className?: string;
    isDisabled?: boolean;
    isFocused?: boolean;
    isSelected?: boolean;
    onFocus?: Function;
    onSelect?: Function;
    option?: ISelectOptions;
}

export class DeleteOptions extends React.PureComponent<IDeleteOptionsProps, {}> {
    constructor() {
        super();
    }

    handleMouseDown = (event) => {
        event.preventDefault();
        this.props.onSelect(this.props.option, event);
    }

    handleMouseEnter = (event) => {
        event.preventDefault();
        this.props.onFocus(this.props.option, event);
    }

    handleMouseMove = (event) => {
        event.preventDefault();
        this.props.onFocus(this.props.option, event);
    }

    handleOptionDelete = (value) => {
        showDeleteConcernModal('oi-form', value);
    }

    handleChange = (event) => {
        console.log('>> event.target.value', event.target.value);
    }

    render() {
        return (
            <div className={this.props.className}
                onMouseEnter={this.handleMouseEnter}
                onMouseMove={this.handleMouseMove}
            >
                <div style={{ textAlign: 'left', width: '80%', display: 'inline-block' }}>
                    <FormControl
                        type="text"
                        onChange={this.handleChange}
                    />
                </div>
                <div
                    onMouseDown={() => { this.handleOptionDelete(this.props.children); }}
                    className="delete-option"
                    style={{ textAlign: 'right', width: '20%', display: 'inline-block' }}
                >
                    <i className="fa fa-times" aria-hidden="true"></i>
                </div>
            </div>
        );
    }
}

Do you need to allow new options to be created if they do not already exist Right?如果新选项不存在,您是否需要允许创建新选项,对吗?

If yes then try this: https://github.com/JedWatson/react-select如果是,那么试试这个: https : //github.com/JedWatson/react-select

The Creatable component enables users to create new tags within react-select. Creatable 组件使用户能够在 react-select 中创建新标签。 It decorates a Select and so it supports all of the default properties (eg single/multi mode, filtering, etc) in addition to a couple of custom ones (shown below).它装饰了一个 Select,因此除了几个自定义属性(如下所示)之外,它还支持所有默认属性(例如单/多模式、过滤等)。 The easiest way to use it is like so:最简单的使用方法是这样的:

import { Creatable } from 'react-select';

function render (selectProps) {
  return <Creatable {...selectProps} />;
};

Demo: Your suitable solution is custom tag creation from https://jedwatson.github.io/react-select/演示:您合适的解决方案是从https://jedwatson.github.io/react-select/创建自定义标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM