1 import axios from 'axios';
2 import PropTypes from 'prop-types';
3 import React, { useCallback, useEffect, useRef, useState } from 'react';
4 import { Button, Form, ListGroup } from 'react-bootstrap';
6 import Icon from '../common/Icon';
7 import UserBox from '../users/Box';
8 import debounce from '../../helpers/debounce';
10 const UserSelect = ({ name, onChange, value }) => {
11 const [resolved, setResolved] = useState(null);
12 const [results, setResults] = useState([]);
13 const [search, setSearch] = useState('');
14 const [showResults, setShowResults] = useState(false);
16 const ref = useRef(null);
19 const handleEventOutside = e => {
20 if (ref.current && !ref.current.contains(e.target)) {
21 setShowResults(false);
24 document.addEventListener('click', handleEventOutside, true);
25 document.addEventListener('focus', handleEventOutside, true);
27 document.removeEventListener('click', handleEventOutside, true);
28 document.removeEventListener('focus', handleEventOutside, true);
33 const fetch = useCallback(debounce(async phrase => {
37 ctrl = new AbortController();
38 if (!phrase || phrase.length < 3) {
43 const response = await axios.get(`/api/users`, {
50 setResults(response.data);
64 .get(`/api/users/${value}`)
66 setResolved(response.data);
74 return <div className="d-flex justify-content-between">
75 {resolved ? <UserBox discriminator noLink user={resolved} /> : <span>value</span>}
77 onClick={() => onChange({ target: { name, value: null }})}
79 variant="outline-danger"
85 return <div className={`user-select ${showResults ? 'expanded' : 'collapsed'}`} ref={ref}>
87 className="search-input"
88 name={Math.random().toString(20).substr(2, 10)}
89 onChange={e => setSearch(e.target.value)}
90 onFocus={() => setShowResults(true)}
94 <div className="search-results-holder">
95 <ListGroup className="search-results">
96 {results.map(result =>
100 onClick={() => onChange({
101 target: { name, value: result.id },
104 <UserBox discriminator noLink user={result} />
112 UserSelect.propTypes = {
113 name: PropTypes.string,
114 onChange: PropTypes.func,
115 value: PropTypes.string,
118 export default UserSelect;