2019-06-05 18:36:14 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import type { SelectValue } from "@scm-manager/ui-types";
|
2019-06-11 18:10:29 +02:00
|
|
|
import Autocomplete from "./Autocomplete";
|
2019-06-05 18:36:14 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-06-11 18:10:29 +02:00
|
|
|
autocompleteLink: string,
|
2019-06-12 09:27:26 +02:00
|
|
|
label: string,
|
|
|
|
|
noOptionsMessage: string,
|
|
|
|
|
loadingMessage: string,
|
|
|
|
|
placeholder: string,
|
2019-06-05 18:36:14 +02:00
|
|
|
valueSelected: SelectValue => void,
|
2019-06-12 09:27:26 +02:00
|
|
|
value?: SelectValue
|
2019-06-05 18:36:14 +02:00
|
|
|
};
|
|
|
|
|
|
2019-06-11 18:10:29 +02:00
|
|
|
class UserGroupAutocomplete extends React.Component<Props> {
|
|
|
|
|
loadSuggestions = (inputValue: string) => {
|
|
|
|
|
const url = this.props.autocompleteLink;
|
2019-06-05 18:36:14 +02:00
|
|
|
const link = url + "?q=";
|
|
|
|
|
return fetch(link + inputValue)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(json => {
|
|
|
|
|
return json.map(element => {
|
|
|
|
|
const label = element.displayName
|
|
|
|
|
? `${element.displayName} (${element.id})`
|
|
|
|
|
: element.id;
|
|
|
|
|
return {
|
|
|
|
|
value: element,
|
|
|
|
|
label
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
selectName = (selection: SelectValue) => {
|
|
|
|
|
this.props.valueSelected(selection);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-06-12 09:27:26 +02:00
|
|
|
const { autocompleteLink, label, noOptionsMessage, loadingMessage, placeholder, value } = this.props;
|
2019-06-05 18:36:14 +02:00
|
|
|
return (
|
|
|
|
|
<Autocomplete
|
2019-06-12 09:27:26 +02:00
|
|
|
autocompleteLink={autocompleteLink}
|
|
|
|
|
label={label}
|
|
|
|
|
noOptionsMessage={noOptionsMessage}
|
|
|
|
|
loadingMessage={loadingMessage}
|
|
|
|
|
placeholder={placeholder}
|
2019-06-11 18:10:29 +02:00
|
|
|
loadSuggestions={this.loadSuggestions}
|
2019-06-05 18:36:14 +02:00
|
|
|
valueSelected={this.selectName}
|
|
|
|
|
value={value}
|
|
|
|
|
creatable={true}
|
|
|
|
|
/>
|
2019-06-12 09:27:26 +02:00
|
|
|
); // {...this.props}
|
2019-06-05 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 18:10:29 +02:00
|
|
|
export default UserGroupAutocomplete;
|