2026-01-23 13:20:56 +01:00
|
|
|
import type { ComponentPropsWithoutRef, CSSProperties } from 'react';
|
|
|
|
|
import { forwardRef } from 'react';
|
|
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
2026-01-30 15:44:28 +01:00
|
|
|
import type { CommonFieldWrapperProps } from './form_field_wrapper';
|
|
|
|
|
import { FormFieldWrapper } from './form_field_wrapper';
|
2026-01-23 13:20:56 +01:00
|
|
|
import classes from './toggle.module.css';
|
|
|
|
|
|
|
|
|
|
type Props = Omit<ComponentPropsWithoutRef<'input'>, 'type'> & {
|
|
|
|
|
size?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ToggleField = forwardRef<
|
|
|
|
|
HTMLInputElement,
|
|
|
|
|
Props & CommonFieldWrapperProps
|
2026-03-13 14:55:17 +01:00
|
|
|
>(({ id, label, hint, status, required, ...otherProps }, ref) => (
|
2026-01-23 13:20:56 +01:00
|
|
|
<FormFieldWrapper
|
|
|
|
|
label={label}
|
|
|
|
|
hint={hint}
|
|
|
|
|
required={required}
|
2026-03-13 14:55:17 +01:00
|
|
|
status={status}
|
2026-01-23 13:20:56 +01:00
|
|
|
inputId={id}
|
2026-01-30 15:44:28 +01:00
|
|
|
inputPlacement='inline-end'
|
2026-01-23 13:20:56 +01:00
|
|
|
>
|
2026-01-30 15:44:28 +01:00
|
|
|
{(inputProps) => <Toggle {...otherProps} {...inputProps} ref={ref} />}
|
2026-01-23 13:20:56 +01:00
|
|
|
</FormFieldWrapper>
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
ToggleField.displayName = 'ToggleField';
|
|
|
|
|
|
2026-01-30 15:44:28 +01:00
|
|
|
export const Toggle = forwardRef<HTMLInputElement, Props>(
|
2026-01-23 13:20:56 +01:00
|
|
|
({ className, size, ...otherProps }, ref) => (
|
2026-01-28 11:17:32 +01:00
|
|
|
<span className={classes.wrapper}>
|
2026-01-23 13:20:56 +01:00
|
|
|
<input
|
|
|
|
|
{...otherProps}
|
|
|
|
|
type='checkbox'
|
|
|
|
|
className={classes.input}
|
|
|
|
|
ref={ref}
|
|
|
|
|
/>
|
|
|
|
|
<span
|
|
|
|
|
className={classNames(classes.toggle, className)}
|
|
|
|
|
style={
|
|
|
|
|
{ '--diameter': size ? `${size}px` : undefined } as CSSProperties
|
|
|
|
|
}
|
|
|
|
|
hidden
|
|
|
|
|
/>
|
2026-01-28 11:17:32 +01:00
|
|
|
</span>
|
2026-01-23 13:20:56 +01:00
|
|
|
),
|
|
|
|
|
);
|
2026-01-30 15:44:28 +01:00
|
|
|
Toggle.displayName = 'Toggle';
|