checkbox ui component for react
npx @tessl/cli install tessl/npm-rc-checkbox@3.5.0RC Checkbox is a flexible, customizable checkbox UI component for React applications. It provides a comprehensive checkbox implementation with support for controlled and uncontrolled modes, custom styling, event handling, and accessibility features. The component offers three-state logic support (unchecked, checked, indeterminate) and can also function as a radio button.
npm install rc-checkboximport Checkbox from "rc-checkbox";For TypeScript types:
import Checkbox, { type CheckboxProps, type CheckboxRef, type CheckboxChangeEvent } from "rc-checkbox";CommonJS:
const Checkbox = require("rc-checkbox");import React, { useState } from "react";
import Checkbox from "rc-checkbox";
function App() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>
);
}The primary checkbox component with full TypeScript support and React forwardRef capabilities.
const Checkbox: React.ForwardRefExoticComponent<
CheckboxProps & React.RefAttributes<CheckboxRef>
>;Configuration interface for the checkbox component, extending HTML input attributes.
interface CheckboxProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
/** CSS class prefix for styling (default: 'rc-checkbox') */
prefixCls?: string;
/** Custom change event handler with enhanced event object */
onChange?: (e: CheckboxChangeEvent) => void;
}Custom event object providing additional control over event behavior.
interface CheckboxChangeEvent {
/** Enhanced target object with checkbox props and state */
target: CheckboxChangeEventTarget;
/** Stop event bubbling to parent elements */
stopPropagation: () => void;
/** Prevent default browser behavior */
preventDefault: () => void;
/** Original native change event */
nativeEvent: React.ChangeEvent<HTMLInputElement>['nativeEvent'];
}
interface CheckboxChangeEventTarget extends CheckboxProps {
/** Current checked state */
checked: boolean;
}Ref interface for programmatic control of the checkbox.
interface CheckboxRef {
/** Focus the checkbox input with optional focus options */
focus: (options?: FocusOptions) => void;
/** Remove focus from the checkbox input */
blur: () => void;
/** Direct access to the underlying input element */
input: HTMLInputElement | null;
/** Access to the wrapper span element */
nativeElement: HTMLElement | null;
}prefixCls?: string - CSS class prefix for custom styling (default: 'rc-checkbox')onChange?: (e: CheckboxChangeEvent) => void - Enhanced change event handlerchecked?: boolean - Controlled checked statedefaultChecked?: boolean - Initial checked state for uncontrolled mode (default: false)disabled?: boolean - Disable the checkboxname?: string - Form field namevalue?: string | number - Form field valueclassName?: string - Additional CSS classesstyle?: React.CSSProperties - Inline stylestitle?: string - Tooltip text displayed on wrapper elementtype?: string - Input type (default: 'checkbox', can be 'radio')autoFocus?: boolean - Auto focus on component mountrequired?: boolean - Required field validationtabIndex?: number - Tab navigation orderid?: string - Unique element identifieronFocus?: React.FocusEventHandler<HTMLInputElement> - Focus event handleronBlur?: React.FocusEventHandler<HTMLInputElement> - Blur event handleronKeyDown?: React.KeyboardEventHandler<HTMLInputElement> - Key down event handleronKeyPress?: React.KeyboardEventHandler<HTMLInputElement> - Key press event handleronKeyUp?: React.KeyboardEventHandler<HTMLInputElement> - Key up event handlerdata-* attributes - Custom data attributes passed to inputaria-* attributes - Accessibility attributes passed to inputrole?: string - ARIA role attributeimport React, { useState } from "react";
import Checkbox from "rc-checkbox";
function ControlledCheckbox() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
checked={checked}
onChange={(e) => {
setChecked(e.target.checked);
console.log('Checkbox is now:', e.target.checked);
}}
/>
);
}import React from "react";
import Checkbox from "rc-checkbox";
function UncontrolledCheckbox() {
return (
<Checkbox
defaultChecked={false}
onChange={(e) => {
console.log('Checkbox changed to:', e.target.checked);
}}
/>
);
}import React from "react";
import Checkbox from "rc-checkbox";
import "rc-checkbox/assets/index.css"; // Default styles
function StyledCheckbox() {
return (
<Checkbox
prefixCls="my-checkbox"
className="custom-checkbox"
style={{ margin: '10px' }}
onChange={(e) => console.log(e.target.checked)}
/>
);
}import React, { useState } from "react";
import Checkbox from "rc-checkbox";
function RadioGroup() {
const [selected, setSelected] = useState('option1');
return (
<div>
<Checkbox
type="radio"
name="options"
value="option1"
checked={selected === 'option1'}
onChange={(e) => setSelected(e.target.value)}
/>
<Checkbox
type="radio"
name="options"
value="option2"
checked={selected === 'option2'}
onChange={(e) => setSelected(e.target.value)}
/>
</div>
);
}import React, { useRef } from "react";
import Checkbox, { type CheckboxRef } from "rc-checkbox";
function RefExample() {
const checkboxRef = useRef<CheckboxRef>(null);
const focusCheckbox = () => {
checkboxRef.current?.focus();
};
const blurCheckbox = () => {
checkboxRef.current?.blur();
};
return (
<div>
<Checkbox ref={checkboxRef} />
<button onClick={focusCheckbox}>Focus Checkbox</button>
<button onClick={blurCheckbox}>Blur Checkbox</button>
</div>
);
}import React from "react";
import Checkbox from "rc-checkbox";
function AdvancedEventHandling() {
const handleChange = (e) => {
// Prevent the change from bubbling up
e.stopPropagation();
// Access all checkbox props and state
console.log('Target props:', e.target);
console.log('Checked state:', e.target.checked);
console.log('Native event:', e.nativeEvent);
};
return (
<div onClick={() => console.log('Div clicked')}>
<Checkbox onChange={handleChange} />
</div>
);
}The component generates the following CSS classes for styling:
rc-checkbox - Base wrapper class (or custom prefixCls)rc-checkbox-checked - Applied when checked state is truerc-checkbox-disabled - Applied when disabled prop is truerc-checkbox-input - Applied to the input elementrc-checkbox-inner - Applied to the inner visual elementuseMergedState hook