diff --git a/.gitignore b/.gitignore index 6c9a15e..aaeb352 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,8 @@ dist .env.development.local .env.test.local .env.production.local +.yarn +.yarnrc.yml npm-debug.log* yarn-debug.log* diff --git a/src/components/SearchInput.tsx b/src/components/SearchInput.tsx index 5d9363a..cce8e4f 100755 --- a/src/components/SearchInput.tsx +++ b/src/components/SearchInput.tsx @@ -1,4 +1,4 @@ -import React, { useContext } from "react"; +import React, { forwardRef, useContext } from "react"; import { SearchIcon } from "./Icons"; import { SelectContext } from "./SelectProvider"; @@ -10,12 +10,10 @@ interface SearchInputProps { name?: string; } -const SearchInput: React.FC = ({ - placeholder = "", - value = "", - onChange, - name = "" -}) => { +const SearchInput = forwardRef(function SearchInput( + { placeholder = "", value = "", onChange, name = "" }, + ref +) { const { classNames } = useContext(SelectContext); return (
= ({ } /> = ({ />
); -}; +}); export default SearchInput; diff --git a/src/components/Select.tsx b/src/components/Select.tsx index bea6165..f75c69f 100755 --- a/src/components/Select.tsx +++ b/src/components/Select.tsx @@ -67,6 +67,7 @@ const Select: React.FC = ({ const [list, setList] = useState(options); const [inputValue, setInputValue] = useState(""); const ref = useRef(null); + const searchBoxRef = useRef(null); useEffect(() => { const formatItem = (item: Option) => { @@ -91,6 +92,16 @@ const Select: React.FC = ({ ); }, [options]); + useEffect(() => { + if (isSearchable) { + if (open) { + searchBoxRef.current?.select(); + } else { + setInputValue(""); + } + } + }, [open, isSearchable]); + const toggle = useCallback(() => { if (!isDisabled) { setOpen(!open); @@ -297,6 +308,7 @@ const Select: React.FC = ({ > {isSearchable && ( setInputValue(e.target.value)}