UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

53 lines 2.84 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const util_1 = require("../../util"); const useFormField_1 = require("../useFormField"); const CheckboxGroup_1 = require("./CheckboxGroup"); /** * Handles props for Checkboxes in context with Fieldset and CheckboxGroup */ const useCheckbox = (props) => { const checkboxGroup = (0, react_1.useContext)(CheckboxGroup_1.CheckboxGroupContext); const _a = (0, useFormField_1.useFormField)((0, util_1.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); } }) }); }; exports.default = useCheckbox; //# sourceMappingURL=useCheckbox.js.map