2021-12-14 21:14:53 +02:00
|
|
|
import clsx from 'clsx';
|
2023-09-03 10:26:38 +01:00
|
|
|
import uuid from 'uuid';
|
2023-09-05 18:06:36 +02:00
|
|
|
import { ComponentProps, PropsWithChildren, ReactNode } from 'react';
|
2021-12-14 21:14:53 +02:00
|
|
|
|
2022-11-13 10:10:18 +02:00
|
|
|
import { FeatureId } from '@/react/portainer/feature-flags/enums';
|
2022-06-17 19:18:42 +03:00
|
|
|
|
|
|
|
import { Tooltip } from '@@/Tip/Tooltip';
|
2021-12-14 21:14:53 +02:00
|
|
|
|
|
|
|
import styles from './SwitchField.module.css';
|
|
|
|
import { Switch } from './Switch';
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
label: string;
|
|
|
|
checked: boolean;
|
2023-05-25 11:59:32 +08:00
|
|
|
onChange(value: boolean, index?: number): void;
|
2021-12-14 21:14:53 +02:00
|
|
|
|
2023-05-25 11:59:32 +08:00
|
|
|
index?: number;
|
2021-12-14 21:14:53 +02:00
|
|
|
name?: string;
|
2023-02-23 01:43:33 +05:30
|
|
|
tooltip?: ComponentProps<typeof Tooltip>['message'];
|
|
|
|
setTooltipHtmlMessage?: ComponentProps<typeof Tooltip>['setHtmlMessage'];
|
2021-12-14 21:14:53 +02:00
|
|
|
labelClass?: string;
|
2022-07-22 14:16:50 +12:00
|
|
|
switchClass?: string;
|
2022-07-27 13:05:25 +12:00
|
|
|
fieldClass?: string;
|
2021-12-14 21:14:53 +02:00
|
|
|
dataCy?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
featureId?: FeatureId;
|
2023-09-05 18:06:36 +02:00
|
|
|
valueExplanation?: ReactNode;
|
2021-12-14 21:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function SwitchField({
|
|
|
|
tooltip,
|
|
|
|
checked,
|
|
|
|
label,
|
2023-05-25 11:59:32 +08:00
|
|
|
index,
|
2023-09-03 10:26:38 +01:00
|
|
|
name = uuid(),
|
2021-12-14 21:14:53 +02:00
|
|
|
labelClass,
|
2022-07-27 13:05:25 +12:00
|
|
|
fieldClass,
|
2021-12-14 21:14:53 +02:00
|
|
|
dataCy,
|
|
|
|
disabled,
|
|
|
|
onChange,
|
|
|
|
featureId,
|
2022-07-22 14:16:50 +12:00
|
|
|
switchClass,
|
2022-12-09 10:41:11 +13:00
|
|
|
setTooltipHtmlMessage,
|
2023-09-05 18:06:36 +02:00
|
|
|
valueExplanation,
|
|
|
|
}: PropsWithChildren<Props>) {
|
2021-12-14 21:14:53 +02:00
|
|
|
const toggleName = name ? `toggle_${name}` : '';
|
|
|
|
|
|
|
|
return (
|
2023-09-03 10:26:38 +01:00
|
|
|
<div className={clsx(styles.root, fieldClass)}>
|
|
|
|
<label
|
2022-12-13 22:56:47 +02:00
|
|
|
className={clsx('space-right control-label !p-0 text-left', labelClass)}
|
2023-09-03 10:26:38 +01:00
|
|
|
htmlFor={toggleName}
|
2021-12-14 21:14:53 +02:00
|
|
|
>
|
|
|
|
{label}
|
2022-12-09 10:41:11 +13:00
|
|
|
{tooltip && (
|
|
|
|
<Tooltip message={tooltip} setHtmlMessage={setTooltipHtmlMessage} />
|
|
|
|
)}
|
2023-09-03 10:26:38 +01:00
|
|
|
</label>
|
2021-12-14 21:14:53 +02:00
|
|
|
<Switch
|
2022-07-22 14:16:50 +12:00
|
|
|
className={clsx('space-right', switchClass)}
|
2021-12-14 21:14:53 +02:00
|
|
|
name={toggleName}
|
|
|
|
id={toggleName}
|
|
|
|
checked={checked}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={onChange}
|
2023-05-25 11:59:32 +08:00
|
|
|
index={index}
|
2021-12-14 21:14:53 +02:00
|
|
|
featureId={featureId}
|
|
|
|
dataCy={dataCy}
|
|
|
|
/>
|
2023-09-05 18:06:36 +02:00
|
|
|
{valueExplanation && <span>{valueExplanation}</span>}
|
2023-09-03 10:26:38 +01:00
|
|
|
</div>
|
2021-12-14 21:14:53 +02:00
|
|
|
);
|
|
|
|
}
|