2018-11-14 13:40:14 +01:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import AsyncSelect from "react-select/lib/Async";
|
2018-11-19 10:31:01 +01:00
|
|
|
import { LabelWithHelpIcon } from "@scm-manager/ui-components";
|
2018-11-14 13:40:14 +01:00
|
|
|
|
2018-11-19 10:31:01 +01:00
|
|
|
export type AutocompleteObject = {
|
2018-11-14 13:40:14 +01:00
|
|
|
id: string,
|
|
|
|
|
displayName: string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type SelectValue = {
|
2018-11-19 10:31:01 +01:00
|
|
|
value: AutocompleteObject,
|
2018-11-14 13:40:14 +01:00
|
|
|
label: string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = {
|
2018-11-19 10:31:01 +01:00
|
|
|
loadSuggestions: string => Promise<AutocompleteObject>,
|
|
|
|
|
valueSelected: AutocompleteObject => void,
|
2018-11-15 16:58:40 +01:00
|
|
|
label: string,
|
|
|
|
|
helpText?: string,
|
2018-11-19 10:31:01 +01:00
|
|
|
value?: AutocompleteObject
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2018-11-19 10:31:01 +01:00
|
|
|
value: AutocompleteObject
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
2018-11-19 10:31:01 +01:00
|
|
|
class Autocomplete extends React.Component<Props, State> {
|
2018-11-14 13:40:14 +01:00
|
|
|
handleInputChange = (newValue: SelectValue) => {
|
|
|
|
|
this.setState({ value: newValue.value });
|
2018-11-15 16:58:40 +01:00
|
|
|
this.props.valueSelected(newValue.value);
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-11-15 16:58:40 +01:00
|
|
|
const { label, helpText, value } = this.props;
|
|
|
|
|
const stringValue = value ? value.id : "";
|
2018-11-14 13:40:14 +01:00
|
|
|
return (
|
2018-11-15 16:58:40 +01:00
|
|
|
<div className="field">
|
|
|
|
|
<LabelWithHelpIcon label={label} helpText={helpText} />
|
|
|
|
|
<div className="control">
|
|
|
|
|
<AsyncSelect
|
|
|
|
|
cacheOptions
|
|
|
|
|
defaultOptions
|
|
|
|
|
loadOptions={this.props.loadSuggestions}
|
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
|
value={stringValue}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2018-11-14 13:40:14 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 10:31:01 +01:00
|
|
|
export default Autocomplete;
|