import React from 'react';
import PropTypes from 'prop-types';
import Select from 'react-select';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import TextField from '@material-ui/core/TextField';
import MenuItem from '@material-ui/core/MenuItem';
import i18n from 'react-intl-universal';
import styles from './styles';
const _ = require('lodash');
function NoOptionsMessage(props) {
if (props.options.length) return null;
return (
<Typography
className={props.selectProps.classes.noOptionsMessage}
{...props.innerProps}
>
{i18n.get('app.commons.errors.emptySearchResult')}
</Typography>
);
}
function inputComponent({ inputRef, ...props }) {
return <div ref={inputRef} {...props} />;
}
function Option(props) {
return (
<MenuItem
buttonRef={props.innerRef}
selected={props.isFocused}
component="div"
style={{
fontWeight: props.isSelected ? 500 : 400
}}
{...props.innerProps}
>
{props.children}
</MenuItem>
);
}
function SingleValue(props) {
return (
<Typography
className={props.selectProps.classes.singleValue}
{...props.innerProps}
>
{props.children}
</Typography>
);
}
function ValueContainer(props) {
return (
<div className={props.selectProps.classes.valueContainer}>
{props.children}
</div>
);
}
class SearchableSelect extends React.Component {
constructor(props) {
super(props);
this.triggerOnValueChange = _.debounce(this.triggerOnValueChange, 500);
}
state = {
selectedOption: this.props.defaultValue
};
handleChange = selectedOption => {
this.setState({
selectedOption: selectedOption
});
if (this.props.setSelectedOption) {
this.props.setSelectedOption(selectedOption.value);
}
};
placeholder = props => {
const { placeholderColor, placeholderVariant } = this.props;
return (
<Typography
color={placeholderColor ? placeholderColor : 'default'}
variant={placeholderVariant ? placeholderVariant : 'body1'}
className={props.selectProps.classes.placeholder}
{...props.innerProps}
>
{props.children}
</Typography>
);
};
triggerOnValueChange = value => {
this.props.onValueChange(value);
};
handleTextFieldChange = event => {
if (this.props.onValueChange) {
event.persist();
this.triggerOnValueChange(event.target.value);
}
};
control = props => {
return (
<TextField
fullWidth
onChange={this.handleTextFieldChange}
InputProps={{
inputComponent,
inputProps: {
className: props.selectProps.classes.input,
ref: props.innerRef,
children: props.children,
...props.innerProps
}
}}
/>
);
};
render() {
const { classes, options, searchable } = this.props;
const components = {
Option,
Control: this.control,
NoOptionsMessage,
Placeholder: this.placeholder,
SingleValue,
ValueContainer
};
return (
<div className={classes.root}>
<Select
classes={classes}
options={options}
isSearchable={searchable}
components={components}
value={this.state.selectedOption}
onChange={this.handleChange}
placeholder={this.props.placeholder}
/>
</div>
);
}
}
SearchableSelect.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SearchableSelect);