UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

167 lines (165 loc) 6.16 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, _MatchingResult; 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 find from 'lodash/fp/find'; import { Text } from '@instructure/ui-text'; import { jsx } from '@instructure/emotion'; import t from '@instructure/quiz-i18n/es/format-message'; import { ItemBodyWrapper } from '@instructure/quiz-rce'; import { FeedbackWrapper, NoResponse } from '@instructure/quiz-results-feedback'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: Matching --- Matching Result component ```jsx_example <SettingsSwitcher locales={LOCALES}> <MatchingResult itemBody="Match the name of the celestial body on the left with its correct classification." interactionData={{ questions: [ { id: 'quuid1', itemBody: 'Mars' }, { id: 'quuid2', itemBody: 'Phobos' }, { id: 'quuid3', itemBody: 'Andromeda' }, { id: 'quuid4', itemBody: 'Alpha Centauri' } ], answers: [ 'Planet', 'Moon', 'Nebula', 'Star', 'Galaxy - this is a very long answer - this is a very long answer - this is a very long answer' ] }} scoredData={{ value: [{ id: 'quuid1', userResponded: 'Planet', value: 'Planet' }, { id: 'quuid2', userResponded: 'Moon', value: 'Moon' }, { id: 'quuid3', userResponded: 'Nebula', value: 'Nebula' }, { id: 'quuid4', userResponded: 'Star', value: 'GalaxyGalaxy - this is a very long answer - this is a very long answer - this is a very long answer' }] }} /> </SettingsSwitcher> ``` **/ var MatchingResult = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_MatchingResult = /*#__PURE__*/function (_Component) { function MatchingResult() { _classCallCheck(this, MatchingResult); return _callSuper(this, MatchingResult, arguments); } _inherits(MatchingResult, _Component); return _createClass(MatchingResult, [{ key: "getAnswerBodyForQuestion", value: function getAnswerBodyForQuestion(questionId) { var answer = find({ id: String(questionId) }, this.props.scoredData.value || {}); return answer && answer.value; } }, { key: "renderAnswer", value: function renderAnswer(response, answerBody) { var correctnessKnown = typeof this.props.scoredData.correct !== 'undefined'; var status = 'incorrect'; if (response && response.resultScore) { status = 'correct'; } else if ((!response || !response.resultScore) && correctnessKnown) { status = 'incorrect'; } else { status = 'unknown'; } return jsx("div", { css: this.props.styles.answer }, response && response.userResponded ? jsx(FeedbackWrapper, { userResponse: response.userResponded, correctAnswer: answerBody, status: status, hiddenCorrectAnswerText: t('Correct match: '), hiddenIncorrectAnswerText: t('Incorrect match: ') }) : jsx(NoResponse, { correctAnswerLabel: answerBody, responseHidden: !this.props.scoredData.value, status: status })); } }, { key: "render", value: function render() { var _this2 = this; return jsx(ItemBodyWrapper, { itemBody: this.props.itemBody }, jsx("div", null, this.props.interactionData.questions.map(function (question) { var response = find({ id: String(question.id) }, _this2.props.scoredData.value || {}); var answerBody = _this2.getAnswerBodyForQuestion(question.id); return jsx("div", { css: _this2.props.styles.item, key: question.id }, jsx("div", { css: _this2.props.styles.question, "data-testid": "question" }, jsx(Text, { color: "primary" }, question.itemBody)), jsx("div", { css: _this2.props.styles.divider }), _this2.renderAnswer(response, answerBody)); }))); } }]); }(Component), _defineProperty(_MatchingResult, "displayName", 'MatchingResult'), _defineProperty(_MatchingResult, "componentId", "Quizzes".concat(_MatchingResult.displayName)), _defineProperty(_MatchingResult, "propTypes", { interactionData: PropTypes.shape({ questions: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.string, itemBody: PropTypes.string })) }).isRequired, itemBody: PropTypes.string.isRequired, scoredData: PropTypes.shape({ correct: PropTypes.bool, value: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.string.isRequired, resultScore: PropTypes.number, userResponded: PropTypes.string, value: PropTypes.string })) }).isRequired, styles: PropTypes.object }), _MatchingResult)) || _class); export default MatchingResult;