UNPKG

wix-style-react

Version:
134 lines (106 loc) 5.29 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _constants = require("./constants"); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(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; } } /** A wrapper component to support native file upload */ var FileUpload = /*#__PURE__*/function (_React$PureComponent) { (0, _inherits2["default"])(FileUpload, _React$PureComponent); var _super = _createSuper(FileUpload); function FileUpload(props) { var _this; (0, _classCallCheck2["default"])(this, FileUpload); _this = _super.call(this, props); _this.inputRef = /*#__PURE__*/_react["default"].createRef(); return _this; } (0, _createClass2["default"])(FileUpload, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, dataHook = _this$props.dataHook, className = _this$props.className, children = _this$props.children, multiple = _this$props.multiple, accept = _this$props.accept, capture = _this$props.capture, name = _this$props.name, _onChange = _this$props.onChange; return /*#__PURE__*/_react["default"].createElement("label", { className: className, "data-hook": dataHook }, children({ openFileUploadDialog: function openFileUploadDialog(event) { // Open the file upload dialog _this2.inputRef.current.click(); // In case the click event comes from a non-button element if (event) { event.preventDefault(); } } }), /*#__PURE__*/_react["default"].createElement("input", { type: "file", "data-hook": _constants.dataHooks.input, style: { display: 'none' }, ref: this.inputRef, multiple: multiple, accept: accept, capture: capture === 'none' ? null : capture, name: name, onChange: function onChange(event) { return _onChange(event.target.files); } })); } }]); return FileUpload; }(_react["default"].PureComponent); FileUpload.displayName = 'FileUpload'; FileUpload.propTypes = { /** Applies a data-hook HTML attribute that can be used in tests. */ dataHook: _propTypes["default"].string, /** Allows the application of a CSS class to the components’ root element. */ className: _propTypes["default"].string, /** Accepts any kind of component as a child item that calls out the file picker. * ##### Syntax: * function({ openFileUpload }): element * * `openFileUpload`: A function that will open a file upload dialog when triggered. * * return: A react element */ children: _propTypes["default"].func.isRequired, /** Allows you to select and upload multiple files at once. */ multiple: _propTypes["default"].bool, /** Defines which file types to accept (i.e. .jpeg, .gif, .png). Each format should be separated by a comma. */ accept: _propTypes["default"].string, /** Specifies that a new file should be captured and which camera to use to capture the image or video data. * `Use user` for the user-facing camera and `environment` for the outward-facing camera. * ##### Note: This only works on mobile devices; * If your device is a desktop computer, you'll get a standard file picker. * */ capture: _propTypes["default"].oneOf(['user', 'environment', 'none']), /** Specifies a form data name to be submitted along with the control value. */ name: _propTypes["default"].string, /** Defines a standard input onChange callback. */ onChange: _propTypes["default"].func.isRequired }; FileUpload.defaultProps = { multiple: false, accept: '', capture: 'user', onChange: function onChange() {} }; var _default = FileUpload; exports["default"] = _default;