UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

142 lines (140 loc) 4.67 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"; 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 { ItemBodyWrapper } from '@instructure/quiz-rce'; import { ScreenReaderContent } from '@instructure/ui-a11y-content'; import { Decimal } from '@instructure/quiz-i18n'; import { TextInput, parseSeparators } from '@instructure/quiz-common'; import t from '@instructure/quiz-i18n/es/format-message'; /** --- category: Numeric --- Numeric Show component ```jsx_example <SettingsSwitcher locales={LOCALES}> <NumericShow itemBody="x is an integer and 9 < x^2 < 99. What's the max value of x, minus the minimum value of x" scoringData={{ value: [{ type: 'exactResponse', id: '1', value: '18' }, { type: 'withinARange', id: '2', start: '18', end: '18' }, { type: 'preciseResponse', id: '3', value: '18', precision: '1', precisionType: 'decimals' }, { type: 'marginOfError', id: '4', value: '18', margin: '0.4', marginType: 'absolute' }] }} /> </SettingsSwitcher> ``` **/ var NumericShow = /*#__PURE__*/function (_Component) { function NumericShow() { _classCallCheck(this, NumericShow); return _callSuper(this, NumericShow, arguments); } _inherits(NumericShow, _Component); return _createClass(NumericShow, [{ 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: "firstCriteria", get: function get() { var scoringData = this.props.scoringData; return scoringData && scoringData.value && scoringData.value[0] || {}; } }, { key: "value", get: function get() { var _this$firstCriteria = this.firstCriteria, type = _this$firstCriteria.type, value = _this$firstCriteria.value, start = _this$firstCriteria.start, end = _this$firstCriteria.end; if (type === 'withinARange') { return t('{minimum} to {maximum} inclusive', { minimum: this.formatNumber(start), maximum: this.formatNumber(end) }); } return this.formatNumber(value); } }, { key: "formatNumber", value: function formatNumber(value) { return isNaN(value) ? value : Decimal.toLocaleString(value.toString(), this.locale); } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement(ItemBodyWrapper, { itemBody: this.props.itemBody }, /*#__PURE__*/React.createElement(TextInput, { renderLabel: /*#__PURE__*/React.createElement(ScreenReaderContent, null, t('Answer value')), interaction: "readonly", tabIndex: -1, defaultValue: this.value })); } }]); }(Component); _defineProperty(NumericShow, "propTypes", { itemBody: PropTypes.string.isRequired, locale: PropTypes.string, scoringData: PropTypes.shape({ value: PropTypes.array }), separatorConfig: PropTypes.shape({ decimalSeparator: PropTypes.string, thousandSeparator: PropTypes.string }) }); _defineProperty(NumericShow, "contextTypes", { locale: PropTypes.string }); _defineProperty(NumericShow, "defaultProps", { locale: void 0, scoringData: void 0 }); export { NumericShow as default };