UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

260 lines (259 loc) • 12.1 kB
/** @jsx jsx */ function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } 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 _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 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 _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } 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 { Component } from 'react'; import PropTypes from 'prop-types'; import map from 'lodash/fp/map'; import striptags from 'striptags'; import { jsx } from '@instructure/emotion'; import { CheckboxGroup, Checkbox } from '@instructure/ui-checkbox'; import { IconButton } from '@instructure/ui-buttons'; import { IconStrikethroughLine } from '@instructure/ui-icons'; import { ItemBodyWrapper, RichContentRenderer } from '@instructure/quiz-rce'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/format-message'; import { withStyleOverrides } from '@instructure/quiz-common'; var MultipleAnswerTake = /*#__PURE__*/ function(Component) { "use strict"; _inherits(MultipleAnswerTake, Component); function MultipleAnswerTake() { _class_call_check(this, MultipleAnswerTake); var _this; _this = _call_super(this, MultipleAnswerTake, arguments), _define_property(_this, "handleCheckboxGroupChange", function(value) { if (!_this.props.readOnly) { _this.props.handleResponseUpdate(value, { eliminated: _this.getEliminations() }); } }), _define_property(_this, "toggleElimination", function(choiceId) { return function(event) { event.preventDefault(); var currEliminations = _this.getEliminations(); if (currEliminations.includes(choiceId)) { var _this_props_userResponse; var choiceIndex = currEliminations.indexOf(choiceId); _this.props.handleResponseUpdate((_this_props_userResponse = _this.props.userResponse) === null || _this_props_userResponse === void 0 ? void 0 : _this_props_userResponse.value, { eliminated: _to_consumable_array(currEliminations.slice(0, choiceIndex)).concat(_to_consumable_array(currEliminations.slice(choiceIndex + 1))) }); } else { var _this_props_userResponse1; _this.props.handleResponseUpdate((_this_props_userResponse1 = _this.props.userResponse) === null || _this_props_userResponse1 === void 0 ? void 0 : _this_props_userResponse1.value, { eliminated: _to_consumable_array(currEliminations).concat([ choiceId ]) }); } }; }), _define_property(_this, "renderChoice", function(choice) { var _this_props = _this.props, readOnly = _this_props.readOnly, choiceElimination = _this_props.choiceElimination; var eliminated = _this.getEliminations().includes(choice.id); var itemBody = /*#__PURE__*/ jsx(RichContentRenderer, { customStyles: _this.props.styles.itemBody, content: choice.itemBody }); var showEliminations = !readOnly && choiceElimination; var screenReaderLabel = eliminated ? t('Undo elimination of answer {choice}', { choice: striptags(choice.itemBody) }) : t('Eliminate answer {choice}', { choice: striptags(choice.itemBody) }); var eliminationLabel = /*#__PURE__*/ jsx("div", { css: _this.props.styles.labelWrapper }, eliminated ? /*#__PURE__*/ jsx("s", null, itemBody) : itemBody, /*#__PURE__*/ jsx(IconButton, { withBackground: false, withBorder: false, onClick: _this.toggleElimination(choice.id), screenReaderLabel: screenReaderLabel }, /*#__PURE__*/ jsx(IconStrikethroughLine, null))); var ariaLabel = t('Answer: {choice}', { choice: striptags(choice.itemBody) }); var ariaEliminationLabel = eliminated ? t('Eliminated Answer: {choice}', { choice: striptags(choice.itemBody) }) : ariaLabel; return /*#__PURE__*/ jsx(Checkbox, { key: choice.id, value: choice.id, "aria-label": showEliminations ? ariaEliminationLabel : ariaLabel, label: showEliminations ? eliminationLabel : itemBody }); }); return _this; } _create_class(MultipleAnswerTake, [ { key: "getEliminations", value: function getEliminations() { var _this_props_userResponse_properties, _this_props_userResponse; return ((_this_props_userResponse = this.props.userResponse) === null || _this_props_userResponse === void 0 ? void 0 : (_this_props_userResponse_properties = _this_props_userResponse.properties) === null || _this_props_userResponse_properties === void 0 ? void 0 : _this_props_userResponse_properties.eliminated) || []; } }, { key: "render", value: function render() { var _this_props_userResponse; return /*#__PURE__*/ jsx(ItemBodyWrapper, null, /*#__PURE__*/ jsx("div", { className: "fs-mask" }, /*#__PURE__*/ jsx(CheckboxGroup, { value: ((_this_props_userResponse = this.props.userResponse) === null || _this_props_userResponse === void 0 ? void 0 : _this_props_userResponse.value) || [], name: "interaction_".concat(this.props.itemId), onChange: this.handleCheckboxGroupChange, readOnly: this.props.readOnly, description: /*#__PURE__*/ jsx(RichContentRenderer, { content: this.props.itemBody, customStyles: this.props.styles.itemDescription }) }, map(this.renderChoice, this.props.interactionData.choices)))); } } ]); return MultipleAnswerTake; }(Component); _define_property(MultipleAnswerTake, "displayName", 'MultipleAnswerTake'); _define_property(MultipleAnswerTake, "componentId", "Quizzes".concat(MultipleAnswerTake.displayName)); _define_property(MultipleAnswerTake, "propTypes", { choiceElimination: PropTypes.bool, handleResponseUpdate: PropTypes.func.isRequired, itemId: PropTypes.string, itemBody: PropTypes.string.isRequired, interactionData: PropTypes.shape({ choices: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.string, itemBody: PropTypes.string })) }).isRequired, userResponse: PropTypes.shape({ value: PropTypes.arrayOf(PropTypes.string), properties: PropTypes.shape({ eliminated: PropTypes.array }) }), readOnly: PropTypes.bool, styles: PropTypes.object }); _define_property(MultipleAnswerTake, "defaultProps", { choiceElimination: false, itemId: void 0, readOnly: false, userResponse: {}, handleResponseUpdate: Function.prototype }); export { MultipleAnswerTake as default }; MultipleAnswerTake = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], MultipleAnswerTake);