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>
|
2023-06-26 04:21:19 +00:00
|
|
|
> = { isCreatable?: false; size?: 'sm' | 'md' } & ReactSelectProps<
|
|
|
|
Option,
|
|
|
|
IsMulti,
|
|
|
|
Group
|
|
|
|
> &
|
2022-09-21 07:10:58 +00:00
|
|
|
RefAttributes<ReactSelectType<Option, IsMulti, Group>>;
|
|
|
|
|
|
|
|
type CreatableProps<
|
|
|
|
Option = DefaultOption,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
2023-06-26 04:21:19 +00:00
|
|
|
> = { isCreatable: true; size?: 'sm' | 'md' } & ReactSelectCreatableProps<
|
|
|
|
Option,
|
|
|
|
IsMulti,
|
|
|
|
Group
|
|
|
|
>;
|
2022-09-21 07:10:58 +00:00
|
|
|
|
|
|
|
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>
|
2023-06-26 04:21:19 +00:00
|
|
|
>({
|
|
|
|
className,
|
|
|
|
isCreatable = false,
|
|
|
|
size = 'md',
|
|
|
|
...props
|
|
|
|
}: Props<Option, IsMulti, Group>) {
|
2022-09-21 07:10:58 +00:00
|
|
|
const Component = isCreatable ? ReactSelectCreatable : ReactSelect;
|
|
|
|
|
2022-03-16 06:35:32 +00:00
|
|
|
return (
|
2022-09-21 07:10:58 +00:00
|
|
|
<Component
|
2023-06-26 04:21:19 +00:00
|
|
|
className={clsx(className, 'portainer-selector-root', size)}
|
2022-09-21 07:10:58 +00:00
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|