UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

226 lines (224 loc) • 7.52 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, _MatchingExample; 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 isEqual from 'lodash/isEqual'; import { Heading } from '@instructure/ui-heading'; import { jsx } from '@instructure/emotion'; import MatchingEdit from '../Edit'; import MatchingShow from '../Show'; import MatchingTake from '../Take'; import MatchingResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: Matching --- Matching Example. ```jsx_example <SettingsSwitcher locales={LOCALES}> <MatchingExample /> </SettingsSwitcher> ``` **/ var MatchingExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_MatchingExample = /*#__PURE__*/function (_Component) { function MatchingExample(props) { var _this2; _classCallCheck(this, MatchingExample); _this2 = _callSuper(this, MatchingExample); _defineProperty(_this2, "handleChangeItemState", function (itemState) { var newState = Object.assign({}, itemState, itemState.scoringData ? { scoredData: _this2.transformScoredData(itemState.scoringData) } : null); _this2.setState(newState); }); _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, { interactionData: { questions: [{ id: 'uuid1', itemBody: 'Condi Rice' }, { id: 'uuid2', itemBody: 'Alexander Haig' }, { id: 'uuid3', itemBody: 'John Kerry' }], answers: ['George W. Bush', 'Ronald Reagan', 'Barack Obama', 'George H.W. Bush', 'Bill Clinton'] }, itemBody: 'Match the Secretary of State with the President they served under.', properties: { shuffleRules: { questions: { shuffled: true } } }, scoringData: { value: { uuid1: 'George W. Bush', uuid2: 'Ronald Reagan', uuid3: 'Barack Obama' } }, scoredData: { value: [{ id: 'uuid1', userResponded: 'Condi Rice', value: 'Condi Rice' }, { id: 'uuid2', userResponded: 'Alexander Haig', value: 'Alexander Haig' }, { id: 'uuid3', userResponded: 'John Kerry', value: 'John Kerry' }] }, userResponse: { value: {} } }); } return _this2; } _inherits(MatchingExample, _Component); return _createClass(MatchingExample, [{ key: "componentDidMount", value: function componentDidMount() { this.setState({ scoredData: this.transformScoredData(this.state.scoringData) }); } }, { key: "transformScoredData", value: function transformScoredData(scoringData) { var _this3 = this; return { correct: isEqual(this.state.userResponse.value, scoringData.value), value: Object.keys(scoringData.value).map(function (currKey) { return { id: currKey, userResponded: _this3.state.userResponse.value[currKey], resultScore: Object.keys(scoringData.value).includes(currKey) ? 1 : 0, value: scoringData.value[currKey] }; }) }; } }, { key: "renderEdit", value: function renderEdit() { return jsx("div", { title: "edit" }, jsx(Heading, { level: "h2" }, "Edit"), jsx(MatchingEdit, { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData, properties: this.state.properties, errors: {} })); } }, { key: "renderShow", value: function renderShow() { return jsx("div", { title: "show" }, jsx(Heading, { level: "h2" }, "Show"), jsx(MatchingShow, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return jsx("div", { title: "take" }, jsx(Heading, { level: "h2" }, "Take"), jsx(MatchingTake, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, handleResponseUpdate: this.handleResponseUpdate })); } }, { key: "renderResult", value: function renderResult() { return jsx("div", { title: "result" }, jsx(Heading, { level: "h2" }, "Result"), jsx(MatchingResult, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, 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(_MatchingExample, "displayName", 'MatchingExample'), _defineProperty(_MatchingExample, "componentId", "Quizzes".concat(_MatchingExample.displayName)), _defineProperty(_MatchingExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_MatchingExample, "defaultProps", { useDefaultData: true }), _MatchingExample)) || _class); export { MatchingExample as default };