UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

191 lines (190 loc) • 7.12 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_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } 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); } import { useCallback, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import { useDrop } 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/components/RichContentRenderer/mathRenderer'; import BlankAnswer from '../BlankAnswer'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/format-message'; import { withStyleOverrides } from '@instructure/quiz-common/util/withStyleOverrides'; var ENTER_KEY = 'Enter'; var SPACE_KEY = ' '; var hasAnswerOf = function(choice) { return Boolean(choice && choice.id); }; function BlankDropTarget(props) { var choices = props.choices, choice = props.choice, blankId = props.blankId, readOnly = props.readOnly, onChange = props.onChange, onDrop = props.onDrop, returnChoiceToWordbank = props.returnChoiceToWordbank, styles = props.styles; var blankAnswerRef = useRef(null); var dragTargetRef = useRef(null); var parentNodeRef = useRef(null); var popoverRef = useRef(null); var hasAnswer = hasAnswerOf(choice); var prevHasAnswerRef = useRef(hasAnswer); var _useDrop = _sliced_to_array(useDrop(function() { return { accept: 'fitb', drop: function drop(item) { if (onDrop) onDrop(item); } }; }, [ onDrop ]), 2), drop = _useDrop[1]; useEffect(function() { var prevHasAnswer = prevHasAnswerRef.current; if (hasAnswer && !prevHasAnswer) { var _blankAnswerRef_current; (_blankAnswerRef_current = blankAnswerRef.current) === null || _blankAnswerRef_current === void 0 ? void 0 : _blankAnswerRef_current.focus(); } else if (!hasAnswer && prevHasAnswer) { var _dragTargetRef_current; (_dragTargetRef_current = dragTargetRef.current) === null || _dragTargetRef_current === void 0 ? void 0 : _dragTargetRef_current.focus(); } processNewMathInElem(parentNodeRef.current); prevHasAnswerRef.current = hasAnswer; }); var setParentRef = useCallback(function(node) { parentNodeRef.current = node; drop(node); }, [ drop ]); var setPopoverRef = useCallback(function(node) { popoverRef.current = node; }, []); function toggleDragDropdown() { setTimeout(function() { var _popoverRef_current; if ((_popoverRef_current = popoverRef.current) === null || _popoverRef_current === void 0 ? void 0 : _popoverRef_current._contentElement) { processNewMathInElem(popoverRef.current._contentElement); } }); } function handleRemoveAnswer() { returnChoiceToWordbank(choice.id); } function handleDragTargetKeyDown(event) { if (event.key === ENTER_KEY || event.key === SPACE_KEY) { event.preventDefault(); event.target.click(); } } function makeOptionSelectHandler(selectedChoice) { return function onOptionSelect() { onChange(selectedChoice); }; } function renderAnswer() { return /*#__PURE__*/ jsx(BlankAnswer, { ref: blankAnswerRef, choiceId: choice.id, blankId: blankId, itemBody: choice.itemBody, onRemoveAnswer: handleRemoveAnswer, readOnly: readOnly }); } function renderDragTarget() { return /*#__PURE__*/ jsx("div", { css: styles.dragTarget, onKeyDown: handleDragTargetKeyDown, ref: dragTargetRef, role: "button", tabIndex: "0" }, /*#__PURE__*/ jsx(Text, { color: "secondary" }, t('Answer'))); } function renderOptions() { return choices.map(function(c) { return /*#__PURE__*/ jsx(Menu.Item, { key: c.id, value: c.id, onSelect: makeOptionSelectHandler(c) }, c.itemBody); }); } function renderDragDropdown() { return /*#__PURE__*/ jsx(Menu, { trigger: renderDragTarget(), onToggle: toggleDragDropdown, popoverRef: setPopoverRef }, renderOptions()); } return /*#__PURE__*/ jsx("div", { ref: setParentRef, role: "presentation", tabIndex: "-1" }, hasAnswer ? renderAnswer() : renderDragDropdown()); } BlankDropTarget.displayName = 'BlankDropTarget'; BlankDropTarget.componentId = "Quizzes".concat(BlankDropTarget.displayName); BlankDropTarget.propTypes = { choices: PropTypes.array, choice: PropTypes.object, blankId: PropTypes.string.isRequired, value: PropTypes.string, readOnly: PropTypes.bool, onChange: PropTypes.func, onDrop: PropTypes.func, returnChoiceToWordbank: PropTypes.func, styles: PropTypes.object }; BlankDropTarget.defaultProps = { choices: undefined, choice: undefined, value: undefined, readOnly: false, onChange: undefined, onDrop: undefined, returnChoiceToWordbank: undefined }; export default withStyleOverrides(generateStyle, generateComponentTheme)(BlankDropTarget);