Files
SCM-Manager/scm-ui/ui-components/src/UserGroupAutocomplete.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-06-05 18:36:14 +02:00
// @flow
import React from "react";
2019-08-15 10:51:36 +02:00
import type {SelectValue} from "@scm-manager/ui-types";
import Autocomplete from "./Autocomplete";
2019-06-05 18:36:14 +02:00
2019-06-18 15:20:44 +02:00
export type AutocompleteProps = {
autocompleteLink: string,
2019-06-18 15:20:44 +02:00
valueSelected: SelectValue => void,
value?: SelectValue
};
type Props = AutocompleteProps & {
2019-06-12 09:27:26 +02:00
label: string,
noOptionsMessage: string,
loadingMessage: string,
2019-06-18 15:20:44 +02:00
placeholder: string
2019-06-05 18:36:14 +02:00
};
2019-06-18 15:20:44 +02:00
export default 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() {
return (
<Autocomplete
loadSuggestions={this.loadSuggestions}
2019-06-05 18:36:14 +02:00
valueSelected={this.selectName}
creatable={true}
2019-06-18 15:20:44 +02:00
{...this.props}
2019-06-05 18:36:14 +02:00
/>
2019-06-18 15:20:44 +02:00
);
2019-06-05 18:36:14 +02:00
}
}