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, Paper, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core';
7 import SearchIcon from '@material-ui/icons/Search';
9 interface SearchBarDataProps {
13 interface SearchBarActionProps {
14 onSearch: (value: string) => any;
18 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>
20 interface SearchBarState {
24 const DEFAULT_SEARCH_DEBOUNCE = 1000;
26 class SearchBar extends React.Component<SearchBarProps> {
28 state: SearchBarState = {
32 timeout: NodeJS.Timer;
35 const { classes } = this.props
36 return <Paper className={classes.container}>
37 <form onSubmit={this.handleSubmit}>
39 className={classes.input}
40 onChange={this.handleChange}
42 value={this.state.value}
44 <IconButton className={classes.button}>
51 componentWillReceiveProps(nextProps: SearchBarProps) {
52 if (nextProps.value !== this.props.value) {
53 this.setState({ value: nextProps.value });
57 componentWillUnmount() {
58 clearTimeout(this.timeout);
61 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
62 event.preventDefault();
63 clearTimeout(this.timeout);
64 this.props.onSearch(this.state.value);
67 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
68 clearTimeout(this.timeout);
69 this.setState({ value: event.target.value });
70 this.timeout = setTimeout(
71 () => this.props.onSearch(this.state.value),
72 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
79 type CssRules = 'container' | 'input' | 'button'
81 const styles: StyleRulesCallback<CssRules> = theme => {
82 const { unit } = theme.spacing
90 borderRadius: unit / 4,
91 boxSizing: 'border-box',
93 paddingRight: unit * 4,
106 export default withStyles(styles)(SearchBar)