import React, { FocusEvent, MouseEvent, useState, useContext } from "react";
import classnames from "classnames";
import { useId } from "@reach/auto-id";
import { Box } from "../Box";
import { Icon, ICON_TYPE } from "../Icon";
import { Label } from "../Label";
import { Text } from "../Text";
import { Input, InputProps } from "../Input";
import { Stack, StackProps } from "../Stack";
import { ORIENTATION } from "../../types";
import { bem } from "../../utilities/bem";
import isUndefined from "../../utilities/isUndefined";
import { FormGroupContext } from "../FormGroup/FormGroupContext";
const cn = "Checkbox";
export interface CheckboxProps extends InputProps {
label?: string;
/**
* Hides required styling on Label if it is within a FormGroupContext with `isRequired` equal to true
*/
hideRequiredStyles?: boolean;
/**
* Shows required styling on Label
*/
isRequired?: boolean;
}
const CheckboxGroup = (props: StackProps) => {
const { orientation = ORIENTATION.VERTICAL, ...rest } = props;
return ;
};
export const Checkbox = ({
checked,
children,
className,
disabled = false,
id: idProp,
label,
onBlur,
onChange = () => {},
onClick,
onFocus,
hideRequiredStyles = true,
isRequired = undefined,
tabIndex,
theme: themeProp,
...rest
}: CheckboxProps) => {
const [hasFocus, setHasFocus] = useState(false);
const { isRequired: isRequiredContext, theme: themeContext } = useContext(
FormGroupContext,
);
const fallbackId = `checkbox:${useId()}`;
const id = isUndefined(idProp) ? fallbackId : idProp;
const theme = themeProp || themeContext;
const classes = classnames([
bem(cn),
{ "is-disabled": disabled },
{ "is-focused": hasFocus },
className,
]);
const handleClick = (event: MouseEvent): void => {
if (!disabled && onClick) {
onClick(event);
}
};
const handleFocus = (event: FocusEvent): void => {
setHasFocus(true);
if (onFocus) {
onFocus(event);
}
};
const handleBlur = (event: FocusEvent): void => {
setHasFocus(false);
if (onBlur) {
onBlur(event);
}
};
let calculatedRequired = false;
if (isRequired !== undefined) {
calculatedRequired = isRequired;
} else if (isRequiredContext !== undefined) {
calculatedRequired = isRequiredContext;
}
const bareCheckbox = (
{checked && (
)}
);
const checkboxWithLabel =
label || children ? (
) : (
bareCheckbox
);
return (
{checkboxWithLabel}
);
};
Checkbox.Group = CheckboxGroup;