UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

130 lines 4.68 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _get from "@babel/runtime/helpers/esm/get"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function _callSuper(_this, derived, args) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (e) { return false; } } derived = _getPrototypeOf(derived); return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args)); } import React from 'react'; import compact from 'lodash/compact'; import { v4 as uuid } from 'uuid'; import { rule, each, onSelf } from 'instructure-validations'; import { RichContentRenderer } from '@instructure/quiz-rce'; import { MULTIPLE_ANSWER_SLUG } from '../../interaction_slugs'; import InteractionType from '../InteractionType'; import { noDuplicatesMessage, presenceMessage } from '../../util/validationHelpers'; import t from '@instructure/quiz-i18n/es/format-message'; function newChoiceId() { return uuid(); } var MultipleAnswerInteractionType = /*#__PURE__*/function (_InteractionType) { function MultipleAnswerInteractionType(obj) { var _this2; _classCallCheck(this, MultipleAnswerInteractionType); _this2 = _callSuper(this, MultipleAnswerInteractionType); _defineProperty(_this2, "slug", MULTIPLE_ANSWER_SLUG); _defineProperty(_this2, "translatedName", t('Multiple Answer')); _defineProperty(_this2, "defaultProperties", { shuffleRules: { choices: { shuffled: false, toLock: [] } } }); _defineProperty(_this2, "getDefaultScoringData", function (intData) { return { value: [intData.choices[0].id, intData.choices[1].id] }; }); _defineProperty(_this2, "getDefaultInteractionData", function () { return { choices: [{ id: newChoiceId(), itemBody: '', position: 1 }, { id: newChoiceId(), itemBody: '', position: 2 }, { id: newChoiceId(), itemBody: '', position: 3 }, { id: newChoiceId(), itemBody: '', position: 4 }] }; }); _defineProperty(_this2, "getRenderedResponse", function (responseValue) { var interactionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { choices: [] }; return compact(responseValue.map(function (value) { var choice = interactionData.choices.find(function (choice) { return choice.id === value; }); if (choice !== void 0) { return /*#__PURE__*/React.createElement(RichContentRenderer, { key: choice.id, content: choice ? choice.itemBody : '' }); } else { return null; } })); }); _get((_this2, _getPrototypeOf(MultipleAnswerInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj); return _this2; } _inherits(MultipleAnswerInteractionType, _InteractionType); return _createClass(MultipleAnswerInteractionType, [{ key: "hasResponse", value: function hasResponse(resp) { return resp.length > 0; } }], [{ key: "validations", value: function validations() { var eachChoiceRule = { itemBody: [rule('presence', { message: presenceMessage(t('Answer')) })] }; return { interactionData: { choices: [each(eachChoiceRule), rule('noDuplicates', { field: 'itemBody', message: noDuplicatesMessage(t('Answer')) }), onSelf(rule('listSize', { minMessage: t('You must have at least two choices'), min: 2 }))] }, scoringData: { value: [onSelf(rule('listSize', { minMessage: t('You must select at least one correct answer'), min: 1 }))] } }; } }]); }(InteractionType); export { MultipleAnswerInteractionType as default };