/* Select Input Common React Component Inspired By: react-select , https://codepen.io/dixie0704/pen/jOVxGXL, and others Created By: Aji Kamaludin */ import React, { useRef, useEffect, useState } from 'react' export default function SelectInput(props) { const ref = useRef() const items = [ 'rahayuheni3@gmail.com', 'inovacell04@gmail.com', 'Btg33@gmail.com', 'nikolampo96@gmail.com', 'grinaldifoc@gmail.com', 'nienha_tj26@yahoo.com', 'rizkitriwhyuni139@gmail.com', 'wahyu.saputro6982@gmail.com', 'fariyath315@gmail.com', 'suryaningsihworo@gmail.com', 'andiyanayudi@gmail.com', 'hoshos2303@gmail.com', 'emailnaontehiyeu@gmail.com', 'surahmanomez@gmail.com', 'mmainah172@gmail.com', 'kiosbuuyun@gmail.com', ] const [showItems, setShowItem] = useState(items) const [selected, setSelected] = useState('') const [isOpen, setIsOpen] = useState(false) const [createNew] = useState(true) const disable = false const toggle = () => { setSelected('') setShowItem(items) setIsOpen(!isOpen) } const onInputMouseDown = () => { setSelected('') setShowItem(items) setIsOpen(true) } const handleSelectItem = (item) => { setSelected(item) setIsOpen(false) } const filterItems = (value) => { setSelected(value) setShowItem(items.filter(item => item.toLowerCase().includes(value))) } useEffect(() => { const checkIfClickedOutside = (e) => { if (isOpen && ref.current && !ref.current.contains(e.target)) { setIsOpen(false) } } document.addEventListener('mousedown', checkIfClickedOutside) return () => { document.removeEventListener('mousedown', checkIfClickedOutside) } }, [isOpen]) return (
filterItems(e.target.value)} disabled={disable} />
{} : toggle} className={`text-gray-300 w-8 py-1 pl-2 pr-1 border-l flex items-center border-gray-200 ${ disable ? 'bg-gray-50' : '' }`} >
{isOpen && (
{createNew && (
New
)} {showItems.map((item, index) => (
handleSelectItem(item)} >
{item}
))} {showItems.length <= 0 && (
No Items Found
)}
)}
) }