"use client"; import React from "react"; import { controlHeightMixin, fieldBackgroundDisabledMixin, fieldBackgroundHoverMixin, fieldBackgroundMixin, focusedClasses } from "../styles"; import { BooleanSwitch, BooleanSwitchProps } from "./BooleanSwitch"; import { cls } from "../util"; export type BooleanSwitchWithLabelProps = BooleanSwitchProps & { position?: "start" | "end", invisible?: boolean, label?: React.ReactNode, error?: boolean, autoFocus?: boolean, fullWidth?: boolean, className?: string, inputClassName?: string, }; /** * Simple boolean switch. * */ export const BooleanSwitchWithLabel = function BooleanSwitchWithLabel({ value, position = "end", size = "medium", invisible, onValueChange, error, label, autoFocus, disabled, className, fullWidth = true, inputClassName, ...props }: BooleanSwitchWithLabelProps) { const ref = React.useRef(null); const refInput = React.useRef(null); const switchLabelId = React.useId(); const [_, setFocused] = React.useState(autoFocus) const onFocus = () => setFocused(true); const onBlur = () => setFocused(false); React.useEffect(() => { if (autoFocus) { // refInput.current?.focus(); } }, []); const focus = document.activeElement === refInput?.current || document.activeElement === ref?.current return (
{ if (props.allowIndeterminate) { const onChange = onValueChange as ((newValue: boolean | null) => void) | undefined; if (value === null || value === undefined) onChange?.(true) else if (value) onChange?.(false) else onChange?.(null); } else { onValueChange?.(!value); } // refInput.current?.focus(); }} >
{label}
); };