UNPKG

@fremtind/jkl-feedback-react

Version:
105 lines (104 loc) 4.52 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 SmileyQuestion_exports = {}; __export(SmileyQuestion_exports, { SmileyQuestion: () => SmileyQuestion }); module.exports = __toCommonJS(SmileyQuestion_exports); 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"); var import_smileyUtils = require("./smileyUtils"); const isNotInScale = (option) => typeof option.value !== "number" || ![1, 2, 3, 4, 5].includes(option.value); const SmileyQuestion = ({ label, name = "smiley", helpLabel, options = import_smileyUtils.defaultOptions }) => { const followupContext = (0, import_followupContext.useFollowUpContext)(); const feedbackContext = (0, import_mainQuestionContext.useMainQuestionContext)(); const context = followupContext || feedbackContext; const id = (0, import_react.useId)(); 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 : (_a = context == null ? void 0 : context.currentValue) == null ? void 0 : _a.value; }, [context == null ? void 0 : context.currentValue] ); if (options.some(isNotInScale)) { console.error( "SmileyQuestion m\xE5 ha tallene 1 til 5 som verdier for alternativene sine" ); return null; } if (!context || !options) { 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 }, /* @__PURE__ */ import_react.default.createElement("div", { className: "jkl-feedback-smileys" }, options.map((option) => /* @__PURE__ */ import_react.default.createElement(import_react.Fragment, { key: option.value }, /* @__PURE__ */ import_react.default.createElement( "input", { className: "jkl-sr-only", id: "".concat(id, "-").concat(name, "-").concat(option.value), name: "".concat(id, "-").concat(name), type: "radio", value: option.value, onChange: handleChange, checked: selectedValue === option.value } ), /* @__PURE__ */ import_react.default.createElement( "label", { className: "jkl-feedback-smiley-option", htmlFor: "".concat(id, "-").concat(name, "-").concat(option.value) }, /* @__PURE__ */ import_react.default.createElement("span", { className: "jkl-sr-only" }, option.label), (0, import_smileyUtils.getSmiley)(Number(option.value)) )))) ); }; //# sourceMappingURL=SmileyQuestion.js.map