UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

140 lines 5.31 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 _inherits from "@babel/runtime/helpers/esm/inherits"; import _get from "@babel/runtime/helpers/esm/get"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; 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 from 'react'; import { rule, each } from 'instructure-validations'; import { isScientificNotation } from '@instructure/quiz-scientific-notation'; import { Text } from '@instructure/ui-text'; import { NUMERIC_SLUG } from '../../interaction_slugs'; import InteractionType from '../InteractionType'; import { numericMessage } from '../../util/validationHelpers'; import t from '@instructure/quiz-i18n/es/format-message'; var NumericInteractionType = /*#__PURE__*/function (_InteractionType) { function NumericInteractionType(obj) { var _this2; _classCallCheck(this, NumericInteractionType); _this2 = _callSuper(this, NumericInteractionType); _defineProperty(_this2, "slug", NUMERIC_SLUG); _defineProperty(_this2, "translatedName", t('Numeric')); _defineProperty(_this2, "getDefaultScoringData", function (intData) { return { value: [{ type: 'exactResponse', value: '', id: '1' }] }; }); _defineProperty(_this2, "getDefaultInteractionData", function () { return {}; }); _defineProperty(_this2, "getRenderedResponse", function (responseValue) { return /*#__PURE__*/React.createElement(Text, { color: "primary" }, responseValue); }); _get((_this2, _getPrototypeOf(NumericInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj); return _this2; } _inherits(NumericInteractionType, _InteractionType); return _createClass(NumericInteractionType, [{ key: "getDefaultUserResponse", value: function getDefaultUserResponse() { return { value: '' }; } }]); }(InteractionType); _defineProperty(NumericInteractionType, "validations", function (data) { var exactResponseValidations = { value: [rule('numeric', { message: numericMessage(t('Answer')) })] }; var scientificNotationRuleOptions = { message: t('Scientific notation not supported with margin of error') }; var marginOfErrorValidations = { value: [rule('noScientificNotation', scientificNotationRuleOptions), rule('numeric', { message: numericMessage(t('Answer')) })], margin: [rule('noScientificNotation', scientificNotationRuleOptions), rule('numeric', { message: numericMessage(t('Margin')) })] }; var withinARangeValidations = function withinARangeValidations(obj) { var startRuleOptions = { message: numericMessage(t('Range start')) }; var endRuleOptions = { message: numericMessage(t('Range end')), min: obj.start, minMessage: t('Range end must be greater than range start') }; var scientificNotationRuleOptions = { message: t('Must be in scientific notation') }; var start = []; var end = []; if (isScientificNotation(obj.end)) { start.push(rule('scientificNotation', scientificNotationRuleOptions)); } start.push(rule('numeric', startRuleOptions)); if (isScientificNotation(obj.start)) { end.push(rule('scientificNotation', scientificNotationRuleOptions)); } end.push(rule('numeric', endRuleOptions)); return { start: start, end: end }; }; var preciseResponseValidations = function preciseResponseValidations(obj) { var scientificNotationRuleOptions = { message: t('Scientific notation not supported with precise response') }; return { value: [rule('noScientificNotation', scientificNotationRuleOptions), rule('numeric', { message: numericMessage(t('Answer')) })], precision: [rule('noScientificNotation', scientificNotationRuleOptions), rule('numeric', { message: numericMessage(t('Precision')), type: obj.precisionType, value: obj.value })] }; }; var scoredDatumValidator = function scoredDatumValidator(obj) { return { exactResponse: exactResponseValidations, marginOfError: marginOfErrorValidations, withinARange: withinARangeValidations, preciseResponse: preciseResponseValidations }[obj.type]; }; return { scoringData: { value: [each(scoredDatumValidator)] } }; }); export { NumericInteractionType as default };