UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

51 lines 2.72 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { useContext } from "react"; import { omit } from "../../util/index.js"; import { useFormField } from "../useFormField.js"; import { CheckboxGroupContext } from "./CheckboxGroup.js"; /** * Handles props for Checkboxes in context with Fieldset and CheckboxGroup */ const useCheckbox = (props) => { const checkboxGroup = useContext(CheckboxGroupContext); const _a = useFormField(omit(props, ["description", "children"]), "checkbox"), { inputProps, readOnly } = _a, rest = __rest(_a, ["inputProps", "readOnly"]); if (checkboxGroup) { if (props.checked) { console.warn("`checked` is unsupported on <Checkbox> elements within a <CheckboxGroup>. Please set a `value` or `defaultValue` on <CheckboxGroup> instead."); } if (props.value === undefined) { console.warn("A <Checkbox> element within a <CheckboxGroup> requires a `value` property."); } } return Object.assign(Object.assign({}, rest), { readOnly, nested: !!checkboxGroup, inputProps: Object.assign(Object.assign({}, inputProps), { checked: (checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.value) ? checkboxGroup.value.includes(props.value) : props.checked, defaultChecked: (checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.defaultValue) ? checkboxGroup.defaultValue.includes(props.value) : props.defaultChecked, onChange: (event) => { var _a; if (readOnly) { return; } (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, event); checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.toggleValue(props.value); }, onClick: (event) => { var _a; if (readOnly) { event.preventDefault(); return; } (_a = props === null || props === void 0 ? void 0 : props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, event); } }) }); }; export default useCheckbox; //# sourceMappingURL=useCheckbox.js.map