Files
SCM-Manager/scm-ui/src/containers/AsyncAutocomplete.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-11-14 13:40:14 +01:00
// @flow
import React from "react";
import AsyncSelect from "react-select/lib/Async";
import {LabelWithHelpIcon} from "@scm-manager/ui-components";
2018-11-14 13:40:14 +01:00
type SelectionResult = {
id: string,
displayName: string
};
type SelectValue = {
value: SelectionResult,
label: string
};
type Props = {
loadSuggestions: string => Promise<SelectionResult>,
valueSelected: SelectionResult => void,
label: string,
helpText?: string,
value?: any
2018-11-14 13:40:14 +01:00
};
type State = {
value: SelectionResult
};
class AsyncAutocomplete extends React.Component<Props, State> {
handleInputChange = (newValue: SelectValue) => {
this.setState({ value: newValue.value });
this.props.valueSelected(newValue.value);
2018-11-14 13:40:14 +01:00
};
render() {
const { label, helpText, value } = this.props;
const stringValue = value ? value.id : "";
2018-11-14 13:40:14 +01:00
return (
<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
);
}
}
export default AsyncAutocomplete;