UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

84 lines (83 loc) 3.33 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 from 'react'; import PropTypes from 'prop-types'; import { ScientificNumberInput } from '@instructure/quiz-number-input'; import { omitProps } from '@instructure/ui-react-utils'; // Wraps ScientificNumberInput, replacing the onChange and onBlur // props with a single onUpdate handler that only fires on blur. var VariableInput = /*#__PURE__*/function (_React$Component) { function VariableInput(props) { var _this2; _classCallCheck(this, VariableInput); _this2 = _callSuper(this, VariableInput, [props]); _defineProperty(_this2, "handleBlur", function () { if (_this2.state.normalized === null) { _this2.setState(_this2.stateFromProps()); } else if (_this2.state.normalized != _this2.props.value) { // intentional double-equals _this2.props.onUpdate(_this2.state.normalized); } }); _defineProperty(_this2, "handleChange", function (event, value, normalized) { _this2.setState({ value: value, normalized: normalized }); }); _this2.state = _this2.stateFromProps(props); return _this2; } _inherits(VariableInput, _React$Component); return _createClass(VariableInput, [{ key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { this.setState(this.stateFromProps(nextProps)); } }, { key: "stateFromProps", value: function stateFromProps() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; return { value: props.value, normalized: props.value }; } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement(ScientificNumberInput, Object.assign({}, omitProps(this.props, this.constructor.propTypes), { onBlur: this.handleBlur, onChange: this.handleChange, showArrows: false, value: this.state.value })); } }]); }(React.Component); _defineProperty(VariableInput, "propTypes", { onUpdate: PropTypes.func.isRequired, value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }); _defineProperty(VariableInput, "defaultProps", { value: void 0 }); export { VariableInput as default };