1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, {useState, useEffect} from 'react';
13 } from '@material-ui/core';
14 import SearchIcon from '@material-ui/icons/Search';
16 interface SearchInputDataProps {
19 selfClearProp: string;
22 interface SearchInputActionProps {
23 onSearch: (value: string) => any;
27 type SearchInputProps = SearchInputDataProps & SearchInputActionProps;
29 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
31 export const SearchInput = (props: SearchInputProps) => {
32 const [timeout, setTimeout] = useState(0);
33 const [value, setValue] = useState("");
34 const [label, setLabel] = useState("Search");
35 const [selfClearProp, setSelfClearProp] = useState("");
39 setValue(props.value);
42 setLabel(props.label);
47 clearTimeout(timeout);
49 }, [props.value, props.label]); // eslint-disable-line react-hooks/exhaustive-deps
52 if (selfClearProp !== props.selfClearProp) {
54 setSelfClearProp(props.selfClearProp);
55 handleChange({ target: { value: "" } } as any);
57 }, [props.selfClearProp]); // eslint-disable-line react-hooks/exhaustive-deps
59 const handleSubmit = (event: React.FormEvent<HTMLElement>) => {
60 event.preventDefault();
61 clearTimeout(timeout);
62 props.onSearch(value);
65 const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
66 const { target: { value: eventValue } } = event;
67 clearTimeout(timeout);
70 setTimeout(window.setTimeout(
72 props.onSearch(eventValue);
74 props.debounce || DEFAULT_SEARCH_DEBOUNCE
78 return <form onSubmit={handleSubmit}>
80 <InputLabel>{label}</InputLabel>
83 data-cy="search-input"
85 onChange={handleChange}
87 <InputAdornment position="end">
88 <Tooltip title='Search'>
90 onClick={handleSubmit}>