UNPKG

rsuite

Version:

A suite of react components

164 lines (133 loc) 5.43 kB
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import Ripple from '../Ripple'; import FormattedMessage from '../IntlProvider/FormattedMessage'; import { getUnhandledProps, defaultProps, prefix } from '../utils'; var Button = function Button(props) { return React.createElement("button", _extends({}, props, { type: "button" })); }; var UploadTrigger = /*#__PURE__*/ function (_React$Component) { _inheritsLoose(UploadTrigger, _React$Component); function UploadTrigger(props) { var _this; _this = _React$Component.call(this, props) || this; _this.inputRef = void 0; _this.handleClick = function () { !_this.props.disabled && _this.inputRef.current.click(); }; _this.handleDragEnter = function (event) { var _this$props$onDragEnt, _this$props; if (_this.props.dragable) { event.preventDefault(); _this.setState({ dragOver: true }); } (_this$props$onDragEnt = (_this$props = _this.props).onDragEnter) === null || _this$props$onDragEnt === void 0 ? void 0 : _this$props$onDragEnt.call(_this$props, event); }; _this.handleDragLeave = function (event) { var _this$props$onDragLea, _this$props2; if (_this.props.dragable) { event.preventDefault(); _this.setState({ dragOver: false }); } (_this$props$onDragLea = (_this$props2 = _this.props).onDragLeave) === null || _this$props$onDragLea === void 0 ? void 0 : _this$props$onDragLea.call(_this$props2, event); }; _this.handleDragOver = function (event) { var _this$props$onDragOve, _this$props3; _this.props.dragable && event.preventDefault(); (_this$props$onDragOve = (_this$props3 = _this.props).onDragOver) === null || _this$props$onDragOve === void 0 ? void 0 : _this$props$onDragOve.call(_this$props3, event); }; _this.handleDrop = function (event) { var _this$props$onDrop, _this$props5; if (_this.props.dragable) { var _this$props$onChange, _this$props4; event.preventDefault(); _this.setState({ dragOver: false }); (_this$props$onChange = (_this$props4 = _this.props).onChange) === null || _this$props$onChange === void 0 ? void 0 : _this$props$onChange.call(_this$props4, event); } (_this$props$onDrop = (_this$props5 = _this.props).onDrop) === null || _this$props$onDrop === void 0 ? void 0 : _this$props$onDrop.call(_this$props5, event); }; _this.inputRef = React.createRef(); _this.state = { dragOver: false }; return _this; } var _proto = UploadTrigger.prototype; _proto.getInputInstance = function getInputInstance() { return this.inputRef.current; }; _proto.render = function render() { var _classNames; var _this$props6 = this.props, name = _this$props6.name, accept = _this$props6.accept, multiple = _this$props6.multiple, disabled = _this$props6.disabled, onChange = _this$props6.onChange, children = _this$props6.children, classPrefix = _this$props6.classPrefix, className = _this$props6.className, Component = _this$props6.componentClass, rest = _objectWithoutPropertiesLoose(_this$props6, ["name", "accept", "multiple", "disabled", "onChange", "children", "classPrefix", "className", "componentClass"]); var unhandled = getUnhandledProps(UploadTrigger, rest); var addPrefix = prefix(classPrefix); var classes = classNames(classPrefix, className, (_classNames = {}, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('customize')] = children, _classNames[addPrefix('drag-over')] = this.state.dragOver, _classNames)); var buttonProps = _extends({}, unhandled, { className: addPrefix('btn'), onClick: this.handleClick, onDragEnter: this.handleDragEnter, onDragLeave: this.handleDragLeave, onDragOver: this.handleDragOver, onDrop: this.handleDrop }); var trigger = children ? React.cloneElement(React.Children.only(children), buttonProps) : React.createElement(Component, buttonProps, React.createElement(FormattedMessage, { id: "upload" }), React.createElement(Ripple, null)); return React.createElement("div", { className: classes }, React.createElement("input", { type: "file", name: name, multiple: multiple, disabled: disabled, accept: accept, ref: this.inputRef, onChange: onChange }), trigger); }; return UploadTrigger; }(React.Component); UploadTrigger.propTypes = { name: PropTypes.string, multiple: PropTypes.bool, disabled: PropTypes.bool, accept: PropTypes.string, onChange: PropTypes.func, classPrefix: PropTypes.string, className: PropTypes.string, children: PropTypes.node, componentClass: PropTypes.elementType, dragable: PropTypes.bool, onDragEnter: PropTypes.func, onDragLeave: PropTypes.func, onDragOver: PropTypes.func, onDrop: PropTypes.func }; export default defaultProps({ componentClass: Button, classPrefix: 'uploader-trigger' })(UploadTrigger);