UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

164 lines 6.07 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 { SimpleSelect, Flex } from '@instructure/quiz-common'; import { Decimal } from '@instructure/quiz-i18n'; import { ScientificNumberInput } from '@instructure/quiz-number-input'; import t from '@instructure/quiz-i18n/es/format-message'; import { ABSOLUTE, PERCENT } from './constants'; var MarginOfError = /*#__PURE__*/function (_Component) { function MarginOfError() { var _this2; _classCallCheck(this, MarginOfError); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, MarginOfError, [].concat(args)); _defineProperty(_this2, "state", { margin: '', value: '' }); _defineProperty(_this2, "handleMarginChange", function (event, value, normalized) { _this2.setState({ margin: value }); _this2.props.onChange(event, { id: _this2.props.id, margin: normalized }); }); _defineProperty(_this2, "handleMarginTypeChange", function (event, _ref) { var value = _ref.value; _this2.props.onChange(event, { id: _this2.props.id, marginType: value }); }); _defineProperty(_this2, "handleValueChange", function (event, value, normalized) { _this2.setState({ value: value }); _this2.props.onChange(event, { id: _this2.props.id, value: normalized }); }); return _this2; } _inherits(MarginOfError, _Component); return _createClass(MarginOfError, [{ key: "UNSAFE_componentWillMount", value: function UNSAFE_componentWillMount() { if (this.props.margin) { var margin; try { margin = Decimal.toLocaleString(this.props.margin, this.props.locale); } catch (_unused) { margin = this.props.margin; } this.setState({ margin: margin }); } if (this.props.value) { var value; try { value = Decimal.toLocaleString(this.props.value, this.props.locale); } catch (_unused2) { value = this.props.value; } this.setState({ value: value }); } } }, { key: "render", value: function render() { var currentLocaleValue = this.state.value; var currentMarginValue = this.state.margin; return /*#__PURE__*/React.createElement(Flex, { alignItems: "start" }, /*#__PURE__*/React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, this.props.numericTypeSelect), /*#__PURE__*/React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, /*#__PURE__*/React.createElement(ScientificNumberInput, { renderLabel: t('Answer'), locale: this.props.locale, messages: this.props.messages.value, onChange: this.handleValueChange, value: currentLocaleValue, "aria-valuetext": "".concat(currentLocaleValue, " ").concat(t('Answer')), "data-automation": "sdk-numeric-marginoferror-response-number-input", isRequired: true })), /*#__PURE__*/React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, /*#__PURE__*/React.createElement(ScientificNumberInput, { renderLabel: t('+/- margin'), locale: this.props.locale, messages: this.props.messages.margin, min: 0, onChange: this.handleMarginChange, value: currentMarginValue, "aria-valuetext": "".concat(currentMarginValue, " ").concat(t('+/- margin')), isRequired: true })), /*#__PURE__*/React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, /*#__PURE__*/React.createElement(SimpleSelect, { renderLabel: t('Type'), onChange: this.handleMarginTypeChange, value: this.props.marginType }, /*#__PURE__*/React.createElement(SimpleSelect.Option, { id: "margin-of-error-select-option-percent", value: PERCENT }, t('Percent')), /*#__PURE__*/React.createElement(SimpleSelect.Option, { id: "margin-of-error-select-option-absolute", value: ABSOLUTE }, t('Absolute'))))); } }]); }(Component); _defineProperty(MarginOfError, "propTypes", { id: PropTypes.string.isRequired, locale: PropTypes.string.isRequired, margin: PropTypes.string, marginType: PropTypes.oneOf([PERCENT, ABSOLUTE]).isRequired, messages: PropTypes.objectOf(PropTypes.array), numericTypeSelect: PropTypes.node.isRequired, onChange: PropTypes.func.isRequired, value: PropTypes.string }); _defineProperty(MarginOfError, "defaultProps", { margin: void 0, messages: void 0, value: void 0 }); export { MarginOfError as default };