// SPDX-License-Identifier: AGPL-3.0
import * as React from 'react';
-import { Input as MuiInput, Chip as MuiChip, Popper as MuiPopper, Paper as MuiPaper, FormControl, InputLabel, StyleRulesCallback, withStyles, RootRef, ListItemText, ListItem, List, FormHelperText } from '@material-ui/core';
+import {
+ Input as MuiInput,
+ Chip as MuiChip,
+ Popper as MuiPopper,
+ Paper as MuiPaper,
+ FormControl, InputLabel, StyleRulesCallback, withStyles, RootRef, ListItemText, ListItem, List, FormHelperText
+} from '@material-ui/core';
import { PopperProps } from '@material-ui/core/Popper';
import { WithStyles } from '@material-ui/core/styles';
import { noop } from 'lodash';
suggestions?: Suggestion[];
error?: boolean;
helperText?: string;
+ autofocus?: boolean;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
suggestionsOpen: boolean;
selectedSuggestionIndex: number;
}
+
export class Autocomplete<Value, Suggestion> extends React.Component<AutocompleteProps<Value, Suggestion>, AutocompleteState> {
state = {
renderInput() {
return <Input
+ autoFocus={this.props.autofocus}
inputRef={this.inputRef}
value={this.props.value}
startAdornment={this.renderChips()}
if (event.key === 'Enter') {
if (this.isSuggestionBoxOpen() && selectedSuggestionIndex < suggestions.length) {
// prevent form submissions when selecting a suggestion
- event.preventDefault();
+ event.preventDefault();
onSelect(suggestions[selectedSuggestionIndex]);
} else if (this.props.value.length > 0) {
onCreate();
renderChips() {
const { items, onDelete } = this.props;
+
+ /**
+ * If input startAdornment prop is not undefined, input's label will stay above the input.
+ * If there is not items, we want the label to go back to placeholder position.
+ * That why we return without a value instead of returning a result of a _map_ which is an empty array.
+ */
+ if (items.length === 0) {
+ return;
+ }
+
return items.map(
(item, index) =>
<Chip