2018-11-14 13:40:14 +01:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
2018-11-19 10:31:01 +01:00
|
|
|
import { LabelWithHelpIcon } from "@scm-manager/ui-components";
|
2018-11-21 18:08:21 +01:00
|
|
|
import { AsyncCreatable } from "react-select";
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-21 18:08:21 +01:00
|
|
|
export 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>,
|
2018-11-21 18:08:21 +01:00
|
|
|
valueSelected: SelectValue => void,
|
2018-11-15 16:58:40 +01:00
|
|
|
label: string,
|
|
|
|
|
helpText?: string,
|
2018-11-22 10:18:13 +01:00
|
|
|
value?: SelectValue,
|
|
|
|
|
placeholder: string,
|
|
|
|
|
loadingMessage: string,
|
|
|
|
|
noOptionsMessage: string
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
2018-11-22 10:18:13 +01:00
|
|
|
|
2018-11-19 16:10:36 +01:00
|
|
|
type State = {};
|
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-22 10:18:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
placeholder: "Type here",
|
|
|
|
|
loadingMessage: "Loading...",
|
|
|
|
|
noOptionsMessage: "No suggestion available"
|
2018-11-22 10:55:21 +01:00
|
|
|
};
|
2018-11-22 10:18:13 +01:00
|
|
|
|
2018-11-14 13:40:14 +01:00
|
|
|
handleInputChange = (newValue: SelectValue) => {
|
2018-11-21 18:08:21 +01:00
|
|
|
this.props.valueSelected(newValue);
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-22 10:18:13 +01:00
|
|
|
// We overwrite this to avoid running into a bug (https://github.com/JedWatson/react-select/issues/2944)
|
|
|
|
|
isValidNewOption = (inputValue: string, selectValue: SelectValue, selectOptions: SelectValue[]) => {
|
2018-11-21 18:08:21 +01:00
|
|
|
const isNotDuplicated = !selectOptions
|
|
|
|
|
.map(option => option.label)
|
|
|
|
|
.includes(inputValue);
|
|
|
|
|
const isNotEmpty = inputValue !== "";
|
|
|
|
|
return isNotEmpty && isNotDuplicated;
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-11-22 10:18:13 +01:00
|
|
|
const { label, helpText, value, placeholder, loadingMessage, noOptionsMessage, loadSuggestions } = this.props;
|
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">
|
2018-11-21 18:08:21 +01:00
|
|
|
<AsyncCreatable
|
2018-11-15 16:58:40 +01:00
|
|
|
cacheOptions
|
2018-11-22 10:18:13 +01:00
|
|
|
loadOptions={loadSuggestions}
|
2018-11-15 16:58:40 +01:00
|
|
|
onChange={this.handleInputChange}
|
2018-11-21 18:08:21 +01:00
|
|
|
value={value}
|
2018-11-22 10:18:13 +01:00
|
|
|
placeholder={placeholder}
|
|
|
|
|
loadingMessage={() => loadingMessage}
|
|
|
|
|
noOptionsMessage={() => noOptionsMessage}
|
2018-11-21 18:08:21 +01:00
|
|
|
isValidNewOption={this.isValidNewOption}
|
|
|
|
|
onCreateOption={value => {
|
|
|
|
|
this.handleInputChange({
|
|
|
|
|
label: value,
|
|
|
|
|
value: { id: value, displayName: value }
|
|
|
|
|
});
|
|
|
|
|
}}
|
2018-11-15 16:58:40 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2018-11-14 13:40:14 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 10:18:13 +01:00
|
|
|
|
2018-11-19 10:31:01 +01:00
|
|
|
export default Autocomplete;
|