@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
113 lines • 4.13 kB
JavaScript
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 { v4 as uuid } from 'uuid';
import { rule, each, onSelf } from 'instructure-validations';
import { RichContentRenderer } from '@instructure/quiz-rce';
import { MULTIPLE_CHOICE_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 eachChoiceRule = {
itemBody: [rule('presence', {
message: presenceMessage(t('Answer'))
})]
};
var MultipleChoiceInteractionType = /*#__PURE__*/function (_InteractionType) {
function MultipleChoiceInteractionType(obj) {
var _this2;
_classCallCheck(this, MultipleChoiceInteractionType);
_this2 = _callSuper(this, MultipleChoiceInteractionType);
_defineProperty(_this2, "slug", MULTIPLE_CHOICE_SLUG);
_defineProperty(_this2, "translatedName", t('Multiple Choice'));
_defineProperty(_this2, "defaultProperties", {
shuffleRules: {
choices: {
shuffled: false,
toLock: []
}
}
});
_defineProperty(_this2, "getDefaultScoringData", function (intData) {
return {
value: intData.choices[0].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, interactionData) {
var choice = interactionData.choices.find(function (c) {
return c.id === responseValue;
});
return /*#__PURE__*/React.createElement(RichContentRenderer, {
content: choice ? choice.itemBody : ''
});
});
_get((_this2, _getPrototypeOf(MultipleChoiceInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
_inherits(MultipleChoiceInteractionType, _InteractionType);
return _createClass(MultipleChoiceInteractionType, null, [{
key: "validations",
value: function validations() {
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: [rule('presence', {
message: t('You must select a correct answer')
})]
}
};
}
}]);
}(InteractionType);
export { MultipleChoiceInteractionType as default };