import { useFoStore } from '@/app/store'; import type { facilityListProps } from '@/app/store/processFacility'; import { Label } from '@common/components/ui'; import React, { useEffect, useState, useRef, useCallback } from 'react' type thisProps = { label: string | undefined; setValue : (fcltyCd?: string, fcltyNm?: string, fcltyCpc?: number | '', trtFrfNm?: string | '', efflWqltStndrdNm?: string | '') => void; sidoSigun? : string | undefined; fcltyName? : string | undefined; } export default function SearchInputText({label=undefined, setValue=()=>{}, sidoSigun=undefined, fcltyName=undefined} :thisProps) { const { ProcessFacilityList, searchProcessFacilitySearching } = useFoStore((state) => state); const [isOpen, setIsOpen] = useState(false); const [textValue, setTextValue] = useState(fcltyName || ''); const wrapperRef = useRef(null); const processFacilityList = ProcessFacilityList?.processFacilityList || []; const handleInputChange = useCallback((e: React.ChangeEvent) => { const value = e.target.value; setTextValue(value); if (value) { setIsOpen(true); if(sidoSigun) { searchProcessFacilitySearching(1, 4999, 'A', sidoSigun, '0', '0', '0', value); } else { searchProcessFacilitySearching(1, 4999, '0', '0', '0', '0', '0', value); } } else { setIsOpen(false); } }, [searchProcessFacilitySearching, sidoSigun]); const onInputFocus = useCallback(() => { setIsOpen(true); if(sidoSigun) { searchProcessFacilitySearching(1, 4999, 'A', sidoSigun, '0', '0', '0'); } else { searchProcessFacilitySearching(1, 4999, '0', '0', '0', '0', '0'); } }, [searchProcessFacilitySearching, sidoSigun]); useEffect(() => { setTextValue(fcltyName || ''); }, [fcltyName]); const handleSuggestionClick = useCallback((bscFctCd: string, bscFctNm: string, fctCpc: number | '', trtFrfNm: string | '', efflWqltStndrdNm: string | '') => { setTextValue(bscFctNm); setValue(bscFctCd, bscFctNm, fctCpc, trtFrfNm, efflWqltStndrdNm); setIsOpen(false); }, [setValue]); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); return (
{label && } {isOpen && processFacilityList.length > 0 && (
{processFacilityList.map((item : facilityListProps, index : number) => (
handleSuggestionClick(item.bscFctCd, item.bscFctNm, Number(item.fctCpc), item.trtFrfNm, item.efflWqltStndrdNm)} role="button" tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { handleSuggestionClick(item.bscFctCd, item.bscFctNm, Number(item.fctCpc), item.trtFrfNm, item.efflWqltStndrdNm); } }} > {item.sido} {item.gugun} {item.bscFctNm}
))}
)}
) }