@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
48 lines • 2.83 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;
};
import { useContext } from "react";
import { omit } from "../../util/index.js";
import { useFormField } from "../useFormField.js";
import { RadioGroupContext } from "./RadioGroup.js";
/**
* Handles props for Radios in context with Fieldset and RadioGroup
*/
export const useRadio = (props) => {
const radioGroup = useContext(RadioGroupContext);
const _a = useFormField(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" }) });
};
//# sourceMappingURL=useRadio.js.map