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

66 lines
1.5 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 } from "../../containers/Autocomplete";
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>
};
type State = {
2018-11-19 10:31:01 +01:00
entryToAdd: AutocompleteObject
};
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
label={fieldLabel}
loadSuggestions={this.props.loadSuggestions}
valueSelected={this.handleAddEntryChange}
helpText={helpText}
2018-11-19 10:31:01 +01:00
value={entryToAdd}
/>
<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;