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";
|
|
|
|
|
import type { AutocompleteObject } from "../../containers/Autocomplete";
|
2018-11-15 16:58:40 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
addEntry: string => void,
|
|
|
|
|
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-19 10:31:01 +01:00
|
|
|
entryToAdd: AutocompleteObject
|
2018-11-15 16:58:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AutocompleteAddEntryToTableField extends React.Component<Props, State> {
|
|
|
|
|
render() {
|
|
|
|
|
const { disabled, buttonLabel, fieldLabel, helpText } = this.props;
|
|
|
|
|
|
|
|
|
|
const { entryToAdd } = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="field">
|
2018-11-19 10:31:01 +01:00
|
|
|
<Autocomplete
|
2018-11-15 16:58:40 +01:00
|
|
|
label={fieldLabel}
|
|
|
|
|
loadSuggestions={this.props.loadSuggestions}
|
|
|
|
|
valueSelected={this.handleAddEntryChange}
|
|
|
|
|
helpText={helpText}
|
2018-11-19 10:31:01 +01:00
|
|
|
value={entryToAdd}
|
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 = () => {
|
|
|
|
|
const { entryToAdd } = this.state;
|
|
|
|
|
this.props.addEntry(entryToAdd.id);
|
|
|
|
|
this.setState({ ...this.state, entryToAdd: undefined });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleAddEntryChange = (selection: any) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
entryToAdd: selection
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AutocompleteAddEntryToTableField;
|