UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

193 lines (189 loc) • 7.95 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; 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 _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _dec, _class, _MultipleAnswerTake; 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)); } /** @jsx jsx */ 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/es/format-message'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: MultipleAnswer --- Multiple Answer Take component ```jsx_example function Example (props) { const exampleProps = { itemBody: 'Who was in the first cabinet of the USA?', interactionData: { choices: [ { id: 'uuid1', itemBody: 'Thomas Jefferson', position: 1 }, { id: 'uuid2', itemBody: 'John Marshall', position: 2 }, { id: 'uuid3', itemBody: 'John Knox', position: 3 }, { id: 'uuid4', itemBody: 'Alexander Hamilton', position: 4 }, { id: 'uuid5', itemBody: 'Aaron Burr', position: 5 }, { id: 'uuid6', itemBody: 'Ben Franklin', position: 6 } ] }, scoringData: { value: ['uuid1', 'uuid3', 'uuid4'] }, userResponse: { value: ['uuid1', 'uuid6'] } } return ( <MultipleAnswerTake {...exampleProps} {...props} /> ) } <SettingsSwitcher locales={LOCALES}> <TakeStateProvider> <Example /> </TakeStateProvider> </SettingsSwitcher> ``` **/ var MultipleAnswerTake = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_MultipleAnswerTake = /*#__PURE__*/function (_Component) { function MultipleAnswerTake() { var _this2; _classCallCheck(this, MultipleAnswerTake); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, MultipleAnswerTake, [].concat(args)); _defineProperty(_this2, "handleCheckboxGroupChange", function (value) { if (!_this2.props.readOnly) { _this2.props.handleResponseUpdate(value, { eliminated: _this2.getEliminations() }); } }); _defineProperty(_this2, "toggleElimination", function (choiceId) { return function (event) { event.preventDefault(); var currEliminations = _this2.getEliminations(); if (currEliminations.includes(choiceId)) { var choiceIndex = currEliminations.indexOf(choiceId); _this2.props.handleResponseUpdate(_this2.props.userResponse.value, { eliminated: [].concat(_toConsumableArray(currEliminations.slice(0, choiceIndex)), _toConsumableArray(currEliminations.slice(choiceIndex + 1))) }); } else { _this2.props.handleResponseUpdate(_this2.props.userResponse.value, { eliminated: [].concat(_toConsumableArray(currEliminations), [choiceId]) }); } }; }); _defineProperty(_this2, "renderChoice", function (choice) { var _this2$props = _this2.props, readOnly = _this2$props.readOnly, choiceElimination = _this2$props.choiceElimination; var eliminated = _this2.getEliminations().includes(choice.id); var itemBody = jsx(RichContentRenderer, { customStyles: _this2.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 = jsx("div", { css: _this2.props.styles.labelWrapper }, eliminated ? jsx("s", null, itemBody) : itemBody, jsx(IconButton, { withBackground: false, withBorder: false, onClick: _this2.toggleElimination(choice.id), screenReaderLabel: screenReaderLabel }, 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 jsx(Checkbox, { key: choice.id, value: choice.id, "aria-label": showEliminations ? ariaEliminationLabel : ariaLabel, label: showEliminations ? eliminationLabel : itemBody }); }); return _this2; } _inherits(MultipleAnswerTake, _Component); return _createClass(MultipleAnswerTake, [{ key: "getEliminations", value: function getEliminations() { var _this$props$userRespo; return ((_this$props$userRespo = this.props.userResponse.properties) === null || _this$props$userRespo === void 0 ? void 0 : _this$props$userRespo.eliminated) || []; } }, { key: "render", value: function render() { return jsx(ItemBodyWrapper, null, jsx("div", { className: "fs-mask" }, jsx(CheckboxGroup, { value: this.props.userResponse.value || [], name: "interaction_".concat(this.props.itemId), onChange: this.handleCheckboxGroupChange, readOnly: this.props.readOnly, description: jsx(RichContentRenderer, { content: this.props.itemBody, customStyles: this.props.styles.itemDescription }) }, map(this.renderChoice, this.props.interactionData.choices)))); } }]); }(Component), _defineProperty(_MultipleAnswerTake, "displayName", 'MultipleAnswerTake'), _defineProperty(_MultipleAnswerTake, "componentId", "Quizzes".concat(_MultipleAnswerTake.displayName)), _defineProperty(_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 }), _defineProperty(_MultipleAnswerTake, "defaultProps", { choiceElimination: false, itemId: void 0, readOnly: false, userResponse: {}, handleResponseUpdate: Function.prototype }), _MultipleAnswerTake)) || _class); export { MultipleAnswerTake as default };