import React, { useState, useEffect, useRef } from 'react'; import Icon from '../Icon'; import Divider from '../Divider'; import ClickAwayListener from '../ClickAwayListener'; import { Container, Selected, Content, InputContent, OptionWrapper, Option, } from './styled'; type Props = { onChange?: (item: SelectOptionType) => void; options: SelectOptionType[]; defaultValue?: React.ReactNode; isDivide?: boolean; useInput?: boolean; style?: Record; }; const SelectBox: React.FC = ({ onChange, options, defaultValue, isDivide, useInput, style, }: Props) => { const selectRef = useRef(null); const optionRef = useRef(null); const [isCollapse, setCollapse] = useState(true); const [value, setValue] = useState(defaultValue); const handleClick = (): void => { setCollapse(!isCollapse); }; const handleSelect = (index: number): void => { setValue(options[index].content); if (onChange) { onChange(options[index]); } }; const handleBlur = (): void => { setCollapse(true); }; const handleChange = (e: React.ChangeEvent): void => { setValue(e.target.value || 0); }; const handleKeyDown = (e: React.KeyboardEvent): void => { if (e.keyCode === 13 && onChange && value) { const param = { key: '', content: '', child: parseInt(value as string, 10), }; onChange(param); setCollapse(true); } }; useEffect(() => { if (defaultValue) { setValue(defaultValue); } else { setValue(options[0].content); } }, [defaultValue]); useEffect(() => { if (!isCollapse && selectRef.current) { const position = selectRef.current.getBoundingClientRect(); if (optionRef.current) { optionRef.current.style.top = `${ selectRef.current.clientHeight + position.top }px`; optionRef.current.style.left = `${position.left}px`; } } }); return ( {useInput && (typeof value === 'string' || typeof value === 'number') ? ( ) : ( {value} )} {isDivide ? : null} {!isCollapse ? ( {options.map((option: SelectOptionType, index: number) => ( ))} ) : null} ); }; SelectBox.defaultProps = { isDivide: false, useInput: false, style: {}, defaultValue: '', }; export default SelectBox;