UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

161 lines (160 loc) • 6.07 kB
function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { SimpleSelect } from '@instructure/quiz-common/components/SimpleSelect/index'; import { Flex } from '@instructure/quiz-common/components/Flex/index'; import { Decimal } from '@instructure/quiz-i18n/util/Decimal'; import { ScientificNumberInput } from '@instructure/quiz-number-input/components/ScientificNumberInput'; import t from '@instructure/quiz-i18n/format-message'; import { ABSOLUTE, PERCENT } from './constants'; function MarginOfError(param) { var id = param.id, locale = param.locale, marginProp = param.margin, marginType = param.marginType, messages = param.messages, numericTypeSelect = param.numericTypeSelect, onChange = param.onChange, valueProp = param.value; var _useState = _sliced_to_array(useState(function() { if (!marginProp) return ''; try { return Decimal.toLocaleString(marginProp, locale); } catch (e) { return marginProp; } }), 2), margin = _useState[0], setMargin = _useState[1]; var _useState1 = _sliced_to_array(useState(function() { if (!valueProp) return ''; try { return Decimal.toLocaleString(valueProp, locale); } catch (e) { return valueProp; } }), 2), value = _useState1[0], setValue = _useState1[1]; var handleMarginChange = function(event, newMargin, normalized) { setMargin(newMargin); onChange(event, { id: id, margin: normalized }); }; var handleMarginTypeChange = function(event, param) { var value = param.value; onChange(event, { id: id, marginType: value }); }; var handleValueChange = function(event, newValue, normalized) { setValue(newValue); onChange(event, { id: id, value: normalized }); }; return /*#__PURE__*/ React.createElement(Flex, { alignItems: "start" }, /*#__PURE__*/ React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, numericTypeSelect), /*#__PURE__*/ React.createElement(Flex.Item, { shouldGrow: true, shouldShrink: true, margin: "0 xx-small" }, /*#__PURE__*/ React.createElement(ScientificNumberInput, { renderLabel: t('Answer'), locale: locale, messages: messages.value, onChange: handleValueChange, value: value, "aria-valuetext": "".concat(value, " ").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: locale, messages: messages.margin, min: 0, onChange: handleMarginChange, value: margin, "aria-valuetext": "".concat(margin, " ").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: handleMarginTypeChange, value: 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'))))); } 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 }; MarginOfError.defaultProps = { margin: void 0, messages: void 0, value: void 0 }; export default MarginOfError;