wix-style-react
Version:
176 lines (138 loc) • 6.68 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import { buildChildrenObject } from "wix-ui-core/dist/es/src/utils";
import { dataHooks } from './constants';
import { st, classes } from './Dropzone.st.css';
/** Defines a region in the page where files can be dropped */
var Dropzone = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Dropzone, _React$PureComponent);
var _super = _createSuper(Dropzone);
function Dropzone() {
var _this;
_classCallCheck(this, Dropzone);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
isDragActive: false
});
_defineProperty(_assertThisInitialized(_this), "_dragEventCounter", 0);
_defineProperty(_assertThisInitialized(_this), "_eventHasFiles", function (event) {
/** DataTransfer object is defined here: https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer */
return event.dataTransfer ? Array.from(event.dataTransfer.items).some(function (item) {
return item.kind === 'file';
}) : !!(event.target && event.target.files);
});
_defineProperty(_assertThisInitialized(_this), "_onDragEnter", function (event) {
Dropzone._overrideEventDefaults(event);
_this._dragEventCounter++;
/** We only want to show the overlay when files are dragged over the dropzone */
return _this._eventHasFiles(event) && _this.setState({
isDragActive: true
});
});
_defineProperty(_assertThisInitialized(_this), "_onDragLeave", function (event) {
Dropzone._overrideEventDefaults(event);
_this._dragEventCounter--;
return _this._dragEventCounter === 0 && _this.setState({
isDragActive: false
});
});
_defineProperty(_assertThisInitialized(_this), "_onDrop", function (event) {
Dropzone._overrideEventDefaults(event);
_this._dragEventCounter = 0;
if (_this._eventHasFiles(event)) {
var files = event.dataTransfer ? Array.from(event.dataTransfer.items).map(function (item) {
return item.getAsFile();
}) : event.target.files;
_this.setState({
isDragActive: false
});
return _this.props.onDrop(files);
}
});
return _this;
}
_createClass(Dropzone, [{
key: "render",
value: function render() {
var _this$props = this.props,
children = _this$props.children,
dataHook = _this$props.dataHook,
className = _this$props.className;
var isDragActive = this.state.isDragActive;
var childrenObj = buildChildrenObject(children, {
Content: null,
Overlay: null
});
return /*#__PURE__*/React.createElement("div", {
"data-hook": dataHook,
className: st(classes.root, className),
onDrop: this._onDrop,
onDragEnter: this._onDragEnter,
onDragLeave: this._onDragLeave,
onDragOver: Dropzone._overrideEventDefaults
}, isDragActive && childrenObj.Overlay, childrenObj.Content);
}
}]);
return Dropzone;
}(React.PureComponent);
_defineProperty(Dropzone, "displayName", 'Dropzone');
_defineProperty(Dropzone, "propTypes", {
/** Applies a data-hook HTML attribute that can be used in the tests. */
dataHook: PropTypes.string,
/** Specifies a CSS class name to be appended to the component’s root element. */
className: PropTypes.string,
/** Defines an event handler which is called when files are dropped over the dropzone. Dropped files are supplied as an argument to the function. */
onDrop: PropTypes.func.isRequired,
/** Accepts `<Dropzone.Overlay />` or `<Dropzone.Content />`. Both of them can contain any required content. */
children: function children(props, propName) {
var childrenArr = React.Children.toArray(props[propName]);
var childrenObj = buildChildrenObject(childrenArr, {
Overlay: null,
Content: null
});
if (!childrenObj.Content) {
return new Error('Invalid children provided, <Dropzone.Content /> must be provided');
}
if (!childrenObj.Overlay) {
return new Error('Invalid children provided, <Dropzone.Overlay /> must be provided');
}
return childrenArr.reduce(function (err, child) {
if (!err && child.type.displayName !== 'Dropzone.Content' && child.type.displayName !== 'Dropzone.Overlay') {
return new Error("Invalid children provided, unknown child <".concat(child.type.displayName || child.type, " /> supplied"));
}
return err;
}, false);
}
});
_defineProperty(Dropzone, "Overlay", function (_ref) {
var children = _ref.children;
return /*#__PURE__*/React.createElement("div", {
"data-hook": dataHooks.dropzoneOverlay,
className: classes.dropzoneOverlay
}, children);
});
_defineProperty(Dropzone, "Content", function (_ref2) {
var children = _ref2.children;
return /*#__PURE__*/React.createElement("div", {
"data-hook": dataHooks.dropzoneContent
}, children);
});
_defineProperty(Dropzone, "_overrideEventDefaults", function (event) {
event.preventDefault();
event.stopPropagation();
});
Dropzone.Content.displayName = 'Dropzone.Content';
Dropzone.Overlay.displayName = 'Dropzone.Overlay';
export default Dropzone;