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, 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 SearchBarDataProps {
39 interface SearchBarActionProps {
40 onSearch: (value: string) => any;
44 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
46 interface SearchBarState {
50 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
52 export const SearchBar = withStyles(styles)(
53 class extends React.Component<SearchBarProps> {
54 state: SearchBarState = {
61 const { classes } = this.props;
62 return <Paper className={classes.container}>
63 <form onSubmit={this.handleSubmit}>
65 className={classes.input}
66 onChange={this.handleChange}
68 value={this.state.value}
70 <Tooltip title='Search'>
71 <IconButton className={classes.button}>
80 this.setState({ value: this.props.value });
83 componentWillReceiveProps(nextProps: SearchBarProps) {
84 if (nextProps.value !== this.props.value) {
85 this.setState({ value: nextProps.value });
89 componentWillUnmount() {
90 clearTimeout(this.timeout);
93 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
94 event.preventDefault();
95 clearTimeout(this.timeout);
96 this.props.onSearch(this.state.value);
99 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
100 clearTimeout(this.timeout);
101 this.setState({ value: event.target.value });
102 this.timeout = window.setTimeout(
103 () => this.props.onSearch(this.state.value),
104 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE