ReactJS:如何在redux-form字段中包含react-select?

问题描述 投票:10回答:4

我正在研究react-select库并面临一些问题,我正在使用redux-form库并从中导入<Field />组件。这样我就可以通过表单将值提交给服务了。

我的问题:

当我使用react-select的默认值时,下面提到的代码工作正常。我可以从下拉列表中选择值,即使在聚焦时也会选择该值,该值将保留。但由于redux-form,所选价值不是通过表格提交的,这就是为什么我要包装组件并使用<Field name="sample" component={RenderSelectInput} id="sampleEX" options={options} />

import React from 'react';
import Select from 'react-select';
import RenderSelectInput from './RenderSelectInput'; // my customize select box

var options = [{ value: 'one', label: 'One' }, { value: 'two', label: 'Two' }];


class SelectEx extends React.Component {
    constructor() {
        super();
        this.state = { selectValue: 'sample' }
        this.updateValue = this.updateValue.bind(this);
    }

    updateValue(newValue) {
        this.setState({ selectValue: newValue })
    }

    render() {

        return (
            <div>
                <Select name="select1" id="selectBox" value={this.state.selectValue} options={options} onChange={this.updateValue}/>

                //This works but value won't submit ...

                <Field name="sample" component={RenderSelectInput} id="sampleEX" options={options} />
            //For this, selected value vanishes once I come out of component. 
            </div>
        )
    }
}

export default SelectEx;

但是当我使用我的自定义选择(我正在包装以从表单提交值)时,<Select>组件可以在UI中显示,即使值也是如此。但无法从下拉列表中选择值...,如果我选择它也会显示在<Select>框中,但在焦点上它会消失。请帮我 ...

RenderSelectInput组件:

import React from 'react';
import {Field, reduxForm} from 'redux-form';
import Select from 'react-select';
import 'react-select/dist/react-select.css';


const RenderSelectInput = ({input, options, name, id}) => (

    <div>
        <Select {...input} name={name} options={options} id={id} />
    </div>
)

export default RenderSelectInput;

谢谢,Shash

reactjs redux redux-form react-bootstrap react-select
4个回答
9
投票

当使用带有redux-form的react-select时,你需要改变onChangeonBlur方法的默认行为,并分别调用redux-formonChangeonBlur方法。

所以,试试这个:

const RenderSelectInput = ({input, options, name, id}) => (
    <Select 
         {...input}
         id={id} 
         name={name} 
         options={options}
         value={input.value}
         onChange={(value) => input.onChange(value)}
         onBlur={(value) => input.onBlur(value)}
    />
)

并使用上面的组件

<Field component={RenderSelectInput} />

当焦点从redux-form字段中移除时,调用onBlurSelect方法将防止价值损失。


2
投票

这对我有用,

import React, { Component } from 'react';
import Select from 'react-select';
import 'react-select/dist/react-select.css';

export default class RenderSelectInput extends Component {
 onChange(event) {
  if (this.props.input.onChange && event != null) {
   this.props.input.onChange(event.value);
  } else {
   this.props.input.onChange(null);
  }
 }

 render() {
  const { input, options, name, id, ...custom } = this.props;
  return (
   <Select
    {...input}
    {...custom}
    id={id}
    name={name}
    options={options}
    value={this.props.input.value || ''}
    onBlur={() => this.props.input.onBlur(this.props.input.value)}
    onChange={this.onChange.bind(this)}
   />
  );
 }
}

这是从这里提取的:https://ashiknesin.com/blog/use-react-select-within-redux-form/


1
投票

使用它完美地工作,它还处理redux表单验证。

import React, {Component} from 'react';
import Select from 'react-select';
import {FormGroup} from "reactstrap";


class CustomSelect extends Component {

 render() {
   const {meta: {touched, error}} = this.props;
   const className = ` form-group mb-3 ${touched && error ? 'has-danger' : '' }`;
   return (    
        <FormGroup>
                <Select
                      {...this.props}
                       value={this.props.input.value}
                       onChange={(value) => this.props.input.onChange(value)}
                       onBlur={() => this.props.input.onBlur(this.props.input.value)}
                       options={this.props.options}
                       placeholder={this.props.placeholder}
                    />
                    <div className={className}>
                         <div className="text-help">
                              {touched ? error : ''}
                          </div>
                     </div>
           </FormGroup>

            );

使用redux表单字段组件中的CustomSelect组件作为

   <Field
        name='country_name'
        options={this.state.countries}
        component={CustomSelect}
        placeholder="Select your country"
   />

0
投票

尝试将onBlurResetInput属性设置为false。

就像是。

const SelectInput = ({input: { onChange, value }, options, name, id}) => (
    <Select              
      name={name}
      value={value} 
      options={options}
      onChange={onChange}
      onBlurResetsInput={false}
    />
)

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.