UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

199 lines (198 loc) • 7.13 kB
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, _BlankDropTarget; 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 { DropTarget } from 'react-dnd'; import { Menu } from '@instructure/ui-menu'; import { Text } from '@instructure/ui-text'; import { jsx } from '@instructure/emotion'; import { processNewMathInElem } from '@instructure/quiz-rce'; import BlankAnswer from '../BlankAnswer'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/es/format-message'; import { withStyleOverrides } from '@instructure/quiz-common'; var ENTER_KEY = 'Enter'; var SPACE_KEY = ' '; var BlankDropTarget = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_BlankDropTarget = /*#__PURE__*/function (_Component) { function BlankDropTarget() { var _this2; _classCallCheck(this, BlankDropTarget); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, BlankDropTarget, [].concat(args)); _defineProperty(_this2, "toggleDragDropdown", function () { setTimeout(function () { var _this2$popover; if ((_this2$popover = _this2.popover) !== null && _this2$popover !== void 0 && _this2$popover._contentElement) { processNewMathInElem(_this2.popover._contentElement); } }); }); // ============= // HANDLERS // ============= _defineProperty(_this2, "handleRemoveAnswer", function () { _this2.props.returnChoiceToWordbank(_this2.props.choice.id); }); _defineProperty(_this2, "handleDragTargetKeyPress", function (event) { if (event.key === ENTER_KEY || event.key === SPACE_KEY) { event.preventDefault(); event.target.click(); } }); _defineProperty(_this2, "handleOptionSelect", function (choice) { return function () { _this2.props.onChange(choice); }; }); _defineProperty(_this2, "handleBlankAnswerRef", function (node) { _this2.blankAnswer = node; }); _defineProperty(_this2, "handleDragTargetRef", function (node) { _this2.dragTarget = node; }); _defineProperty(_this2, "handleParentRef", function (node) { _this2.parentNode = node; }); _defineProperty(_this2, "handlePopoverRef", function (node) { _this2.popover = node; }); return _this2; } _inherits(BlankDropTarget, _Component); return _createClass(BlankDropTarget, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var hasAnswer = this.hasAnswer(); var hasPrevAnswer = this.hasAnswer(prevProps); if (hasAnswer && !hasPrevAnswer) { this.blankAnswer.focus(); } else if (!hasAnswer && hasPrevAnswer) { this.dragTarget.focus(); } processNewMathInElem(this.parentNode); } }, { key: "hasAnswer", value: function hasAnswer() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; return props.choice && props.choice.id; } }, { key: "renderAnswer", value: // ============= // RENDER // ============= function renderAnswer() { return jsx(BlankAnswer, { ref: this.handleBlankAnswerRef, choiceId: this.props.choice.id, blankId: this.props.blankId, itemBody: this.props.choice.itemBody, onRemoveAnswer: this.handleRemoveAnswer, readOnly: this.props.readOnly }); } }, { key: "renderDragDropdown", value: function renderDragDropdown() { // when no answer selected return jsx(Menu, { trigger: this.renderDragTarget(), onToggle: this.toggleDragDropdown, popoverRef: this.handlePopoverRef }, this.renderOptions()); } }, { key: "renderDragTarget", value: function renderDragTarget() { return jsx("div", { css: this.props.styles.dragTarget, onKeyPress: this.handleDragTargetKeyPress, ref: this.handleDragTargetRef, role: "button", tabIndex: "0" }, jsx(Text, { color: "secondary" }, t('Answer'))); } }, { key: "renderOptions", value: function renderOptions() { var _this3 = this; return this.props.choices.map(function (choice) { return jsx(Menu.Item, { key: choice.id, value: choice.id, onSelect: _this3.handleOptionSelect(choice) }, choice.itemBody); }); } }, { key: "render", value: function render() { return this.props.connectDropTarget(jsx("div", { ref: this.handleParentRef, role: "presentation", tabIndex: "-1" }, this.hasAnswer() ? this.renderAnswer() : this.renderDragDropdown())); } }]); }(Component), _defineProperty(_BlankDropTarget, "displayName", 'BlankDropTarget'), _defineProperty(_BlankDropTarget, "componentId", "Quizzes".concat(_BlankDropTarget.displayName)), _defineProperty(_BlankDropTarget, "propTypes", { choices: PropTypes.array, choice: PropTypes.object, blankId: PropTypes.string.isRequired, connectDropTarget: PropTypes.func.isRequired, // injected by React DnD value: PropTypes.string, readOnly: PropTypes.bool, onChange: PropTypes.func, onDrop: PropTypes.func, returnChoiceToWordbank: PropTypes.func, styles: PropTypes.object }), _defineProperty(_BlankDropTarget, "defaultProps", { choices: void 0, choice: void 0, value: void 0, readOnly: false, onChange: void 0, onDrop: void 0, returnChoiceToWordbank: void 0 }), _BlankDropTarget)) || _class); // ============= // DRAG AND DROP // ============= var dropTarget = { drop: function drop(props, monitor, component) { var droppedItem = monitor.getItem(); props.onDrop(droppedItem); } }; function collectDrop(connect, monitor) { return { connectDropTarget: connect.dropTarget() }; } export default DropTarget('fitb', dropTarget, collectDrop)(BlankDropTarget);