@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
380 lines (379 loc) • 16.6 kB
JavaScript
/** @jsx jsx */ 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;
})();
}
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import sortBy from 'lodash/fp/sortBy';
import map from 'lodash/fp/map';
import flatten from 'lodash/fp/flatten';
import find from 'lodash/fp/find';
import flow from 'lodash/fp/flow';
import { v4 as uuid } from 'uuid';
import { jsx, css } from '@instructure/emotion';
import { ScreenReaderContent } from '@instructure/ui-a11y-content';
import { Text } from '@instructure/ui-text';
import { ItemBodyWrapper } from '@instructure/quiz-rce';
import BlankDropTarget from '../../drag_components/BlankDropTarget';
import { trimPunctuation, getTrimmedPunctuation } from '../../helpers/punctuation';
import generateStyle from './styles';
import generateComponentTheme from './theme';
import t from '@instructure/quiz-i18n/format-message';
import { SimpleSelect, TextInput, withStyleOverrides } from '@instructure/quiz-common';
var TakeStem = /*#__PURE__*/ function(Component) {
"use strict";
_inherits(TakeStem, Component);
function TakeStem() {
_class_call_check(this, TakeStem);
var _this;
_this = _call_super(this, TakeStem, arguments), _define_property(_this, "state", {
isMounted: false
}), _define_property(_this, "id", uuid()), _define_property(_this, "updateWordBankUserResponse", function(blankId) {
return function(choice) {
var wordBankChoices = _this.props.interactionData.wordBankChoices;
var choiceValue = wordBankChoices ? choice.itemBody : choice.id;
_this.updateUserResponse(blankId, choiceValue);
};
}), _define_property(_this, "renderStemItem", function(stemItem) {
if (stemItem.type === 'text') {
return /*#__PURE__*/ jsx(Text, {
color: "primary",
key: stemItem.id
}, trimPunctuation(stemItem, _this.props.interactionData.stemItems));
}
var blank = find({
id: stemItem.blankId
}, _this.props.interactionData.blanks);
var punctuation = getTrimmedPunctuation(stemItem, _this.props.interactionData.stemItems);
return /*#__PURE__*/ jsx("span", {
key: stemItem.id,
className: "stemItem",
css: _this.props.styles.stemItem
}, punctuation.start, _this.renderBlank(blank), punctuation.end);
}), _define_property(_this, "renderRichBlanks", function() {
if (_this.state.isMounted) {
var questionWrapper = document.getElementById(_this.id);
return _this.props.interactionData.blanks.map(function(blank) {
if (questionWrapper.querySelector("#blank_".concat(blank.id))) {
return /*#__PURE__*/ ReactDOM.createPortal(_this.renderBlank(blank), questionWrapper.querySelector("#blank_".concat(blank.id)));
}
});
}
});
return _this;
}
_create_class(TakeStem, [
{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
isMounted: true
});
}
},
{
// =============
// HELPERS
// =============
key: "getWordbankChoiceByResponseValue",
value: function getWordbankChoiceByResponseValue(responseValue) {
if (this.props.interactionData.wordBankChoices) {
return this.props.interactionData.wordBankChoices.find(function(choice) {
return choice.itemBody === responseValue;
});
}
return this.getWordbankChoiceById(responseValue);
}
},
{
key: "getWordbankChoiceById",
value: function getWordbankChoiceById(choiceId) {
return flow(map(function(b) {
return b.answerType === 'wordbank' ? b.choices : [];
}), flatten, find({
id: choiceId
}))(this.props.interactionData.blanks);
}
},
{
key: "updateUserResponse",
value: function updateUserResponse(blankId, blankValue) {
var newResponses = this.props.userResponses.filter(function(blank) {
return blank.id !== blankId;
}).concat([
{
id: blankId,
value: blankValue,
type: 'Text'
}
]);
this.props.onUpdate(newResponses);
}
},
{
key: "getValueForBlank",
value: function getValueForBlank(blankId) {
var objectForBlank = this.props.userResponses.find(function(response) {
return response.id === blankId;
});
return objectForBlank && 'value' in objectForBlank ? objectForBlank.value : null;
}
},
{
// =============
// RENDERING
// =============
key: "renderDropdown",
value: function renderDropdown(blank, blankSRText) {
var _this = this;
var selectOptionIdPrefix = 'take-stem-select-option-';
var selectedChoiceId = this.getValueForBlank(blank.id);
var choice = blank.choices.find(function(choice) {
return choice.id === selectedChoiceId;
});
var mountNodeId = "mountNode_".concat(blank.id);
return /*#__PURE__*/ jsx(React.Fragment, null, /*#__PURE__*/ jsx("div", {
css: this.props.styles.printBlank
}, /*#__PURE__*/ jsx(TextInput, {
display: "inline-block",
renderLabel: /*#__PURE__*/ jsx(ScreenReaderContent, null, t('Printed Blank')),
interaction: "readonly",
defaultValue: choice && choice.itemBody
})), /*#__PURE__*/ jsx("div", {
className: "fs-mask",
css: this.props.styles.noPrintBlank
}, /*#__PURE__*/ jsx(SimpleSelect, {
value: this.getValueForBlank(blank.id),
renderLabel: /*#__PURE__*/ jsx(ScreenReaderContent, null, blankSRText),
onChange: function(event, param) {
var value = param.value;
return _this.updateUserResponse(blank.id, value);
},
interaction: this.props.readOnly ? 'readonly' : 'enabled',
mountNode: function() {
return document.getElementById(mountNodeId);
}
}, /*#__PURE__*/ jsx(SimpleSelect.Option, {
id: "take-stem-select-option-empty",
value: "",
style: css(this.props.styles.selectPlaceholder).styles
}, t('choose your answer...')), blank.choices.map(function(choice) {
return /*#__PURE__*/ jsx(SimpleSelect.Option, {
id: selectOptionIdPrefix + choice.id,
key: choice.id,
value: choice.id
}, choice.itemBody);
})), /*#__PURE__*/ jsx("div", {
id: mountNodeId
})));
}
},
{
key: "renderWordbankBlank",
value: function renderWordbankBlank(blank, blankSRText) {
var _this = this;
var blankResponseValue = this.getValueForBlank(blank.id);
var choice = this.getWordbankChoiceByResponseValue(blankResponseValue);
return /*#__PURE__*/ jsx(React.Fragment, null, /*#__PURE__*/ jsx("div", {
css: this.props.styles.printBlank
}, /*#__PURE__*/ jsx(TextInput, {
display: "inline-block",
renderLabel: /*#__PURE__*/ jsx(ScreenReaderContent, null, t('Printed Blank')),
interaction: "readonly",
defaultValue: choice && choice.itemBody
})), /*#__PURE__*/ jsx("div", {
className: "fs-mask",
css: this.props.styles.noPrintBlank
}, /*#__PURE__*/ jsx(ScreenReaderContent, null, blankSRText), /*#__PURE__*/ jsx(BlankDropTarget, {
key: blank.id,
choices: this.props.wordBankChoices,
choice: choice,
blankId: blank.id,
onChange: this.updateWordBankUserResponse(blank.id),
onDrop: this.updateWordBankUserResponse(blank.id),
readOnly: this.props.readOnly,
returnChoiceToWordbank: function(choiceId) {
return _this.props.returnChoiceToWordbank(blank.id, choiceId);
}
})));
}
},
{
key: "renderOpenEntry",
value: function renderOpenEntry(blank, blankSRText) {
var _this = this;
var value = this.getValueForBlank(blank.id);
return /*#__PURE__*/ jsx("span", {
className: "fs-mask"
}, /*#__PURE__*/ jsx(TextInput, {
display: "inline-block",
renderLabel: /*#__PURE__*/ jsx(ScreenReaderContent, null, blankSRText),
placeholder: t('type your answer...'),
// use defaultValue for Take (uncontrolled)
defaultValue: this.props.readOnly ? void 0 : value,
// use value for Show (controlled, so it updates when changed)
value: this.props.readOnly ? value : void 0,
onChange: function(e) {
return _this.updateUserResponse(blank.id, e.target.value.trim());
},
interaction: this.props.readOnly ? 'readonly' : 'enabled',
"data-automation": "sdk-fitb-taking-answer",
autoComplete: "off"
}));
}
},
{
key: "renderBlank",
value: function renderBlank(blank) {
var blankPosition = this.props.interactionData.blanks.indexOf(blank) + 1;
var blankSRText = t('Question Blank { blankPosition } of { totalBlanks }', {
blankPosition: blankPosition,
totalBlanks: this.props.interactionData.blanks.length
});
switch(blank.answerType){
case 'openEntry':
return this.renderOpenEntry(blank, blankSRText);
case 'dropdown':
return this.renderDropdown(blank, blankSRText);
default:
return this.renderWordbankBlank(blank, blankSRText);
}
}
},
{
key: "render",
value: function render() {
if (this.props.richFITB) {
return /*#__PURE__*/ jsx(ItemBodyWrapper, {
id: this.id,
itemBody: this.props.itemBody
}, this.renderRichBlanks());
}
return /*#__PURE__*/ jsx("div", {
css: this.props.styles.stemWrapper,
"data-automation": "take-stem-wrapper"
}, flow(sortBy([
'position'
]), map(this.renderStemItem))(this.props.interactionData.stemItems));
}
}
]);
return TakeStem;
}(Component);
_define_property(TakeStem, "displayName", 'TakeStem');
_define_property(TakeStem, "componentId", "Quizzes".concat(TakeStem.displayName));
_define_property(TakeStem, "propTypes", {
onUpdate: PropTypes.func.isRequired,
interactionData: PropTypes.object.isRequired,
itemBody: PropTypes.string,
returnChoiceToWordbank: PropTypes.func,
userResponses: PropTypes.array,
readOnly: PropTypes.bool,
richFITB: PropTypes.bool,
wordBankChoices: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
itemBody: PropTypes.string
})),
styles: PropTypes.object
});
_define_property(TakeStem, "defaultProps", {
itemBody: '',
returnChoiceToWordbank: void 0,
userResponses: [],
readOnly: false,
richFITB: false,
wordBankChoices: void 0
});
export { TakeStem as default };
TakeStem = _ts_decorate([
withStyleOverrides(generateStyle, generateComponentTheme)
], TakeStem);