UNPKG

@fremtind/jkl-feedback-react

Version:
111 lines (110 loc) 4.07 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 CheckboxQuestion_exports = {}; __export(CheckboxQuestion_exports, { CheckboxQuestion: () => CheckboxQuestion }); module.exports = __toCommonJS(CheckboxQuestion_exports); var import_jkl_checkbox_react = require("@fremtind/jkl-checkbox-react"); var import_jkl_input_group_react = require("@fremtind/jkl-input-group-react"); var import_react = __toESM(require("react")); var import_followupContext = require("../followup/followupContext"); var import_mainQuestionContext = require("../main-question/mainQuestionContext"); const CheckboxQuestion = ({ label, name, options, helpLabel, autoFocus = false }) => { const followupContext = (0, import_followupContext.useFollowUpContext)(); const feedbackContext = (0, import_mainQuestionContext.useMainQuestionContext)(); const context = followupContext || feedbackContext; const ref = (0, import_react.useRef)(null); (0, import_react.useEffect)(() => { if (autoFocus && ref.current) { ref.current.focus(); } }, [autoFocus, ref]); const handleChange = (event) => { const { value } = event.target; const matchingOption = options == null ? void 0 : options.find( (option) => option.value.toString() === value ); if (!matchingOption) return; if (!(context == null ? void 0 : context.currentValue)) { context == null ? void 0 : context.setCurrentValue([matchingOption]); return; } if (Array.isArray(context == null ? void 0 : context.currentValue)) { const found = context.currentValue.find( (option) => option === matchingOption ); if (found) { context.setCurrentValue( (oldValues) => oldValues.filter( (option) => option !== found ) ); } else { context.setCurrentValue((oldValues) => [ ...oldValues, matchingOption ]); } } }; 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_input_group_react.FieldGroup, { labelProps: { variant: "large" }, legend: label, helpLabel }, options == null ? void 0 : options.map((option, i) => /* @__PURE__ */ import_react.default.createElement( import_jkl_checkbox_react.Checkbox, { key: "".concat(label, "-").concat(option.value), name: name || label, value: option.value.toString(), onChange: handleChange, ref: i === 0 ? ref : void 0 }, option.label )) ); }; //# sourceMappingURL=CheckboxQuestion.js.map