UNPKG

@fremtind/jkl-feedback-react

Version:
98 lines (97 loc) 3.96 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var RadioQuestion_exports = {}; __export(RadioQuestion_exports, { RadioQuestion: () => RadioQuestion }); module.exports = __toCommonJS(RadioQuestion_exports); var import_jkl_radio_button_react = require("@fremtind/jkl-radio-button-react"); var import_react = __toESM(require("react")); var import_followupContext = require("../followup/followupContext"); var import_mainQuestionContext = require("../main-question/mainQuestionContext"); const RadioQuestion = ({ label, name, options, helpLabel, autoFocus = false }) => { const followupContext = (0, import_followupContext.useFollowUpContext)(); const feedbackContext = (0, import_mainQuestionContext.useMainQuestionContext)(); const context = followupContext || feedbackContext; const id = (0, import_react.useId)(); const numOptions = (options == null ? void 0 : options.length) || 0; const ref = (0, import_react.useRef)(null); (0, import_react.useEffect)(() => { if (autoFocus && ref.current) { ref.current.focus(); } }, [autoFocus, ref]); const handleChange = (e) => { const option = options == null ? void 0 : options.find( (option2) => option2.value.toString() === e.target.value ); context == null ? void 0 : context.setCurrentValue(option); }; const selectedValue = (0, import_react.useMemo)( () => { var _a; return Array.isArray(context == null ? void 0 : context.currentValue) ? context == null ? void 0 : context.currentValue[0].value.toString() : (_a = context == null ? void 0 : context.currentValue) == null ? void 0 : _a.value.toString(); }, [context == null ? void 0 : context.currentValue] ); if (!context) { console.error( "Questions must be used inside a Followup or Feedback context provider" ); return null; } return /* @__PURE__ */ import_react.default.createElement( import_jkl_radio_button_react.RadioButtonGroup, { legend: label, labelProps: { variant: "large" }, name: "".concat(id, "-").concat(name || label), inline: numOptions < 3, value: selectedValue || "", onChange: handleChange, helpLabel }, options == null ? void 0 : options.map((option, i) => /* @__PURE__ */ import_react.default.createElement( import_jkl_radio_button_react.RadioButton, { ref: i === 0 ? ref : void 0, key: "".concat(id, "-").concat(name || label, "-").concat(option.value), label: option.label, value: String(option.value) } )) ); }; //# sourceMappingURL=RadioQuestion.js.map