1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, {useState, useEffect} from 'react';
6 import { IconButton, StyleRulesCallback, withStyles, WithStyles, FormControl, InputLabel, Input, InputAdornment, Tooltip } from '@material-ui/core';
7 import SearchIcon from '@material-ui/icons/Search';
9 type CssRules = 'container' | 'input' | 'button';
11 const styles: StyleRulesCallback<CssRules> = theme => {
19 borderRadius: theme.spacing.unit / 4,
20 boxSizing: 'border-box',
21 padding: theme.spacing.unit,
22 paddingRight: theme.spacing.unit * 4,
27 top: theme.spacing.unit / 2,
28 right: theme.spacing.unit / 2,
29 width: theme.spacing.unit * 3,
30 height: theme.spacing.unit * 3
35 interface SearchInputDataProps {
38 selfClearProp: string;
41 interface SearchInputActionProps {
42 onSearch: (value: string) => any;
46 type SearchInputProps = SearchInputDataProps & SearchInputActionProps & WithStyles<CssRules>;
48 // interface SearchInputState {
53 // let selfClearPropState = '';
55 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
57 const SearchInputComponent = (props: SearchInputProps) => {
58 const [timeout, setTimeout] = useState(1);
59 const [value, setValue] = useState("");
60 const [label, setLabel] = useState("Search");
61 const [selfClearProp, setSelfClearProp] = useState("");
65 setValue(props.value);
68 setLabel(props.label);
73 clearTimeout(timeout);
75 }, [props.value, props.label]); // eslint-disable-line react-hooks/exhaustive-deps
78 if (selfClearProp !== props.selfClearProp) {
80 setSelfClearProp(props.selfClearProp);
81 handleChange({ target: { value: "" } } as any);
83 }, [props.selfClearProp]); // eslint-disable-line react-hooks/exhaustive-deps
85 const handleSubmit = (event: React.FormEvent<HTMLElement>) => {
86 event.preventDefault();
87 clearTimeout(timeout);
88 props.onSearch(value);
91 const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
92 const { target: { value: eventValue } } = event;
93 clearTimeout(timeout);
95 setTimeout(window.setTimeout(
97 props.onSearch(eventValue);
99 props.debounce || DEFAULT_SEARCH_DEBOUNCE
103 return <form onSubmit={handleSubmit}>
105 <InputLabel>{label}</InputLabel>
108 data-cy="search-input"
110 onChange={handleChange}
112 <InputAdornment position="end">
113 <Tooltip title='Search'>
115 onClick={handleSubmit}>
125 export const SearchInput = withStyles(styles)(SearchInputComponent);