UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

160 lines (155 loc) 6.1 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"; 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)); } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import striptags from 'striptags'; import findIndex from 'lodash/findIndex'; import { ItemBodyWrapper } from '@instructure/quiz-rce'; import OrderGroup from '../common/OrderGroup'; import ReorderChoiceButton from '../common/ReorderChoiceButton'; import t from '@instructure/quiz-i18n/es/format-message'; /** --- category: Ordering --- Ordering Take component ```jsx_example const WrappedExample = DragDropContext(HTML5Backend)(OrderingTake) function Example () { const props = { itemBody: 'Order these characters from tallest to shortest:', interactionData: { choices: { uuid6: { id: 'uuid6', itemBody: 'Gandalf the Grey or Gandalf the White' }, uuid5: { id: 'uuid5', itemBody: 'Legolas' }, uuid4: { id: 'uuid4', itemBody: 'Aragorn' }, uuid3: { id: 'uuid3', itemBody: 'Gimli' }, uuid2: { id: 'uuid2', itemBody: 'Frodo' }, uuid1: { id: 'uuid1', itemBody: 'Gollum' }, } }, properties: { displayAnswersParagraph: true, includeLabels: true, topLabel: 'Taller', bottomLabel: 'Shorter' }, userResponse: { value: ['uuid5', 'uuid6', 'uuid4', 'uuid3', 'uuid2'] } } return ( <TakeStateProvider> <WrappedExample {...props} /> </TakeStateProvider> ) } <SettingsSwitcher locales={LOCALES}> <Example /> </SettingsSwitcher> ``` **/ var OrderingTake = /*#__PURE__*/function (_Component) { function OrderingTake() { var _this2; _classCallCheck(this, OrderingTake); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, OrderingTake, [].concat(args)); _defineProperty(_this2, "handleMoveChoice", function (currI, targetI) { // Create a new array to preserve immutability var choices = _this2.props.userResponse.value.slice(0); // remove currI value from choices and insert it at the targetI choices.splice(targetI, 0, choices.splice(currI, 1)[0]); _this2.props.handleResponseUpdate(choices); }); _defineProperty(_this2, "handleMoveChoiceUp", function (choiceId) { var position = findIndex(_this2.props.userResponse.value, function (i) { return i === choiceId; }); _this2.handleMoveChoice(position, position - 1); }); _defineProperty(_this2, "handleMoveChoiceDown", function (choiceId) { var position = findIndex(_this2.props.userResponse.value, function (i) { return i === choiceId; }); _this2.handleMoveChoice(position, position + 1); }); _defineProperty(_this2, "renderActionsContent", function (choiceId, isFinalChoice, isFirstChoice, choicePosition) { return /*#__PURE__*/React.createElement(ReorderChoiceButton, { buttonSize: "small", id: choiceId.split('_')[0], isFinalChoice: isFinalChoice, isFirstChoice: isFirstChoice, onMoveChoiceUp: _this2.handleMoveChoiceUp, onMoveChoiceDown: _this2.handleMoveChoiceDown, screenReaderText: t('Position {position, number}. Reorder Choice: {choice}', { position: choicePosition, choice: striptags(_this2.props.interactionData.choices[choiceId.split('_')[0]].itemBody) }) }); }); return _this2; } _inherits(OrderingTake, _Component); return _createClass(OrderingTake, [{ key: "render", value: function render() { var _this$props = this.props, userResponse = _this$props.userResponse, interactionData = _this$props.interactionData, properties = _this$props.properties; return /*#__PURE__*/React.createElement(ItemBodyWrapper, { itemBody: this.props.itemBody }, /*#__PURE__*/React.createElement(OrderGroup, { actionsContent: this.renderActionsContent, bottomLabel: properties.bottomLabel, choiceOrder: userResponse.value, choices: interactionData.choices, displayAnswersParagraph: properties.displayAnswersParagraph, includeLabels: properties.includeLabels, onMoveChoice: this.handleMoveChoice, topLabel: properties.topLabel })); } }]); }(Component); _defineProperty(OrderingTake, "propTypes", { handleResponseUpdate: PropTypes.func.isRequired, interactionData: PropTypes.shape({ choices: PropTypes.objectOf(PropTypes.shape({ id: PropTypes.string.isRequired, itemBody: PropTypes.string.isRequired })).isRequired }).isRequired, itemBody: PropTypes.string.isRequired, properties: PropTypes.shape({ displayAnswersParagraph: PropTypes.bool, includeLabels: PropTypes.bool, topLabel: PropTypes.string, bottomLabel: PropTypes.string }).isRequired, userResponse: PropTypes.shape({ value: PropTypes.arrayOf(PropTypes.string).isRequired }).isRequired }); export { OrderingTake as default };