-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCellGroupSelectInput.tsx
More file actions
81 lines (76 loc) · 2.58 KB
/
Copy pathCellGroupSelectInput.tsx
File metadata and controls
81 lines (76 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { useState, useEffect, useRef } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import styles from './cells.module.scss';
import { faChevronDown } from '@fortawesome/free-solid-svg-icons';
import AvoidOffscreen from '../AvoidOffscreen';
import DropContainer from '../DropContainer';
import OptionList from '../OptionList';
import OptionListGroup from '../OptionList/OptionListGroup';
import OptionListItem from '../OptionList/OptionListItem';
import TextField from '../TextField';
interface CellGroupSelectInputProps {
items: { name: string; items: { value: string; text: string }[] }[];
value?: string;
}
export default function CellGroupSelectInput({
items,
value,
}: CellGroupSelectInputProps) {
const [show, setShow] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (ref.current && show) {
const onDocumentClick = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) {
setShow(false);
}
};
document.addEventListener('click', onDocumentClick);
return () => document.removeEventListener('click', onDocumentClick);
}
}, [ref, show, setShow]);
return (
<div ref={ref} style={{ position: 'relative' }}>
<div
className={styles.dropdownContainer}
onClick={() => {
setShow(true);
}}
>
<div className={styles.dropdownContent}>{value}</div>
<div className={styles.dropdownArrow}>
<FontAwesomeIcon icon={faChevronDown} />
</div>
</div>
{show && (
<div style={{ position: 'absolute', zIndex: 100 }}>
<AvoidOffscreen>
<DropContainer>
<div style={{ padding: 5 }}>
<TextField autoFocus placeholder="Search type" />
</div>
<div
style={{ height: 300, width: 250, overflowY: 'auto' }}
className="scroll"
>
<OptionList>
{items.map((group) => {
return (
<OptionListGroup key={group.name} text={group.name}>
{group.items.map((sub) => {
return (
<OptionListItem key={sub.value} text={sub.text} />
);
})}
</OptionListGroup>
);
})}
</OptionList>
</div>
</DropContainer>
</AvoidOffscreen>
</div>
)}
</div>
);
}