UNPKG

@yamada-ui/checkbox

Version:

Yamada UI checkbox component

86 lines (85 loc) 2.93 kB
"use client" "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/use-checkbox-group.ts var use_checkbox_group_exports = {}; __export(use_checkbox_group_exports, { useCheckboxGroup: () => useCheckboxGroup }); module.exports = __toCommonJS(use_checkbox_group_exports); var import_use_controllable_state = require("@yamada-ui/use-controllable-state"); var import_utils = require("@yamada-ui/utils"); var import_react = require("react"); var isEvent = (value) => value && (0, import_utils.isObject)(value) && (0, import_utils.isObject)(value.target); var useCheckboxGroup = ({ defaultValue = [], isNative, value: valueProp, onChange: onChangeProp, ...props }) => { const onChangeRef = (0, import_utils.useCallbackRef)(onChangeProp); const [value, setValue] = (0, import_use_controllable_state.useControllableState)({ defaultValue, value: valueProp, onChange: onChangeRef }); const onChange = (0, import_react.useCallback)( (evOrValue) => { const isChecked = isEvent(evOrValue) ? evOrValue.target.checked : !value.includes(evOrValue); const selectedValue = isEvent(evOrValue) ? evOrValue.target.value : evOrValue; const nextValue = isChecked ? [...value, selectedValue] : value.filter((v) => String(v) !== String(selectedValue)); setValue(nextValue); }, [value, setValue] ); const getContainerProps = (0, import_react.useCallback)( (props2 = {}, ref = null) => ({ role: "group", ...props2, ref }), [] ); const getCheckboxProps = (0, import_react.useCallback)( (props2 = {}, ref = null) => ({ ...props2, ref, [isNative ? "checked" : "isChecked"]: value.some( (val) => String(props2.value) === String(val) ), ...!isNative ? { checked: value.some((val) => String(props2.value) === String(val)) } : {}, onChange }), [onChange, isNative, value] ); return { props, setValue, value, getCheckboxProps, getContainerProps, onChange }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useCheckboxGroup }); //# sourceMappingURL=use-checkbox-group.js.map