Extracted promise-logic and created AutocompleteAddEntryToTableField

This commit is contained in:
Philipp Czora
2018-11-15 16:58:40 +01:00
parent 639b483d92
commit 921448145a
4 changed files with 122 additions and 44 deletions

View File

@@ -0,0 +1,71 @@
//@flow
import React from "react";
import { AddButton } from "@scm-manager/ui-components";
import AsyncAutocomplete from "../../containers/AsyncAutocomplete";
type Props = {
addEntry: string => void,
disabled: boolean,
buttonLabel: string,
fieldLabel: string,
helpText?: string,
loadSuggestions: string => any //TODO: type
};
type State = {
entryToAdd: any // TODO: type
};
class AutocompleteAddEntryToTableField extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
entryToAdd: undefined
};
}
render() {
const { disabled, buttonLabel, fieldLabel, helpText } = this.props;
const { entryToAdd } = this.state;
return (
<div className="field">
<AsyncAutocomplete
label={fieldLabel}
loadSuggestions={this.props.loadSuggestions}
valueSelected={this.handleAddEntryChange}
helpText={helpText}
value={entryToAdd ? entryToAdd.id : ""}
/>
<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;