2022-09-21 07:10:58 +00:00
|
|
|
import ReactSelectCreatable, {
|
|
|
|
CreatableProps as ReactSelectCreatableProps,
|
|
|
|
} from 'react-select/creatable';
|
|
|
|
import ReactSelect, {
|
|
|
|
GroupBase,
|
|
|
|
Props as ReactSelectProps,
|
|
|
|
} from 'react-select';
|
2022-03-16 06:35:32 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import { RefAttributes } from 'react';
|
|
|
|
import ReactSelectType from 'react-select/dist/declarations/src/Select';
|
|
|
|
|
2022-09-21 07:10:58 +00:00
|
|
|
import './ReactSelect.css';
|
|
|
|
|
|
|
|
interface DefaultOption {
|
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegularProps<
|
|
|
|
Option = DefaultOption,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
> = { isCreatable?: false } & ReactSelectProps<Option, IsMulti, Group> &
|
|
|
|
RefAttributes<ReactSelectType<Option, IsMulti, Group>>;
|
|
|
|
|
|
|
|
type CreatableProps<
|
|
|
|
Option = DefaultOption,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
> = { isCreatable: true } & ReactSelectCreatableProps<Option, IsMulti, Group>;
|
|
|
|
|
|
|
|
type Props<
|
|
|
|
Option = DefaultOption,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
> =
|
|
|
|
| CreatableProps<Option, IsMulti, Group>
|
|
|
|
| RegularProps<Option, IsMulti, Group>;
|
2022-03-16 06:35:32 +00:00
|
|
|
|
|
|
|
export function Select<
|
2022-09-21 07:10:58 +00:00
|
|
|
Option = DefaultOption,
|
2022-03-16 06:35:32 +00:00
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
2022-09-21 07:10:58 +00:00
|
|
|
>({ className, isCreatable = false, ...props }: Props<Option, IsMulti, Group>) {
|
|
|
|
const Component = isCreatable ? ReactSelectCreatable : ReactSelect;
|
|
|
|
|
2022-03-16 06:35:32 +00:00
|
|
|
return (
|
2022-09-21 07:10:58 +00:00
|
|
|
<Component
|
|
|
|
className={clsx(className, 'portainer-selector-root')}
|
|
|
|
classNamePrefix="portainer-selector"
|
2022-03-16 06:35:32 +00:00
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2022-05-23 14:32:51 +00:00
|
|
|
|
|
|
|
export function Creatable<
|
2022-09-21 07:10:58 +00:00
|
|
|
Option = DefaultOption,
|
2022-05-23 14:32:51 +00:00
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
2022-09-21 07:10:58 +00:00
|
|
|
>({ className, ...props }: ReactSelectCreatableProps<Option, IsMulti, Group>) {
|
2022-05-23 14:32:51 +00:00
|
|
|
return (
|
|
|
|
<ReactSelectCreatable
|
2022-09-21 07:10:58 +00:00
|
|
|
className={clsx(className, 'portainer-selector-root')}
|
|
|
|
classNamePrefix="portainer-selector"
|
2022-05-23 14:32:51 +00:00
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|