UNPKG

wix-style-react

Version:
120 lines (99 loc) 4.61 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; 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 { dataHooks } from './constants'; /** A wrapper component to support native file upload */ var FileUpload = /*#__PURE__*/function (_React$PureComponent) { _inherits(FileUpload, _React$PureComponent); var _super = _createSuper(FileUpload); function FileUpload(props) { var _this; _classCallCheck(this, FileUpload); _this = _super.call(this, props); _this.inputRef = /*#__PURE__*/React.createRef(); return _this; } _createClass(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.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.createElement("input", { type: "file", "data-hook": 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.PureComponent); FileUpload.displayName = 'FileUpload'; FileUpload.propTypes = { /** Applies a data-hook HTML attribute that can be used in tests. */ dataHook: PropTypes.string, /** Allows the application of a CSS class to the components’ root element. */ className: PropTypes.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.func.isRequired, /** Allows you to select and upload multiple files at once. */ multiple: PropTypes.bool, /** Defines which file types to accept (i.e. .jpeg, .gif, .png). Each format should be separated by a comma. */ accept: PropTypes.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.oneOf(['user', 'environment', 'none']), /** Specifies a form data name to be submitted along with the control value. */ name: PropTypes.string, /** Defines a standard input onChange callback. */ onChange: PropTypes.func.isRequired }; FileUpload.defaultProps = { multiple: false, accept: '', capture: 'user', onChange: function onChange() {} }; export default FileUpload;