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