UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

265 lines (260 loc) • 9.73 kB
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 _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } 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 _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } 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 _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } 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); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Decimal } from '@instructure/quiz-i18n'; import t from '@instructure/quiz-i18n/format-message'; import { ItemBodyWrapper } from '@instructure/quiz-rce'; import { FeedbackWrapper, NoResponse } from '@instructure/quiz-results-feedback'; import { parseSeparators } from '@instructure/quiz-common'; import { ApplyLocaleContext } from '@instructure/ui-i18n'; var NumericResult = /*#__PURE__*/ function(Component) { "use strict"; _inherits(NumericResult, Component); function NumericResult() { _class_call_check(this, NumericResult); return _call_super(this, NumericResult, arguments); } _create_class(NumericResult, [ { key: "componentDidMount", value: function componentDidMount() { Decimal.accountSettingDelimiters = this.props.separatorConfig ? parseSeparators(this.props.separatorConfig) : null; } }, { key: "locale", get: function get() { return this.props.locale || this.context.locale || 'en-US'; } }, { key: "correctAnswerString", value: function correctAnswerString() { var correctAnswer = (this.props.scoredData.value || {}).correctAnswer; if (Array.isArray(correctAnswer)) { var _correctAnswer = _sliced_to_array(correctAnswer, 2), minimum = _correctAnswer[0], maximum = _correctAnswer[1]; return t('{minimum} to {maximum} inclusive', { minimum: this.formatNumber(minimum), maximum: this.formatNumber(maximum) }); } else if (correctAnswer) { return this.formatNumber(correctAnswer); } return ''; } }, { key: "formatNumber", value: function formatNumber(value) { return isNaN(value) ? value : Decimal.toLocaleString(value.toString().trim(), this.locale); } }, { key: "render", value: function render() { var itemBody = this.props.itemBody; var _ref = this.props.scoredData.value || {}, resultScore = _ref.resultScore, userResponse = _ref.userResponse; var responseHidden = typeof userResponse === 'undefined'; var status; if (resultScore > 0) { status = 'correct'; } else if (resultScore <= 0) { status = 'incorrect'; } else { status = 'unknown'; } return /*#__PURE__*/ React.createElement(ItemBodyWrapper, { itemBody: itemBody }, userResponse == null ? /*#__PURE__*/ React.createElement(NoResponse, { correctAnswerLabel: this.correctAnswerString(), responseHidden: responseHidden, status: status }) : /*#__PURE__*/ React.createElement(FeedbackWrapper, { status: status, userResponse: this.formatNumber(userResponse), correctAnswer: this.correctAnswerString(), hiddenCorrectAnswerText: t('Correct answer: '), hiddenIncorrectAnswerText: t('Incorrect answer: ') })); } } ]); return NumericResult; }(Component); _define_property(NumericResult, "propTypes", { itemBody: PropTypes.string.isRequired, scoredData: PropTypes.shape({ value: PropTypes.shape({ userResponse: PropTypes.string, correctAnswer: PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string) ]), resultScore: PropTypes.number }) }).isRequired, locale: PropTypes.string, separatorConfig: PropTypes.shape({ decimalSeparator: PropTypes.string, thousandSeparator: PropTypes.string }) }); _define_property(NumericResult, "contextType", ApplyLocaleContext); _define_property(NumericResult, "defaultProps", { locale: void 0 }); /** --- category: Numeric --- Numeric Result component ```jsx_example function Example (props) { const exampleProps = { itemBody: 'x is an integer and 9 < x^2 < 99. What\'s the max value of x, minus the minimum value of x', scoredData: { value: { userResponse: '3.14159265', correctAnswer: '9999.9999', resultScore: 0 } } } return <NumericResult {...exampleProps} {...props} /> } <SettingsSwitcher locales={LOCALES}> <Example /> </SettingsSwitcher> ``` **/ export { NumericResult as default };