"use client"; import React from "react"; import { 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, switchAdornment?: React.ReactNode, }; /** * Simple boolean switch. * */ export const BooleanSwitchWithLabel = function BooleanSwitchWithLabel({ value, position = "end", size = "medium", invisible, onValueChange, error, label, autoFocus, disabled, className, fullWidth = true, inputClassName, switchAdornment, ...props }: BooleanSwitchWithLabelProps) { const ref = React.useRef(null); const refInput = React.useRef(null); 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) { if (value === null || value === undefined) onValueChange?.(true) else if (value) onValueChange?.(false) else onValueChange?.(null as any); } else { onValueChange?.(!value); } // refInput.current?.focus(); }} > {switchAdornment}
{label}
); };