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