1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Input as MuiInput, Chip as MuiChip, Popper as MuiPopper, Paper, FormControl, InputLabel, StyleRulesCallback, withStyles, RootRef, ListItemText, ListItem, List } from '@material-ui/core';
7 import { PopperProps } from '@material-ui/core/Popper';
8 import { WithStyles } from '@material-ui/core/styles';
9 import { noop } from 'lodash';
11 export interface AutocompleteProps<Item, Suggestion> {
15 suggestions?: Suggestion[];
16 onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
17 onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
18 onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
19 onCreate?: () => void;
20 onDelete?: (item: Item, index: number) => void;
21 onSelect?: (suggestion: Suggestion) => void;
22 renderChipValue?: (item: Item) => string;
23 renderSuggestion?: (suggestion: Suggestion) => React.ReactNode;
26 export interface AutocompleteState {
27 suggestionsOpen: boolean;
29 export class Autocomplete<Value, Suggestion> extends React.Component<AutocompleteProps<Value, Suggestion>, AutocompleteState> {
32 suggestionsOpen: false,
35 containerRef = React.createRef<HTMLDivElement>();
36 inputRef = React.createRef<HTMLInputElement>();
40 <RootRef rootRef={this.containerRef}>
41 <FormControl fullWidth>
44 {this.renderSuggestions()}
51 const { label } = this.props;
52 return label && <InputLabel>{label}</InputLabel>;
57 inputRef={this.inputRef}
58 value={this.props.value}
59 startAdornment={this.renderChips()}
60 onFocus={this.handleFocus}
61 onBlur={this.handleBlur}
62 onChange={this.props.onChange}
63 onKeyPress={this.handleKeyPress}
68 const { suggestions = [] } = this.props;
71 open={this.state.suggestionsOpen && suggestions.length > 0}
72 anchorEl={this.containerRef.current}>
73 <Paper onMouseDown={this.preventBlur}>
74 <List dense style={{ width: this.getSuggestionsWidth() }}>
76 (suggestion, index) =>
77 <ListItem button key={index} onClick={this.handleSelect(suggestion)}>
78 {this.renderSuggestion(suggestion)}
87 handleFocus = (event: React.FocusEvent<HTMLInputElement>) => {
88 const { onFocus = noop } = this.props;
89 this.setState({ suggestionsOpen: true });
93 handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
95 const { onBlur = noop } = this.props;
96 this.setState({ suggestionsOpen: false });
101 handleKeyPress = ({ key }: React.KeyboardEvent<HTMLInputElement>) => {
102 const { onCreate = noop } = this.props;
103 if (key === 'Enter' && this.props.value.length > 0) {
109 const { items, onDelete } = this.props;
113 label={this.renderChipValue(item)}
115 onDelete={() => onDelete ? onDelete(item, index) : undefined} />
119 renderChipValue(value: Value) {
120 const { renderChipValue } = this.props;
121 return renderChipValue ? renderChipValue(value) : JSON.stringify(value);
124 preventBlur = (event: React.MouseEvent<HTMLElement>) => {
125 event.preventDefault();
128 handleClickAway = (event: React.MouseEvent<HTMLElement>) => {
129 if (event.target !== this.inputRef.current) {
130 this.setState({ suggestionsOpen: false });
134 handleSelect(suggestion: Suggestion) {
136 const { onSelect = noop } = this.props;
137 const { current } = this.inputRef;
141 onSelect(suggestion);
145 renderSuggestion(suggestion: Suggestion) {
146 const { renderSuggestion } = this.props;
147 return renderSuggestion
148 ? renderSuggestion(suggestion)
149 : <ListItemText>{JSON.stringify(suggestion)}</ListItemText>;
152 getSuggestionsWidth() {
153 return this.containerRef.current ? this.containerRef.current.offsetWidth : 'auto';
157 type ChipClasses = 'root';
159 const chipStyles: StyleRulesCallback<ChipClasses> = theme => ({
161 marginRight: theme.spacing.unit / 4,
162 height: theme.spacing.unit * 3,
166 const Chip = withStyles(chipStyles)(MuiChip);
168 type PopperClasses = 'root';
170 const popperStyles: StyleRulesCallback<ChipClasses> = theme => ({
172 zIndex: theme.zIndex.modal,
176 const Popper = withStyles(popperStyles)(
177 ({ classes, ...props }: PopperProps & WithStyles<PopperClasses>) =>
178 <MuiPopper {...props} className={classes.root} />
181 type InputClasses = 'root';
183 const inputStyles: StyleRulesCallback<InputClasses> = () => ({
194 const Input = withStyles(inputStyles)(MuiInput);