Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dist
.env.development.local
.env.test.local
.env.production.local
.yarn
.yarnrc.yml

npm-debug.log*
yarn-debug.log*
Expand Down
15 changes: 7 additions & 8 deletions src/components/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { forwardRef, useContext } from "react";

import { SearchIcon } from "./Icons";
import { SelectContext } from "./SelectProvider";
Expand All @@ -10,12 +10,10 @@ interface SearchInputProps {
name?: string;
}

const SearchInput: React.FC<SearchInputProps> = ({
placeholder = "",
value = "",
onChange,
name = ""
}) => {
const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(function SearchInput(
{ placeholder = "", value = "", onChange, name = "" },
ref
) {
const { classNames } = useContext(SelectContext);
return (
<div
Expand All @@ -33,6 +31,7 @@ const SearchInput: React.FC<SearchInputProps> = ({
}
/>
<input
ref={ref}
className={
classNames && classNames.searchBox
? classNames.searchBox
Expand All @@ -46,6 +45,6 @@ const SearchInput: React.FC<SearchInputProps> = ({
/>
</div>
);
};
});

export default SearchInput;
12 changes: 12 additions & 0 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const Select: React.FC<SelectProps> = ({
const [list, setList] = useState<ListOption>(options);
const [inputValue, setInputValue] = useState<string>("");
const ref = useRef<HTMLDivElement>(null);
const searchBoxRef = useRef<HTMLInputElement>(null);

useEffect(() => {
const formatItem = (item: Option) => {
Expand All @@ -91,6 +92,16 @@ const Select: React.FC<SelectProps> = ({
);
}, [options]);

useEffect(() => {
if (isSearchable) {
if (open) {
searchBoxRef.current?.select();
} else {
setInputValue("");
}
}
}, [open, isSearchable]);

const toggle = useCallback(() => {
if (!isDisabled) {
setOpen(!open);
Expand Down Expand Up @@ -297,6 +308,7 @@ const Select: React.FC<SelectProps> = ({
>
{isSearchable && (
<SearchInput
ref={searchBoxRef}
value={inputValue}
placeholder={searchInputPlaceholder}
onChange={e => setInputValue(e.target.value)}
Expand Down