Files
SCM-Manager/scm-ui/src/groups/components/AutocompleteAddEntryToTableField.js

83 lines
2.0 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { AddButton } from "@scm-manager/ui-components";
2018-11-19 10:31:01 +01:00
import Autocomplete from "../../containers/Autocomplete";
import type {
AutocompleteObject,
SelectValue
} from "../../containers/Autocomplete";
type Props = {
addEntry: SelectValue => void,
disabled: boolean,
buttonLabel: string,
fieldLabel: string,
helpText?: string,
2018-11-22 10:55:21 +01:00
loadSuggestions: string => Promise<AutocompleteObject>,
placeholder?: string,
loadingMessage?: string,
noOptionsMessage?: string
};
type State = {
selectedValue?: SelectValue
};
class AutocompleteAddEntryToTableField extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { selectedValue: undefined };
}
render() {
2018-11-22 10:55:21 +01:00
const { disabled, buttonLabel, fieldLabel, helpText, loadSuggestions, placeholder, loadingMessage, noOptionsMessage } = this.props;
const { selectedValue } = this.state;
return (
<div className="field">
2018-11-19 10:31:01 +01:00
<Autocomplete
label={fieldLabel}
2018-11-22 10:20:18 +01:00
loadSuggestions={loadSuggestions}
valueSelected={this.handleAddEntryChange}
helpText={helpText}
value={selectedValue}
2018-11-22 10:55:21 +01:00
placeholder={placeholder}
loadingMessage={loadingMessage}
noOptionsMessage={noOptionsMessage}
/>
<AddButton
label={buttonLabel}
action={this.addButtonClicked}
disabled={disabled}
/>
</div>
);
}
addButtonClicked = (event: Event) => {
event.preventDefault();
this.appendEntry();
};
appendEntry = () => {
const { selectedValue } = this.state;
if (!selectedValue) {
return;
}
// $FlowFixMe null is needed to clear the selection; undefined does not work
this.setState({ ...this.state, selectedValue: null }, () =>
this.props.addEntry(selectedValue)
);
};
handleAddEntryChange = (selection: SelectValue) => {
this.setState({
...this.state,
selectedValue: selection
});
};
}
export default AutocompleteAddEntryToTableField;