Advertisement
ahmadandika

MultiSelectInput

Jul 1st, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import Select from 'react-select';
  3. import { customStyles } from '../../helpers/styleSelect';
  4. import CustomOption from './CustomOption';
  5.  
  6. const transformValue = (value, options, multi) => {
  7.   if (multi && typeof value === 'string') return [];
  8.  
  9.   const filteredOptions = options.filter(option =>
  10.     multi ? value.indexOf(option.value) !== -1 : option.value === value
  11.   );
  12.  
  13.   return multi ? filteredOptions : filteredOptions[0];
  14. };
  15.  
  16. const multiChangeHandler = func => values => {
  17.   func(values.map(value => value.value));
  18. };
  19.  
  20. class MultiSelectInput extends Component {
  21.   constructor(props) {
  22.     super(props);
  23.     this.state = {};
  24.   }
  25.  
  26.   onBlur = e => {
  27.     const blurHandler = this.props.onBlur;
  28.     if (blurHandler) {
  29.       blurHandler({
  30.         type: 'blur',
  31.         target: {
  32.           value: e.target.value
  33.         }
  34.       });
  35.     }
  36.   };
  37.  
  38.   render() {
  39.     const {
  40.       input,
  41.       options,
  42.       name,
  43.       id,
  44.       label,
  45.       disabled,
  46.       placeholder,
  47.       requiredStar,
  48.       defaultValue,
  49.       meta: { touched, error, warning }
  50.     } = this.props;
  51.  
  52.     const transformedValue = transformValue(input.value, options, true);
  53.  
  54.     return (
  55.       <div>
  56.         <div className="form-group">
  57.           <label htmlFor={id}>
  58.             {label}
  59.             {requiredStar === true ? (
  60.               <span className="text-required"> *</span>
  61.             ) : (
  62.               ''
  63.             )}
  64.           </label>
  65.           <Select
  66.             {...input}
  67.             id={id}
  68.             name={name}
  69.             defaultValue={defaultValue}
  70.             isSearchable
  71.             isMulti
  72.             isDisabled={disabled}
  73.             className={`${touched && error ? 'focus-error-select' : ''}`}
  74.             styles={customStyles}
  75.             placeholder={placeholder}
  76.             optionClassName="needsclick"
  77.             options={options}
  78.             components={{ Option: CustomOption }}
  79.             value={transformedValue}
  80.             isClearable
  81.             onChange={multiChangeHandler(input.onChange)}
  82.             onBlur={this.onBlur}
  83.           />
  84.           {touched &&
  85.             ((error && error !== 'Required' && (
  86.               <span className="form-error">{error}</span>
  87.             )) ||
  88.               (warning && <span className="form-error">{warning}</span>))}
  89.         </div>
  90.       </div>
  91.     );
  92.   }
  93. }
  94.  
  95. export default MultiSelectInput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement