1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React 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 {
39 interface SearchInputActionProps {
40 onSearch: (value: string) => any;
44 type SearchInputProps = SearchInputDataProps & SearchInputActionProps & WithStyles<CssRules>;
46 interface SearchInputState {
50 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
52 export const SearchInput = withStyles(styles)(
53 class extends React.Component<SearchInputProps> {
54 state: SearchInputState = {
61 const { classes } = this.props;
62 return <form onSubmit={this.handleSubmit}>
64 <InputLabel>Search</InputLabel>
67 value={this.state.value}
68 onChange={this.handleChange}
70 <InputAdornment position="end">
71 <Tooltip title='Search'>
73 onClick={this.handleSubmit}>
84 this.setState({ value: this.props.value });
87 componentWillReceiveProps(nextProps: SearchInputProps) {
88 if (nextProps.value !== this.props.value) {
89 this.setState({ value: nextProps.value });
93 componentWillUnmount() {
94 clearTimeout(this.timeout);
97 handleSubmit = (event: React.FormEvent<HTMLElement>) => {
98 event.preventDefault();
99 clearTimeout(this.timeout);
100 this.props.onSearch(this.state.value);
103 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
104 clearTimeout(this.timeout);
105 this.setState({ value: event.target.value });
106 this.timeout = window.setTimeout(
107 () => this.props.onSearch(this.state.value),
108 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE