UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

174 lines (170 loc) • 6.54 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 { ScreenReaderContent } from '@instructure/ui-a11y-content'; import { ScientificNumberInput } from '@instructure/quiz-number-input'; import { Decimal } from '@instructure/quiz-i18n'; import { ItemBodyWrapper, RichContentRenderer } from '@instructure/quiz-rce'; import { View } from '@instructure/ui-view'; import t from '@instructure/quiz-i18n/es/format-message'; import { Text } from '@instructure/ui-text'; import { ApplyLocaleContext } from '@instructure/ui-i18n'; import { parseSeparators } from '@instructure/quiz-common'; /** --- category: Numeric --- Numeric Take 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', userResponse: { value: '18' } } return ( <NumericTake {...exampleProps} {...props} /> ) } <SettingsSwitcher locales={LOCALES}> <TakeStateProvider> <Example /> </TakeStateProvider> </SettingsSwitcher> ``` **/ var invalidMessage = { text: t('Answer must be a number. Remove any symbols or units.'), type: 'error' }; var NumericTake = /*#__PURE__*/function (_Component) { function NumericTake(props, context) { var _this2; _classCallCheck(this, NumericTake); _this2 = _callSuper(this, NumericTake, [props]); _defineProperty(_this2, "checkInvalid", function (value, normalized) { var warn = _this2.props.displayValidationWarning; if (warn && value && normalized == null) { _this2.setState({ messages: [invalidMessage] }); } }); _defineProperty(_this2, "checkInvalidOnce", function () { var checked = false; return function (value, normalized) { if (!checked) { checked = true; _this2.checkInvalid(value, normalized); if (_this2.props.displayValidationWarning && value && normalized == null) { var update = normalized == null ? value : normalized; _this2.props.handleResponseUpdate(update, null, true); } } }; }); _defineProperty(_this2, "setNormalized", function (normalized) { _this2.setState({ normalized: normalized }, function () { _this2.markInvalidOnMount(_this2.state.value, normalized); }); }); _defineProperty(_this2, "handleResponseChange", function (e, value, normalized) { _this2.setState({ value: value, normalized: normalized, messages: [] }); _this2.updateLogs(value, normalized); }); _defineProperty(_this2, "handleResponseBlur", function () { var _this2$state = _this2.state, value = _this2$state.value, normalized = _this2$state.normalized; _this2.checkInvalid(value, normalized); _this2.updateLogs(value, normalized); }); _defineProperty(_this2, "updateLogs", function (value, normalized) { // If the response isn't a number, call the handler with the raw response var invalid = _this2.props.displayValidationWarning && value && normalized == null; var update = normalized == null ? value : normalized; if (update !== _this2.props.userResponse.value) { _this2.props.handleResponseUpdate(update, null, invalid); } }); _this2.state = { value: Decimal.toLocaleStringIfValid(props.userResponse.value, context.locale) }; _this2.markInvalidOnMount = _this2.checkInvalidOnce(); return _this2; } _inherits(NumericTake, _Component); return _createClass(NumericTake, [{ key: "componentDidMount", value: function componentDidMount() { Decimal.accountSettingDelimiters = this.props.separatorConfig ? parseSeparators(this.props.separatorConfig) : null; } }, { key: "render", value: function render() { var itemBody = this.props.itemBody; return /*#__PURE__*/React.createElement(ItemBodyWrapper, { itemBody: itemBody }, /*#__PURE__*/React.createElement("div", { className: "fs-mask" }, /*#__PURE__*/React.createElement(ScientificNumberInput, { inputType: "text", messages: this.state.messages, renderLabel: /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Text, { "aria-hidden": true }, t('Answer')), /*#__PURE__*/React.createElement(ScreenReaderContent, null, /*#__PURE__*/React.createElement(RichContentRenderer, { content: this.props.itemBody }))), onChange: this.handleResponseChange, onBlur: this.handleResponseBlur, value: this.state.value, onInitialNormalization: this.setNormalized, autoComplete: "off" }))); } }]); }(Component); _defineProperty(NumericTake, "propTypes", { displayValidationWarning: PropTypes.bool, itemBody: PropTypes.string.isRequired, handleResponseUpdate: PropTypes.func.isRequired, userResponse: PropTypes.shape({ value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }), separatorConfig: PropTypes.shape({ decimalSeparator: PropTypes.string, thousandSeparator: PropTypes.string }) }); _defineProperty(NumericTake, "contextType", ApplyLocaleContext); _defineProperty(NumericTake, "defaultProps", { displayValidationWarning: false, userResponse: { value: null } }); export { NumericTake as default };