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 // eslint-disable-next-line react-hooks/exhaustive-deps
50 }, [props.value, props.label]);
53 if (selfClearProp !== props.selfClearProp) {
55 setSelfClearProp(props.selfClearProp);
56 handleChange({ target: { value: "" } } as any);
58 // eslint-disable-next-line react-hooks/exhaustive-deps
59 }, [props.selfClearProp]);
61 const handleSubmit = (event: React.FormEvent<HTMLElement>) => {
62 event.preventDefault();
63 clearTimeout(timeout);
64 props.onSearch(value);
67 const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
68 const { target: { value: eventValue } } = event;
69 clearTimeout(timeout);
72 setTimeout(window.setTimeout(
74 props.onSearch(eventValue);
76 props.debounce || DEFAULT_SEARCH_DEBOUNCE
80 return <form onSubmit={handleSubmit}>
81 <FormControl style={{ width: '14rem'}}>
82 <InputLabel>{label}</InputLabel>
85 data-cy="search-input"
87 onChange={handleChange}
89 <InputAdornment position="end">
90 <Tooltip title='Search'>
92 onClick={handleSubmit}>