import React from 'react'; import PropTypes from 'prop-types'; import AsyncSelect from 'react-select/async'; import { seafileAPI } from '../utils/seafile-api'; import { gettext, enableShowContactEmailWhenSearchUser, enableShowLoginIDWhenSearchUser } from '../utils/constants'; import { Utils } from '../utils/utils'; import toaster from './toast'; import { UserSelectStyle, NoOptionsStyle } from './common/select'; import '../css/user-select.css'; const propTypes = { placeholder: PropTypes.string.isRequired, onSelectChange: PropTypes.func.isRequired, isMulti: PropTypes.bool.isRequired, className: PropTypes.string, value: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), }; class UserSelect extends React.Component { constructor(props) { super(props); this.options = []; this.finalValue = ''; this.state = { searchValue: '' }; this.userSelect = React.createRef(); } onInputChange = (searchValue) => { if (!this.props.isMulti && searchValue.trim()) { this.handleSelectChange(null); this.clearSelect(); } this.setState({ searchValue }); }; handleSelectChange = (option) => { this.options = []; this.props.onSelectChange(option); }; loadOptions = (input, callback) => { const value = input.trim(); this.finalValue = value; setTimeout(() => { if (this.finalValue === value && 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 || enableShowLoginIDWhenSearchUser) ? (