All files / components/input/input-group index.js

0% Statements 0/103
0% Branches 0/51
0% Functions 0/24
0% Lines 0/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                                                                                                                                                                                                                                                                                                                                                     
import React, { PureComponent } from 'react'
import Select from 'react-select'
import PropTypes from 'prop-types'
 
import InputContainer from 'utils/input-container'
import InputGroupStyled from './styled-input-group.styled'
 
import { Arrow as ArrowIcon } from 'icons'
 
const arrowRenderer = () => (
  <div>
    <ArrowIcon />
  </div>
);
 
class InputGroup extends PureComponent {
  constructor(p) {
    super(p);
 
    this.state = {
      selectValue: p.selectDefaultValue || p.options[0].value,
      inputValue: p.inputDefaultValue,
      focus: false,
      timeout: null
    }
  }
 
  onChange = () => {
    const { selectValue, inputValue } = this.state;
 
    if (!!selectValue && !!inputValue) {
      this.props.onChange(selectValue, inputValue);
    }
 
  };
 
  onInputChange = (event) => {
    const value = event.target.value;
    this.setState({ inputValue: value }, () => {
      let { timeout } = this.state
 
      if (timeout != null) {
        clearTimeout(timeout);
      }
 
      timeout = setTimeout(() => {
        if (this.props.onInputChange) {
          this.props.onInputChange(this.state.inputValue)
        }
 
        this.onChange();
      }, 500);
 
      this.setState({ timeout })
    })
  };
 
  onSelectChange = (selected) => {
    this.setState({ selectValue: selected }, () => {
      if (this.props.onSelectChange) {
        this.props.onSelectChange(this.state.selectValue)
      }
 
      this.onChange();
    })
  };
 
  renderOption = (option) => {
    return (
      <div className='option-item'>
        <span>{option['label']} {option['shortLabel'] && (<strong>( {option['shortLabel']} )</strong>)}</span>
      </div>
    )
  };
 
  setFocus = (focus) => {
    this.setState({ focus })
  };
 
  render () {
    const {
      position,
      options,
      selectWidthInPx,
      nameSelect,
      nameInput,
      placeholder,
      ...rest
    } = this.props;
 
    const {
      focus,
      selectValue,
      inputValue
    } = this.state;
 
    return (
      <InputContainer {...rest}>
        <InputGroupStyled
          position={position}
          selectWidthInPx={selectWidthInPx}
          inFocus={focus}
        >
          <div className={"elementContainer"}>
            <div className={"select-component"}>
              <Select
                name={nameSelect}
                onOpen={() => this.setFocus(true)}
                onClose={() => this.setFocus(false)}
                onChange={this.onSelectChange}
                value={selectValue}
                placeholder=''
                clearable={false}
                autosize={false}
                multi={false}
                simpleValue
                autoload
                searchable={false}
                backspaceRemoves={false}
                arrowRenderer={arrowRenderer}
                optionRenderer={this.renderOption}
                valueKey='value'
                labelKey='shortLabel'
                options={options}
              />
            </div>
            <div className={"input-component"}>
              <input
                placeholder={placeholder}
                name={nameInput}
                onChange={this.onInputChange}
                onFocus={() => this.setFocus(true)}
                onBlur={() => this.setFocus(false)}
                value={inputValue}
                type='text'
              />
            </div>
          </div>
        </InputGroupStyled>
      </InputContainer>
    )
  }
}
 
InputGroup.propTypes = {
  position: PropTypes.oneOf(['prefix', 'suffix']).isRequired,
  selectWidthInPx: PropTypes.number.isRequired,
  options: PropTypes.arrayOf(PropTypes.shape({
    value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
    label: PropTypes.string.isRequired,
    shortLabel: PropTypes.string.isRequired
  })),
  onChange: PropTypes.func.isRequired,
  onInputChange: PropTypes.func,
  onSelectChange: PropTypes.func,
  label: PropTypes.string,
  nameSelect: PropTypes.string,
  nameInput: PropTypes.string,
  type: PropTypes.oneOf(['primary', 'alert', 'error', 'success']),
  selectDefaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  inputDefaultValue: PropTypes.string,
  placeholder: PropTypes.string,
};
 
InputGroup.defaultProps = {
  placeholder: "-"
};
 
 
export { InputGroup }