@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
150 lines (145 loc) • 5.3 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
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 { Decimal } from '@instructure/quiz-i18n';
import t from '@instructure/quiz-i18n/es/format-message';
import { ItemBodyWrapper } from '@instructure/quiz-rce';
import { FeedbackWrapper, NoResponse } from '@instructure/quiz-results-feedback';
import { parseSeparators } from '@instructure/quiz-common';
/**
---
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>
```
**/
var NumericResult = /*#__PURE__*/function (_Component) {
function NumericResult() {
_classCallCheck(this, NumericResult);
return _callSuper(this, NumericResult, arguments);
}
_inherits(NumericResult, _Component);
return _createClass(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 _ref = this.props.scoredData.value || {},
correctAnswer = _ref.correctAnswer;
if (Array.isArray(correctAnswer)) {
var _correctAnswer = _slicedToArray(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 _ref2 = this.props.scoredData.value || {},
resultScore = _ref2.resultScore,
userResponse = _ref2.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: ')
}));
}
}]);
}(Component);
_defineProperty(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
})
});
_defineProperty(NumericResult, "contextTypes", {
locale: PropTypes.string
});
_defineProperty(NumericResult, "defaultProps", {
locale: void 0
});
export { NumericResult as default };