// This file is part of Shadcn Admin Kit (https://github.com/marmelab/shadcn-admin-kit)
/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useCallback } from "react";
import { Switch } from "@/components/ui/switch";
import { FormError, FormField, FormLabel } from "@/components/admin/form";
import { useInput, FieldTitle } from "ra-core";
import { InputHelperText } from "./input-helper-text";
export const BooleanInput = (props: BooleanInputProps) => {
const {
className,
defaultValue = false,
format,
label,
helperText,
onBlur,
onChange,
onFocus,
readOnly,
disabled,
parse,
resource,
source,
validate,
...rest
} = props;
const { id, field, isRequired } = useInput({
defaultValue,
format,
parse,
resource,
source,
onBlur,
onChange,
type: "checkbox",
validate,
disabled,
readOnly,
...rest,
});
const handleChange = useCallback(
(checked: boolean) => {
field.onChange(checked);
// Ensure field is considered as touched
field.onBlur();
},
[field],
);
return (
);
};
export interface BooleanInputProps {
className?: string;
defaultValue?: boolean;
format?: (value: any) => any;
helperText?: React.ReactNode;
label?: React.ReactNode;
onBlur?: (event: React.FocusEvent) => void;
onChange?: (value: any) => void;
onFocus?: (event: React.FocusEvent) => void;
readOnly?: boolean;
disabled?: boolean;
parse?: (value: any) => any;
resource?: string;
source: string;
validate?: any;
}