@nex-ui/react
Version:
🎉 A beautiful, modern, and reliable React component library.
215 lines (212 loc) • 7.16 kB
JavaScript
"use client";
import { jsx, jsxs } from 'react/jsx-runtime';
import { nex } from '@nex-ui/styled';
import { useRef, isValidElement, useId } from 'react';
import { __DEV__, isString, mergeRefs, isFunction } from '@nex-ui/utils';
import { useControlledState, useFocusVisible, useEvent } from '@nex-ui/hooks';
import { useCheckboxGroup } from './CheckboxGroupContext.mjs';
import { CheckedIcon } from './CheckedIcon.mjs';
import { useNexUI } from '../provider/Context.mjs';
import { useDefaultProps } from '../utils/useDefaultProps.mjs';
import { useStyles } from '../utils/useStyles.mjs';
import { useSlotProps } from '../utils/useSlotProps.mjs';
import { Box } from '../box/Box.mjs';
import { composeClasses } from '../utils/composeClasses.mjs';
import { checkboxRecipe } from '../../theme/recipes/checkbox.mjs';
import { getUtilityClass } from '../utils/getUtilityClass.mjs';
const useSlotClasses = (ownerState)=>{
const { prefix } = useNexUI();
const checkboxRoot = `${prefix}-checkbox`;
const { radius, size, color, disabled, checked, classes } = ownerState;
const slots = {
root: [
'root',
`radius-${radius}`,
`size-${size}`,
`color-${color}`,
disabled && 'disabled',
checked && 'checked'
],
input: [
'input'
],
label: [
'label'
],
icon: [
'icon'
]
};
const composedClasses = composeClasses(slots, getUtilityClass(checkboxRoot), classes);
return composedClasses;
};
const useSlotAriaProps = (ownerState)=>{
const id = useId();
const { as, disabled, tabIndex, type, checked, children, value } = ownerState;
const childrenString = isString(children);
let input = {
disabled,
checked,
type,
tabIndex: disabled ? -1 : tabIndex
};
if (as === 'input') {
input = {
...input,
value,
'aria-labelledby': childrenString ? id : undefined,
'aria-label': childrenString ? children : undefined
};
} else {
input = {
role: 'checkbox',
tabIndex: disabled ? -1 : tabIndex,
'aria-checked': checked,
'aria-disabled': disabled || undefined,
'aria-labelledby': childrenString ? id : undefined,
'aria-label': childrenString ? children : undefined
};
}
const label = {
id: childrenString ? id : undefined
};
return {
input,
label
};
};
const Checkbox = (inProps)=>{
const { primaryThemeColor } = useNexUI();
const inputRef = useRef(null);
const props = useDefaultProps({
name: 'Checkbox',
props: inProps
});
const groupCtx = useCheckboxGroup();
const inGroup = !!groupCtx;
if (__DEV__ && inGroup) {
if ('checked' in props) {
console.warn('[Nex UI] Checkbox: The Checkbox.Group is being used, `checked` will be ignored. Use the `value` of the Checkbox.Group instead.');
}
if ('defaultChecked' in props) {
console.warn('[Nex UI] Checkbox: The Checkbox.Group is being used, `defaultChecked` will be ignored. Use the `defaultValue` of the Checkbox.Group instead.');
}
}
const { sx, ref, icon, value, className, children, slotProps, onCheckedChange, checked: checkedProp, type = 'checkbox', defaultChecked = false, as = 'input', tabIndex = 0, name = groupCtx?.name, color = groupCtx?.color ?? primaryThemeColor, disabled = groupCtx?.disabled ?? false, size = groupCtx?.size ?? 'md', radius = groupCtx?.radius ?? groupCtx?.size ?? size, ...remainingProps } = props;
const [rawChecked, setRawChecked] = useControlledState(checkedProp, defaultChecked, onCheckedChange);
const [focusVisible] = useFocusVisible({
ref: inputRef
});
const checked = inGroup ? groupCtx.isChecked(value) : rawChecked;
const ownerState = {
...props,
defaultChecked,
type,
tabIndex,
name,
disabled,
color,
checked,
size,
radius,
as,
inGroup
};
const handleChange = useEvent((event)=>{
if (inGroup && value) {
groupCtx.toggleValue(value);
}
if (!inGroup) {
setRawChecked(event.target.checked);
}
});
const handleClick = useEvent(()=>{
if (isString(as) && as !== 'input') {
if (inGroup && value) {
groupCtx.toggleValue(value);
}
if (!inGroup) {
setRawChecked(!checked);
}
}
});
const handleKeyUp = useEvent((event)=>{
// Keyboard accessibility for non interactive elements
if (focusVisible && as !== 'input' && event.code === 'Space') {
event.currentTarget.click();
}
});
const classes = useSlotClasses(ownerState);
const styles = useStyles({
name: 'Checkbox',
ownerState,
recipe: checkboxRecipe
});
const slotAriaProps = useSlotAriaProps(ownerState);
const mergedRefs = mergeRefs(ref, inputRef);
const rootProps = useSlotProps({
ownerState,
externalSlotProps: slotProps?.root,
externalForwardedProps: {
sx,
className
},
sx: styles.root,
classNames: classes.root
});
const inputProps = useSlotProps({
ownerState,
externalForwardedProps: remainingProps,
classNames: classes.input,
sx: styles.input,
additionalProps: {
as,
name,
onChange: handleChange,
onClick: handleClick,
onKeyUp: handleKeyUp,
ref: mergedRefs,
...slotAriaProps.input
}
});
const iconProps = useSlotProps({
ownerState,
externalSlotProps: slotProps?.icon,
sx: styles.icon,
classNames: classes.icon
});
const labelProps = useSlotProps({
ownerState,
externalSlotProps: slotProps?.label,
sx: styles.label,
classNames: classes.label,
additionalProps: slotAriaProps.label
});
const customIcon = icon ? isFunction(icon) ? icon(ownerState) : icon : null;
const checkedIcon = customIcon ? /*#__PURE__*/ isValidElement(customIcon) ? /*#__PURE__*/ jsx(Box, {
// @ts-ignore
as: customIcon.type,
sx: styles.checkedIcon,
...customIcon.props ?? {}
}, customIcon.key) : customIcon : /*#__PURE__*/ jsx(CheckedIcon, {
checked: checked
});
return /*#__PURE__*/ jsxs(nex.label, {
...rootProps,
children: [
/*#__PURE__*/ jsx(nex.input, {
...inputProps
}),
/*#__PURE__*/ jsx(nex.span, {
...iconProps,
children: checkedIcon
}),
children && /*#__PURE__*/ jsx(nex.span, {
...labelProps,
children: children
})
]
});
};
Checkbox.displayName = 'Checkbox';
export { Checkbox };