UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

273 lines (272 loc) • 12.2 kB
function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { v4 as uuid } from 'uuid'; import update from 'immutability-helper'; import { Flex, FormFieldGroup } from '@instructure/quiz-common'; import RichFillBlankInteractionType from '../../../../records/interactions/rich_fill_blank'; import ChoiceInput from '../../../common/edit/components/ChoiceInput'; import Footer from '../../../common/edit/components/Footer'; import { getNextItemIdFromArray } from '../../../../util/focusHelpers'; import { toErrors } from '../../../../util/instUIMessages'; import t from '@instructure/quiz-i18n/format-message'; import { normalizeErrors } from '../../../../util/normalizeErrors'; var WordBankDistractors = /*#__PURE__*/ function(Component) { "use strict"; _inherits(WordBankDistractors, Component); function WordBankDistractors() { _class_call_check(this, WordBankDistractors); var _this; _this = _call_super(this, WordBankDistractors, arguments), _define_property(_this, "distractorRefs", {}), _define_property(_this, "getErrorsForChoice", function(id) { var wordBankChoiceIndex = _this.props.indexForWordBankChoice(id); var errors = _this.props.interactionDataErrors[wordBankChoiceIndex] || {}; return toErrors(errors.itemBody || []); }), _define_property(_this, "onWordBankDistractorInputChange", function(id, e) { var wordBankChoiceIndex = _this.props.indexForWordBankChoice(id); var interactionData = update(_this.props.interactionData, { wordBankChoices: _define_property({}, wordBankChoiceIndex, { itemBody: { $set: e.target.value } }) }); _this.props.changeItemState({ interactionData: interactionData }); }), _define_property(_this, "handleCreateWordBankDistractor", function() { var id = uuid(); var newWordBankDistractor = { id: id, itemBody: '' }; var interactionData = update(_this.props.interactionData, { wordBankChoices: { $push: [ newWordBankDistractor ] } }); _this.props.changeItemState({ interactionData: interactionData }); }), _define_property(_this, "handleRemoveWordBankDistractor", function(distractorId) { var newWordBankChoices = _this.props.interactionData.wordBankChoices.filter(function(wordBankChoice) { return wordBankChoice.id !== distractorId; }); var interactionData = update(_this.props.interactionData, { wordBankChoices: { $set: newWordBankChoices } }); _this.props.changeItemState({ interactionData: interactionData }); // focus handling: var nextDistractorId = getNextItemIdFromArray(distractorId, _this.getWordBankDistractors()); var nextDistractorRef = _this.distractorRefs[nextDistractorId]; if (nextDistractorRef) { nextDistractorRef.removeChoicebutton.focus(); } else { _this.addDistractorRef.focus(); } }), _define_property(_this, "handleRemoveChoice", function(distractorId) { return function() { _this.handleRemoveWordBankDistractor(distractorId); }; }), _define_property(_this, "handleDistractorRef", function(distractorId) { return function(node) { _this.distractorRefs[distractorId] = node; }; }), _define_property(_this, "handleAddDistractorRef", function(node) { _this.addDistractorRef = node; }); return _this; } _create_class(WordBankDistractors, [ { key: "getWordBankDistractors", value: function getWordBankDistractors() { var correctChoiceIds = this.props.scoringData.value.map(function(scoringData) { return scoringData.scoringData.choiceId; }).filter(function(c) { return c; }); return this.props.interactionData.wordBankChoices.filter(function(wordBankChoice) { return !correctChoiceIds.includes(wordBankChoice.id); }); } }, { key: "render", value: function render() { var _this = this; var _this_props = this.props, isSurvey = _this_props.isSurvey, interactionData = _this_props.interactionData, interactionDataErrors = _this_props.interactionDataErrors, notifyScreenreader = _this_props.notifyScreenreader, overrideEditableForRegrading = _this_props.overrideEditableForRegrading, onModalOpen = _this_props.onModalOpen, onModalClose = _this_props.onModalClose; if (!interactionData.wordBankChoices) return null; var noFocusOnFirstTime = true; return /*#__PURE__*/ React.createElement(FormFieldGroup, { description: isSurvey ? t('Answers') : t('Word Bank Distractors'), messages: normalizeErrors(interactionDataErrors.$errors) }, /*#__PURE__*/ React.createElement(Flex, { direction: "column", gap: "small" }, /*#__PURE__*/ React.createElement(Flex.Item, null, /*#__PURE__*/ React.createElement(Flex, { direction: "column", gap: "small" }, this.getWordBankDistractors().map(function(distractor, i, source) { var focusOnMount = noFocusOnFirstTime ? noFocusOnFirstTime = !noFocusOnFirstTime : i + 1 === source.length && !distractor.itemBody; return /*#__PURE__*/ React.createElement(ChoiceInput, { key: distractor.id, id: distractor.id, renderLabel: isSurvey ? t('Answer') : t('Distractor'), isRequired: true, disabledFields: overrideEditableForRegrading ? [ 'answerInput' ] : [], itemBody: distractor.itemBody, errors: _this.getErrorsForChoice(distractor.id), onInputChange: _this.onWordBankDistractorInputChange, onModalClose: onModalClose, onModalOpen: onModalOpen, onRemoveChoice: _this.handleRemoveChoice(distractor.id), ref: _this.handleDistractorRef(distractor.id), focusOnMount: focusOnMount, shouldRenderRemoveChoice: !overrideEditableForRegrading, noRCE: true, notifyScreenreader: notifyScreenreader }); }))), !overrideEditableForRegrading && /*#__PURE__*/ React.createElement(Flex.Item, null, /*#__PURE__*/ React.createElement(Footer, { ref: this.handleAddDistractorRef, onCreateChoice: this.handleCreateWordBankDistractor, buttonText: isSurvey ? t('Answer') : t('Distractor'), screenReaderText: isSurvey ? t('Add Answer') : t('Add Distractor'), notifyScreenreader: notifyScreenreader })))); } } ]); return WordBankDistractors; }(Component); _define_property(WordBankDistractors, "interactionType", RichFillBlankInteractionType); _define_property(WordBankDistractors, "propTypes", { changeItemState: PropTypes.func.isRequired, indexForWordBankChoice: PropTypes.func.isRequired, interactionData: PropTypes.shape({ wordBankChoices: PropTypes.arrayOf(PropTypes.shape({ itemBody: PropTypes.string, id: PropTypes.string })) }).isRequired, interactionDataErrors: PropTypes.oneOfType([ PropTypes.shape({ $errors: PropTypes.arrayOf(PropTypes.string) }), PropTypes.objectOf(PropTypes.shape({ itemBody: PropTypes.arrayOf(PropTypes.string) })) ]).isRequired, notifyScreenreader: PropTypes.func.isRequired, onModalClose: PropTypes.func, onModalOpen: PropTypes.func, overrideEditableForRegrading: PropTypes.bool, scoringData: PropTypes.shape({ value: PropTypes.arrayOf(PropTypes.shape({ scoringData: PropTypes.shape({ choiceId: PropTypes.string }) })) }).isRequired, isSurvey: PropTypes.bool.isRequired }); _define_property(WordBankDistractors, "defaultProps", { overrideEditableForRegrading: false, onModalClose: void 0, onModalOpen: void 0, scoringData: void 0, isSurvey: true }); export { WordBankDistractors as default };