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 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
26 class SearchBar extends React.Component<SearchBarProps> {
28 state: SearchBarState = {
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}>
52 this.setState({value: this.props.value});
55 componentWillReceiveProps(nextProps: SearchBarProps) {
56 if (nextProps.value !== this.props.value) {
57 this.setState({ value: nextProps.value });
61 componentWillUnmount() {
62 clearTimeout(this.timeout);
65 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
66 event.preventDefault();
67 clearTimeout(this.timeout);
68 this.props.onSearch(this.state.value);
71 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
72 clearTimeout(this.timeout);
73 this.setState({ value: event.target.value });
74 this.timeout = window.setTimeout(
75 () => this.props.onSearch(this.state.value),
76 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
83 type CssRules = 'container' | 'input' | 'button';
85 const styles: StyleRulesCallback<CssRules> = theme => {
93 borderRadius: theme.spacing.unit / 4,
94 boxSizing: 'border-box',
95 padding: theme.spacing.unit,
96 paddingRight: theme.spacing.unit * 4,
100 position: 'absolute',
101 top: theme.spacing.unit / 2,
102 right: theme.spacing.unit / 2,
103 width: theme.spacing.unit * 3,
104 height: theme.spacing.unit * 3
109 export default withStyles(styles)(SearchBar);