import React from 'react'; import PropTypes from 'prop-types'; import AsyncSelect from 'react-select/async'; import { seafileAPI } from '../utils/seafile-api.js'; import { gettext, enableShowContactEmailWhenSearchUser } from '../utils/constants'; import { Utils } from '../utils/utils.js'; import toaster from './toast'; import '../css/user-select.css'; const propTypes = { placeholder: PropTypes.string.isRequired, onSelectChange: PropTypes.func.isRequired, isMulti: PropTypes.bool.isRequired, className: PropTypes.string, value: PropTypes.string, }; class UserSelect extends React.Component { constructor(props) { super(props); this.options = []; this.state = { searchValue: '', }; } onInputChange = (searchValue) => { this.setState({ searchValue }); } handleSelectChange = (option) => { this.options = []; this.props.onSelectChange(option); } loadOptions = (input, callback) => { const value = input.trim(); if (value.length > 0) { seafileAPI.searchUsers(value).then((res) => { this.options = []; for (let i = 0 ; i < res.data.users.length; i++) { const item = res.data.users[i]; let obj = {}; obj.value = item.name; obj.email = item.email; obj.label = enableShowContactEmailWhenSearchUser ? (
{item.name}
{item.contact_email}
) : ( {item.name} ); this.options.push(obj); } callback(this.options); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } } clearSelect = () => { this.refs.userSelect.select.onChange([], { action: 'clear' }); } render() { const searchValue = this.state.searchValue; const style = { margin: '6px 10px', textAlign: 'center', color: 'hsl(0,0%,50%)' }; return ( { return (
{searchValue ? gettext('User not found') : gettext('Enter characters to start searching')}
); } }} isMulti={this.props.isMulti} loadOptions={this.loadOptions} onChange={this.handleSelectChange} onInputChange={this.onInputChange} placeholder={this.props.placeholder} className={`user-select ${this.props.className}`} value={this.props.value} ref="userSelect" /> ); } } UserSelect.propTypes = propTypes; export default UserSelect;