@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
52 lines • 2.99 kB
JavaScript
;
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 });
exports.useRadio = void 0;
const react_1 = require("react");
const util_1 = require("../../util");
const useFormField_1 = require("../useFormField");
const RadioGroup_1 = require("./RadioGroup");
/**
* Handles props for Radios in context with Fieldset and RadioGroup
*/
const useRadio = (props) => {
const radioGroup = (0, react_1.useContext)(RadioGroup_1.RadioGroupContext);
const _a = (0, useFormField_1.useFormField)((0, util_1.omit)(props, ["description"]), "radio"), { inputProps, readOnly } = _a, rest = __rest(_a, ["inputProps", "readOnly"]);
if (!radioGroup) {
console.warn("<Radio> must be used inside <RadioGroup>.");
}
if ((props === null || props === void 0 ? void 0 : props.required) !== undefined) {
console.warn("required is only supported on <RadioGroup>.");
}
return Object.assign(Object.assign({}, rest), { readOnly, inputProps: Object.assign(Object.assign({}, inputProps), { name: radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.name, defaultChecked: (radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.defaultValue) === undefined
? undefined
: (radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.defaultValue) === props.value, checked: (radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.value) === undefined
? undefined
: (radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.value) === props.value, onChange: (event) => {
var _a, _b;
if (readOnly) {
return;
}
(_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, event);
(_b = radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.onChange) === null || _b === void 0 ? void 0 : _b.call(radioGroup, 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);
}, required: radioGroup === null || radioGroup === void 0 ? void 0 : radioGroup.required, type: "radio" }) });
};
exports.useRadio = useRadio;
//# sourceMappingURL=useRadio.js.map