2017-11-07 16:33:15 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* InputSearch
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-11-07 18:16:42 +01:00
|
|
|
import { includes, isEmpty, map, toLower } from 'lodash';
|
2017-11-07 16:33:15 +01:00
|
|
|
import cn from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2017-11-07 18:16:42 +01:00
|
|
|
import InputSearchLi from 'components/InputSearchLi';
|
|
|
|
|
2017-11-07 16:33:15 +01:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
class InputSearch extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
2017-11-07 18:16:42 +01:00
|
|
|
state = { errors: [], value: '', users: this.props.values, filteredUsers: this.props.values };
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.didDeleteUser !== this.props.didDeleteUser) {
|
|
|
|
this.setState({ users: nextProps.values, filteredUsers: nextProps.values });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = ({ target }) => {
|
|
|
|
const filteredUsers = isEmpty(target.value) ?
|
|
|
|
this.state.users
|
|
|
|
: this.state.users.filter((user) => {
|
|
|
|
if (includes(toLower(user.name), toLower(target.value))) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.setState({ value: target.value, filteredUsers });
|
|
|
|
}
|
2017-11-07 16:33:15 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className={cn(styles.inputSearch, 'col-md-6')}>
|
|
|
|
<label htmlFor={this.props.name}>
|
|
|
|
<FormattedMessage id={this.props.label} values={this.props.labelValues} />
|
|
|
|
</label>
|
|
|
|
<div className={cn('input-group')}>
|
|
|
|
<span className={cn('input-group-addon', styles.addon)} />
|
|
|
|
<FormattedMessage id="users-permissions.InputSearch.placeholder">
|
|
|
|
{(message) => (
|
|
|
|
<input
|
|
|
|
className={cn('form-control', !isEmpty(this.state.errors) ? 'is-invalid': '')}
|
|
|
|
id={this.props.name}
|
|
|
|
name={this.props.name}
|
2017-11-07 18:16:42 +01:00
|
|
|
onChange={this.handleChange}
|
|
|
|
value={this.state.value}
|
2017-11-07 16:33:15 +01:00
|
|
|
placeholder={message}
|
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</FormattedMessage>
|
|
|
|
</div>
|
2017-11-07 18:16:42 +01:00
|
|
|
<div className={styles.ulContainer}>
|
|
|
|
<ul>
|
|
|
|
{map(this.state.filteredUsers, (user) => (
|
|
|
|
<InputSearchLi key={user.name} item={user} onClickDelete={this.props.onClickDelete} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2017-11-07 16:33:15 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputSearch.defaultProps = {
|
|
|
|
labelValues: {
|
|
|
|
number: 0,
|
|
|
|
},
|
|
|
|
value: '',
|
|
|
|
}
|
|
|
|
|
|
|
|
InputSearch.proptypes = {
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
labelValues: PropTypes.object,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-11-07 18:16:42 +01:00
|
|
|
onClickDelete: PropTypes.func.isRequired,
|
2017-11-07 16:33:15 +01:00
|
|
|
value: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default InputSearch;
|