ReactJS
1import React from 'react';
2import styles from './dropdownTemplate.css';
3import {noop} from "../../utils/react/noop";
4
5export interface IDropdownProps {
6button: React.ReactNode;
7children: React.ReactNode;
8isOpen?: boolean;
9onOpen?: () => void;
10onClose?: () => void;
11}
12
13
14export function Dropdown({button, children, isOpen, onOpen = noop, onClose = noop}: IDropdownProps) {
15const [isDropdownOpen, setIsDropdownOpen] = React.useState(isOpen)
16React.useEffect(() => setIsDropdownOpen(isOpen), [isOpen]);
17React.useEffect(() => isDropdownOpen ? onOpen() : onClose(), [isDropdownOpen])
18
19const handleOpen = () => {
20if (isOpen === undefined) {
21setIsDropdownOpen(!isDropdownOpen);
22}
23}
24
25return (
26<div className={styles.container}>
27<div onClick={handleOpen}>
28{button}
29</div>
30{isDropdownOpen && (
31<div className={styles.listContainer}>
32<div className={styles.list} onClick={() => setIsDropdownOpen(false)}>
33{children}
34</div>
35</div>
36)}
37</div>
38);
39}
40