@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
248 lines (247 loc) • 10.7 kB
JavaScript
function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { v4 as uuid } from 'uuid';
import { SELECT_EMPTY_OPTION, SELECT_EMPTY_OPTION_TEXT, Select } from '@instructure/quiz-common';
var MatchSelect = /*#__PURE__*/ function(Component) {
"use strict";
_inherits(MatchSelect, Component);
function MatchSelect() {
_class_call_check(this, MatchSelect);
var _this;
_this = _call_super(this, MatchSelect, arguments), _define_property(_this, "state", {
inputValue: _this.props.selectedAnswers.value[_this.props.questionId] || '',
isShowingOptions: false,
highlightedOptionId: null,
filteredOptions: _this.props.options
}), _define_property(_this, "filterOptions", function(value) {
return _this.props.options.filter(function(option) {
return option.startsWith(value);
});
}), _define_property(_this, "handleShowOptions", function() {
_this.setState({
isShowingOptions: true
});
}), _define_property(_this, "handleHideOptions", function() {
var _this_state = _this.state, filteredOptions = _this_state.filteredOptions, inputValue = _this_state.inputValue;
var index = filteredOptions.findIndex(function(option) {
return option === inputValue;
});
_this.handleSelectOption(null, {
id: index === -1 ? undefined : _this.getIdFromText(filteredOptions[index])
});
}), _define_property(_this, "handleBlur", function() {
_this.setState({
highlightedOptionId: null
});
}), _define_property(_this, "handleHighlightOption", function(event, option) {
event.persist();
if (!option || !option.id) return; // prevent highlighting of empty option
_this.setState({
highlightedOptionId: option.id,
inputValue: event.type === 'keydown' ? _this.getTextFromId(option.id) : _this.state.inputValue
});
}), _define_property(_this, "handleSelectOption", function(event, option) {
if (!option || option.id === SELECT_EMPTY_OPTION) return;
var value = option.id ? _this.getTextFromId(option.id) : '';
_this.props.handleResponseUpdate(_object_spread_props(_object_spread({}, _this.props.selectedAnswers && _this.props.selectedAnswers.value), _define_property({}, _this.props.questionId, option && value)));
_this.setState({
inputValue: value,
isShowingOptions: false,
filteredOptions: _this.props.options
});
}), _define_property(_this, "handleInputChange", function(event) {
var value = event.target.value;
var newOptions = _this.filterOptions(value);
_this.setState({
isShowingOptions: true,
inputValue: value,
highlightedOptionId: newOptions.length > 0 ? _this.getIdFromText(newOptions[0]) : null
});
}), _define_property(_this, "getIdFromText", function(text) {
return btoa(encodeURIComponent(text));
}), _define_property(_this, "getTextFromId", function(id) {
return decodeURIComponent(atob(id));
});
return _this;
}
_create_class(MatchSelect, [
{
key: "render",
value: function render() {
var _this = this;
var _this_state = this.state, inputValue = _this_state.inputValue, isShowingOptions = _this_state.isShowingOptions, highlightedOptionId = _this_state.highlightedOptionId, filteredOptions = _this_state.filteredOptions;
var mountNodeId = "mountNode_".concat(uuid());
return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Select, {
interaction: this.props.interaction,
renderLabel: this.props.renderLabel,
inputValue: inputValue,
isShowingOptions: isShowingOptions,
onBlur: this.handleBlur,
optionsMaxHeight: "50vh",
onInputChange: this.handleInputChange,
onRequestShowOptions: this.handleShowOptions,
onRequestHideOptions: this.handleHideOptions,
onRequestHighlightOption: this.handleHighlightOption,
onRequestSelectOption: this.handleSelectOption,
mountNode: function() {
return document.getElementById(mountNodeId);
}
}, filteredOptions.length > 0 ? filteredOptions.map(function(option) {
var normalized = _this.getIdFromText(option);
return /*#__PURE__*/ React.createElement(Select.Option, {
id: normalized,
key: option,
isHighlighted: normalized === highlightedOptionId,
isSelected: option === _this.props.selectedAnswers.value[_this.props.questionId]
}, option);
}) : /*#__PURE__*/ React.createElement(Select.Option, {
id: SELECT_EMPTY_OPTION,
key: SELECT_EMPTY_OPTION
}, SELECT_EMPTY_OPTION_TEXT)), /*#__PURE__*/ React.createElement("div", {
id: mountNodeId
}));
}
}
]);
return MatchSelect;
}(Component);
_define_property(MatchSelect, "propTypes", {
questionId: PropTypes.string.isRequired,
options: PropTypes.arrayOf(PropTypes.string).isRequired,
handleResponseUpdate: PropTypes.func,
selectedAnswers: PropTypes.shape({
value: PropTypes.objectOf(PropTypes.string)
}).isRequired,
interaction: PropTypes.string,
renderLabel: PropTypes.element.isRequired
});
_define_property(MatchSelect, "defaultProps", {
handleResponseUpdate: void 0,
interaction: 'enabled'
});
export { MatchSelect as default };