UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

364 lines (359 loc) • 13.3 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"; var _dec, _class, _FileUploadTake; 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)); } /** @jsx jsx */ import { Component } from 'react'; import PropTypes from 'prop-types'; import assign from 'lodash/fp/assign'; import find from 'lodash/fp/find'; import map from 'lodash/fp/map'; import concat from 'lodash/fp/concat'; import sortBy from 'lodash/fp/sortBy'; import remove from 'lodash/fp/remove'; import uniqBy from 'lodash/fp/uniqBy'; import flow from 'lodash/fp/flow'; import { jsx } from '@instructure/emotion'; import { Text } from '@instructure/ui-text'; import { IconUploadLine } from '@instructure/ui-icons'; import { Alert } from '@instructure/ui-alerts'; import { ItemBodyWrapper } from '@instructure/quiz-rce'; import isImage from '../common/isImage'; import FocusGroup from '../../common/components/FocusGroup'; import FilesList from '../common/FilesList'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/es/format-message'; import { ScreenReaderContent } from '@instructure/ui-a11y-content'; import { withStyleOverrides, FileDrop } from '@instructure/quiz-common'; var FILEDROP_SELECTOR = 'input[type=file]'; var REMOVE_BUTTON_SELECTOR = '[data-role=removeButton] button'; /** --- category: FileUpload --- File Upload Take component ```jsx_example function Example (props) { const exampleProps = { itemBody: 'Give three examples of people', interactionData: { restrictCount: true, filesCount: 3 }, properties: { restrictTypes: false, allowedTypes: '' }, userResponse: { value: [{ id: Date.now(), name: 'slide-3.jpg', url: 'http://instructure.github.io/images/slide-3.jpg', size: 49067 }] } } return ( <FileUploadTake {...exampleProps} {...props} /> ) } <SettingsSwitcher locales={LOCALES}> <TakeStateProvider> <Example /> </TakeStateProvider> </SettingsSwitcher> ``` **/ var FileUploadTake = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_FileUploadTake = /*#__PURE__*/function (_Component) { function FileUploadTake() { var _this2; _classCallCheck(this, FileUploadTake); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, FileUploadTake, [].concat(args)); _defineProperty(_this2, "state", { files: [], messages: [] }); _defineProperty(_this2, "focusGroup", null); _defineProperty(_this2, "renderDescription", function () { return "".concat(_this2.fileCountA11yDescription(), " ").concat(_this2.renderAllowedTypesDescription() || ''); }); _defineProperty(_this2, "fileCountA11yDescription", function () { if (_this2.props.interactionData.restrictCount) { var fileResponseLength = _this2.allResponses().length; if (fileResponseLength === 0) return t('Maximum { fileCount } file(s) allowed', { fileCount: _this2.fileCountNumber() }); var requiredFilesCount = _this2.parseFilesCount(); return t('{ filesSubmitted } of a maximum of { filesRequired } file(s) submitted.', { filesRequired: requiredFilesCount, filesSubmitted: fileResponseLength }); } return t('{ fileCount } file(s) submitted.', { fileCount: _this2.fileCountNumber() }); }); _defineProperty(_this2, "fileCountNumber", function () { return _this2.props.interactionData.restrictCount ? _this2.parseFilesCount() - _this2.allResponses().length : _this2.allResponses().length; }); _defineProperty(_this2, "makeLocalLoadHandler", function (id) { return function (e) { var files = map(function (file) { if (file.id !== id) { return file; } return assign(file, { url: e.target.result }); }, _this2.state.files); _this2.setState({ files: files }); }; }); _defineProperty(_this2, "handleDropRejected", function (files) { _this2.setState({ messages: [{ text: t('Invalid file type'), type: 'error' }] }); }); _defineProperty(_this2, "handleDropAccepted", function (files) { if (_this2.props.readOnly) { return; } var uploadFiles; if (_this2.props.interactionData.restrictCount) { var responseLength = _this2.parseResponse().length; uploadFiles = files.slice(0, _this2.parseFilesCount() - responseLength); } else { uploadFiles = files; } var parsedFiles = uploadFiles.map(function (file, index) { var id = Date.now() + index; var callback = _this2.makeUploadCallback(id, file); _this2.props.mediaUpload(id, file, callback); if (isImage(file.name)) { var reader = new FileReader(); reader.onload = _this2.makeLocalLoadHandler(id); reader.readAsDataURL(file); } return { id: id, url: null, name: file.name, size: file.size, isUploading: true }; }); _this2.setState({ files: concat(_this2.state.files, parsedFiles), messages: [] }, function () { // After browsing & selecting 1 or more files, focus on the last one _this2.focusGroup.focusLast('div'); }); }); _defineProperty(_this2, "makeRemoveHandler", function (_ref) { var id = _ref.id; return function () { var stateFiles = _this2.state.files; var response = _this2.parseResponse(); if (stateFiles.length + response.length === 1) { // Focus on FileDrop _this2.focusGroup.focusNext(FILEDROP_SELECTOR); } else { // Focus on another remove button if (_this2.focusGroup.previousExists(REMOVE_BUTTON_SELECTOR)) { _this2.focusGroup.focusPrevious(REMOVE_BUTTON_SELECTOR); } else { _this2.focusGroup.focusNext(REMOVE_BUTTON_SELECTOR); } } if (find({ id: id }, stateFiles)) { _this2.props.cancelMediaUpload(id); _this2.setState({ files: remove({ id: id }, stateFiles) }); } else { _this2.props.handleResponseUpdate(remove({ id: id }, response)); } }; }); _defineProperty(_this2, "handleShifterRef", function (node) { _this2.focusGroup = node; }); _defineProperty(_this2, "allResponses", function () { return flow(concat(_this2.state.files), sortBy('id'), uniqBy('id'))(_this2.parseResponse()); }); return _this2; } _inherits(FileUploadTake, _Component); return _createClass(FileUploadTake, [{ key: "parseFilesCount", value: function parseFilesCount() { return parseInt(this.props.interactionData.filesCount, 10); } }, { key: "parseResponse", value: function parseResponse() { var userResponse = this.props.userResponse; if (!userResponse || !userResponse.value || !userResponse.value.length) { return []; } return userResponse.value.filter(function (f) { return !!f; }); } }, { key: "makeUploadCallback", value: function makeUploadCallback(id, _ref2) { var _this3 = this; var name = _ref2.name, size = _ref2.size; return function (url) { // First add the new file to userResponse and THEN delete from state // So that we never lose track of a file (causes lost focus issues) // This means that for one rerender we'll have a file both in state & userResponse var response = _this3.parseResponse(); _this3.props.handleResponseUpdate(concat(response, { id: id, url: url, name: name, size: size })); _this3.setState({ files: remove({ id: id }, _this3.state.files) }); }; } }, { key: "renderFileDropContent", value: function renderFileDropContent() { var _this4 = this; return jsx("div", { css: this.props.styles.fileDropContent }, jsx("div", { css: this.props.styles.fileDropContentIcon }, jsx(IconUploadLine, null)), jsx("div", { css: this.props.styles.fileDropContentLabel }, jsx(Text, { color: "primary" }, t.rich("Drag n' Drop here or <0>Browse</0>", [function (_ref3) { var children = _ref3.children; return jsx("span", { key: "1", css: _this4.props.styles.fileDropContentLabelBrowse }, children); }])))); } }, { key: "renderAllowedTypesDescription", value: function renderAllowedTypesDescription() { var _this$props$propertie = this.props.properties, restrictTypes = _this$props$propertie.restrictTypes, allowedTypes = _this$props$propertie.allowedTypes; return restrictTypes && allowedTypes ? t('{ types } only!', { types: allowedTypes }) : null; } }, { key: "render", value: function render() { var restrictCount = this.props.interactionData.restrictCount; var _this$props = this.props, itemBody = _this$props.itemBody, properties = _this$props.properties, readOnly = _this$props.readOnly; var restrictTypes = properties.restrictTypes, allowedTypes = properties.allowedTypes; var accept = restrictTypes ? allowedTypes : ''; // The uniqBy is needed because in one rerender there's a file in both state & userResponse var hideFileDrop = restrictCount && this.allResponses().length === this.parseFilesCount(); return jsx("div", null, jsx("div", { css: this.props.styles.printOnly }, jsx(Alert, { variant: "warning" }, t('This question type cannot be printed'))), jsx(FocusGroup, { ref: this.handleShifterRef, asComponent: ItemBodyWrapper, asProps: { itemBody: itemBody } }, jsx(FilesList, { files: this.allResponses(), makeRemoveHandler: this.makeRemoveHandler, readOnly: this.props.readOnly }), hideFileDrop ? null : jsx("div", null, jsx("div", null, jsx(FileDrop, { messages: this.state.messages, accept: accept, onDropAccepted: this.handleDropAccepted, onDropRejected: this.handleDropRejected, shouldAllowMultiple: true, readOnly: readOnly, renderLabel: this.renderFileDropContent() })), restrictCount ? jsx("div", null, jsx(Alert, { variant: "info", margin: "x-small 0", hasShadow: false }, this.renderDescription())) : null)), jsx(ScreenReaderContent, null, this.renderDescription())); } }]); }(Component), _defineProperty(_FileUploadTake, "displayName", 'FileUploadTake'), _defineProperty(_FileUploadTake, "componentId", "Quizzes".concat(_FileUploadTake.displayName)), _defineProperty(_FileUploadTake, "propTypes", { itemBody: PropTypes.string.isRequired, readOnly: PropTypes.bool, handleResponseUpdate: PropTypes.func.isRequired, mediaUpload: PropTypes.func.isRequired, cancelMediaUpload: PropTypes.func, interactionData: PropTypes.shape({ restrictCount: PropTypes.bool.isRequired, filesCount: PropTypes.string.isRequired }).isRequired, properties: PropTypes.shape({ restrictTypes: PropTypes.bool.isRequired, allowedTypes: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]) }).isRequired, userResponse: PropTypes.shape({ value: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.number.isRequired, name: PropTypes.string.isRequired, url: PropTypes.string.isRequired, size: PropTypes.number.isRequired })) }).isRequired, styles: PropTypes.object }), _defineProperty(_FileUploadTake, "defaultProps", { cancelMediaUpload: function cancelMediaUpload() {}, readOnly: false }), _FileUploadTake)) || _class); export { FileUploadTake as default };