2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
2019-11-26 15:59:38 +01:00
|
|
|
import classNames from "classnames";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { Async, AsyncCreatable } from "react-select";
|
|
|
|
|
import { SelectValue } from "@scm-manager/ui-types";
|
|
|
|
|
import LabelWithHelpIcon from "./forms/LabelWithHelpIcon";
|
|
|
|
|
import { ActionMeta, ValueType } from "react-select/lib/types";
|
2018-11-14 13:40:14 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-20 16:59:02 +02:00
|
|
|
loadSuggestions: (p: string) => Promise<SelectValue[]>;
|
2019-10-19 16:38:07 +02:00
|
|
|
valueSelected: (p: SelectValue) => void;
|
2019-12-05 10:29:39 +01:00
|
|
|
label?: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
helpText?: string;
|
|
|
|
|
value?: SelectValue;
|
|
|
|
|
placeholder: string;
|
|
|
|
|
loadingMessage: string;
|
|
|
|
|
noOptionsMessage: string;
|
|
|
|
|
creatable?: boolean;
|
2019-11-26 15:59:38 +01:00
|
|
|
className?: string;
|
2018-11-14 13:40:14 +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 = {
|
2019-10-20 16:59:02 +02:00
|
|
|
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
|
|
|
|
2019-10-21 10:57:56 +02:00
|
|
|
handleInputChange = (newValue: ValueType<SelectValue>, action: ActionMeta) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
this.selectValue(newValue as SelectValue);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
selectValue = (value: SelectValue) => {
|
|
|
|
|
this.props.valueSelected(value);
|
2018-11-21 18:08:21 +01:00
|
|
|
};
|
|
|
|
|
|
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)
|
2019-06-12 09:47:13 +02:00
|
|
|
isValidNewOption = (
|
|
|
|
|
inputValue: string,
|
2019-10-20 16:59:02 +02:00
|
|
|
selectValue: ValueType<SelectValue>,
|
|
|
|
|
selectOptions: readonly SelectValue[]
|
|
|
|
|
): boolean => {
|
2019-10-21 10:57:56 +02:00
|
|
|
const isNotDuplicated = !selectOptions.map(option => option.label).includes(inputValue);
|
2019-10-20 16:59:02 +02:00
|
|
|
const isNotEmpty = inputValue !== "";
|
2018-11-21 18:08:21 +01:00
|
|
|
return isNotEmpty && isNotDuplicated;
|
2018-11-14 13:40:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-06-12 09:47:13 +02:00
|
|
|
const {
|
|
|
|
|
label,
|
|
|
|
|
helpText,
|
|
|
|
|
value,
|
|
|
|
|
placeholder,
|
|
|
|
|
loadingMessage,
|
|
|
|
|
noOptionsMessage,
|
|
|
|
|
loadSuggestions,
|
2019-11-26 15:59:38 +01:00
|
|
|
creatable,
|
|
|
|
|
className
|
2019-06-12 09:47:13 +02:00
|
|
|
} = this.props;
|
2018-11-14 13:40:14 +01:00
|
|
|
return (
|
2019-11-26 15:59:38 +01:00
|
|
|
<div className={classNames("field", className)}>
|
2018-11-15 16:58:40 +01:00
|
|
|
<LabelWithHelpIcon label={label} helpText={helpText} />
|
|
|
|
|
<div className="control">
|
2019-06-12 09:47:13 +02:00
|
|
|
{creatable ? (
|
2019-03-27 10:12:05 +01:00
|
|
|
<AsyncCreatable
|
|
|
|
|
cacheOptions
|
|
|
|
|
loadOptions={loadSuggestions}
|
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
|
value={value}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
loadingMessage={() => loadingMessage}
|
|
|
|
|
noOptionsMessage={() => noOptionsMessage}
|
|
|
|
|
isValidNewOption={this.isValidNewOption}
|
|
|
|
|
onCreateOption={value => {
|
2019-10-20 16:59:02 +02:00
|
|
|
this.selectValue({
|
2019-03-27 10:12:05 +01:00
|
|
|
label: value,
|
2019-10-19 16:38:07 +02:00
|
|
|
value: {
|
|
|
|
|
id: value,
|
2019-10-20 16:59:02 +02:00
|
|
|
displayName: value
|
|
|
|
|
}
|
2019-03-27 10:12:05 +01:00
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2019-06-12 09:47:13 +02:00
|
|
|
) : (
|
2019-03-27 10:12:05 +01:00
|
|
|
<Async
|
|
|
|
|
cacheOptions
|
|
|
|
|
loadOptions={loadSuggestions}
|
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
|
value={value}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
loadingMessage={() => loadingMessage}
|
|
|
|
|
noOptionsMessage={() => noOptionsMessage}
|
|
|
|
|
/>
|
2019-06-12 09:47:13 +02:00
|
|
|
)}
|
2018-11-15 16:58:40 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2018-11-14 13:40:14 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 10:31:01 +01:00
|
|
|
export default Autocomplete;
|