@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
97 lines (93 loc) • 3.37 kB
JavaScript
"use client";
import { createContext as createContext$1 } from "../../utils/context.js";
import { visuallyHiddenAttributes } from "../../utils/dom.js";
import { mergeRefs } from "../../utils/ref.js";
import { utils_exports } from "../../utils/index.js";
import { useControllableState } from "../../hooks/use-controllable-state/index.js";
import { useFieldProps } from "../field/use-field-props.js";
import { useCallback } from "react";
//#region src/components/checkbox/use-checkbox-group.ts
const [CheckboxGroupContext, useCheckboxGroupContext] = createContext$1({
name: "CheckboxGroupContext",
strict: false
});
const useCheckboxGroup = (props = {}) => {
const { context: { labelId } = {}, props: { defaultValue = [], disabled, max, readOnly, required, value: valueProp, onChange: onChangeProp,...rest }, ariaProps: { "aria-describedby": ariaDescribedbyProp,...ariaProps }, dataProps, eventProps } = useFieldProps(props);
const interactive = !(readOnly || disabled);
const [value, setValue] = useControllableState({
defaultValue,
value: valueProp,
onChange: onChangeProp
});
const onChange = useCallback((valueOrEv) => {
if (!interactive) return;
if ((0, utils_exports.isObject)(valueOrEv)) valueOrEv = valueOrEv.target.value;
setValue((prev) => {
if (prev.includes(valueOrEv)) return prev.filter((prevValue) => prevValue !== valueOrEv);
else if (!(0, utils_exports.isNumber)(max) || prev.length < max) return [...prev, valueOrEv];
else return prev;
});
}, [
interactive,
max,
setValue
]);
const getRootProps = useCallback(({ ref, "aria-describedby": ariaDescribedby, "aria-labelledby": ariaLabelledby,...props$1 } = {}) => ({
...dataProps,
"aria-describedby": (0, utils_exports.cx)(ariaDescribedbyProp, ariaDescribedby),
"aria-labelledby": (0, utils_exports.cx)(labelId, ariaLabelledby),
role: "group",
...rest,
...props$1,
ref: mergeRefs(ref, rest.ref)
}), [
ariaDescribedbyProp,
dataProps,
labelId,
rest
]);
return {
max,
value,
getInputProps: useCallback((props$1 = {}) => {
const checked = !(0, utils_exports.isUndefined)(value) && ((0, utils_exports.isString)(props$1.value) || (0, utils_exports.isNumber)(props$1.value)) && value.includes(props$1.value);
const trulyDisabled = !checked && (0, utils_exports.isNumber)(max) && value.length >= max;
return {
...dataProps,
...ariaProps,
type: "checkbox",
style: visuallyHiddenAttributes.style,
"aria-checked": checked,
"aria-disabled": (0, utils_exports.ariaAttr)(trulyDisabled),
"data-checked": (0, utils_exports.dataAttr)(checked),
"data-disabled": (0, utils_exports.dataAttr)(trulyDisabled),
checked,
disabled: trulyDisabled,
readOnly,
required,
...props$1,
onBlur: (0, utils_exports.handlerAll)(props$1.onBlur, eventProps.onBlur),
onChange: (0, utils_exports.handlerAll)(props$1.onChange, onChange),
onFocus: (0, utils_exports.handlerAll)(props$1.onFocus, eventProps.onFocus)
};
}, [
ariaProps,
dataProps,
eventProps,
max,
onChange,
readOnly,
required,
value
]),
getLabelProps: useCallback((props$1) => ({
...dataProps,
...props$1
}), [dataProps]),
getRootProps,
onChange
};
};
//#endregion
export { CheckboxGroupContext, useCheckboxGroup, useCheckboxGroupContext };
//# sourceMappingURL=use-checkbox-group.js.map