UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

243 lines (238 loc) • 8.27 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, _OrderingExample; 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 */ /* eslint-disable react/jsx-no-literals */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { cloneDeep } from 'lodash'; import { Heading } from '@instructure/ui-heading'; import { jsx } from '@instructure/emotion'; import OrderingEdit from '../Edit'; import OrderingShow from '../Show'; import OrderingTake from '../Take'; import OrderingResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: Ordering --- Ordering Example. ```jsx_example const WrappedExample = DragDropContext(HTML5Backend)(OrderingExample); <SettingsSwitcher locales={LOCALES}> <WrappedExample /> </SettingsSwitcher> ``` **/ var OrderingExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_OrderingExample = /*#__PURE__*/function (_Component) { function OrderingExample(props) { var _this2; _classCallCheck(this, OrderingExample); _this2 = _callSuper(this, OrderingExample); _defineProperty(_this2, "handleChangeItemState", function (itemState) { var newState = {}; if (itemState.scoringData) { newState.scoredData = _this2.transformScoredData(itemState.scoringData); newState.userResponse = _this2.transformScoredData(itemState.scoringData); } if (itemState.interactionData) { newState.takeInteractionData = _this2.transformTakeInteractionData(itemState.interactionData); } if (itemState.properties) { newState.properties = itemState.properties; } var updatedState = Object.assign({}, itemState, newState); _this2.setState(updatedState); }); _defineProperty(_this2, "handleResponseUpdate", function (userResponse) { _this2.setState({ userResponse: { value: userResponse, scoredData: _this2.transformScoredData(_this2.state.scoringData) } }); }); _this2.state = {}; if (props.useDefaultData) { Object.assign(_this2.state, { itemBody: 'Order these characters from tallest to shortest:', interactionData: { choices: { uuid6: { id: 'uuid6', itemBody: 'Gandalf' }, 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', 'uuid1'] }, scoringData: { value: ['uuid6', 'uuid5', 'uuid4', 'uuid3', 'uuid2', 'uuid1'] }, scoredData: {} }); _this2.state.scoredData = _this2.transformScoredData(_this2.state.scoringData); _this2.state.takeInteractionData = _this2.transformTakeInteractionData(_this2.state.interactionData); } return _this2; } /** * Normally, this should be calling a service */ _inherits(OrderingExample, _Component); return _createClass(OrderingExample, [{ key: "transformScoredData", value: function transformScoredData(scoringData) { var _this3 = this; return { correct: this.state.userResponse.value.join() === scoringData.value.join(), value: Object.keys(scoringData.value).map(function (currKey, i) { return { id: i, userResponded: _this3.state.userResponse.value[i], resultScore: scoringData.value[i] === _this3.state.userResponse.value[i] ? 1 : 0, value: scoringData.value[i] }; }) }; } /** * Normally, this should be calling a service */ }, { key: "transformTakeInteractionData", value: function transformTakeInteractionData(interactionData) { return cloneDeep(interactionData); } }, { key: "renderEdit", value: function renderEdit() { return jsx("div", { title: "edit" }, jsx(Heading, { level: "h2" }, "Edit"), jsx(OrderingEdit, { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, properties: this.state.properties, scoringData: this.state.scoringData })); } }, { key: "renderShow", value: function renderShow() { return jsx("div", { title: "show" }, jsx(Heading, { level: "h2" }, "Show"), jsx(OrderingShow, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, properties: this.state.properties, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return jsx("div", { title: "take" }, jsx(Heading, { level: "h2" }, "Take"), jsx(OrderingTake, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, properties: this.state.properties, handleResponseUpdate: this.handleResponseUpdate })); } }, { key: "renderResult", value: function renderResult() { return jsx("div", { title: "result" }, jsx(Heading, { level: "h2" }, "Result"), jsx(OrderingResult, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, properties: this.state.properties, scoredData: this.state.scoredData })); } }, { key: "render", value: function render() { return jsx("div", null, jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderEdit()), jsx("div", { css: this.props.styles.panel }, this.renderShow())), jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderTake()), jsx("div", { css: this.props.styles.panel }, this.renderResult()))); } }]); }(Component), _defineProperty(_OrderingExample, "displayName", 'OrderingExample'), _defineProperty(_OrderingExample, "componentId", "Quizzes".concat(_OrderingExample.displayName)), _defineProperty(_OrderingExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_OrderingExample, "defaultProps", { useDefaultData: true }), _OrderingExample)) || _class); export { OrderingExample as default };