ReactJS: How to wrap react-select in redux-form field?

后端 未结 5 1076
难免孤独
难免孤独 2021-02-13 02:57

I am working on react-select library and facing some issues, I am using redux-form library and importing component from it

相关标签:
5条回答
  • 2021-02-13 03:19

    I had to call the onBlur without any argument. The issue with Hardik's answer was, it was not working in iPad (May be also in other iOS or touch devices. I was unable to check).

    The onBlur event is automatically triggered along with the onChange event in iPad. It caused the select value to reset to its initial value. So I had to call onBlur method like this,

    onBlur={(value) => input.onBlur()}

    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()}
        />
    )
    

    and used as,

    <Field component={RenderSelectInput} />
    
    0 讨论(0)
  • 2021-02-13 03:30

    Use this which works perfectly and it also handles redux form validation.

    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>
    
                );
    

    Use the CustomSelect component in redux form field component as

       <Field
            name='country_name'
            options={this.state.countries}
            component={CustomSelect}
            placeholder="Select your country"
       />
    
    0 讨论(0)
  • 2021-02-13 03:31

    Here this worked for me,

    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)}
       />
      );
     }
    }
    

    this was extracted from here: https://ashiknesin.com/blog/use-react-select-within-redux-form/

    0 讨论(0)
  • 2021-02-13 03:34

    When using react-select with redux-form, you'll need to change the default behavior of onChange and onBlur method and call redux-form's onChange and onBlur method respectively.

    So, Try this:

    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)}
        />
    )
    

    and use the above component like

    <Field component={RenderSelectInput} />
    

    Calling redux-form's onBlur method when focus is removed from the Select field will prevent loss of value.

    0 讨论(0)
  • 2021-02-13 03:35

    Try setting onBlurResetInput property to false.

    Something like.

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

    Hope this helps!

    0 讨论(0)
提交回复
热议问题