112 lines
3.7 KiB
JavaScript
112 lines
3.7 KiB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
|
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
|
|
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import classNames from 'classnames';
|
|
import { mapToCssModules } from './utils';
|
|
var propTypes = {
|
|
className: PropTypes.string,
|
|
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
|
|
label: PropTypes.node,
|
|
valid: PropTypes.bool,
|
|
invalid: PropTypes.bool,
|
|
bsSize: PropTypes.string,
|
|
htmlFor: PropTypes.string,
|
|
cssModule: PropTypes.object,
|
|
onChange: PropTypes.func,
|
|
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array, PropTypes.func]),
|
|
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
|
|
};
|
|
|
|
var CustomFileInput =
|
|
/*#__PURE__*/
|
|
function (_React$Component) {
|
|
_inheritsLoose(CustomFileInput, _React$Component);
|
|
|
|
function CustomFileInput(props) {
|
|
var _this;
|
|
|
|
_this = _React$Component.call(this, props) || this;
|
|
_this.state = {
|
|
files: null
|
|
};
|
|
_this.onChange = _this.onChange.bind(_assertThisInitialized(_this));
|
|
return _this;
|
|
}
|
|
|
|
var _proto = CustomFileInput.prototype;
|
|
|
|
_proto.onChange = function onChange(e) {
|
|
var input = e.target;
|
|
var onChange = this.props.onChange;
|
|
var files = this.getSelectedFiles(input);
|
|
|
|
if (typeof onChange === 'function') {
|
|
onChange.apply(void 0, arguments);
|
|
}
|
|
|
|
this.setState({
|
|
files: files
|
|
});
|
|
};
|
|
|
|
_proto.getSelectedFiles = function getSelectedFiles(input) {
|
|
var multiple = this.props.multiple;
|
|
|
|
if (multiple && input.files) {
|
|
var files = [].slice.call(input.files);
|
|
return files.map(function (file) {
|
|
return file.name;
|
|
}).join(', ');
|
|
}
|
|
|
|
if (input.value.indexOf('fakepath') !== -1) {
|
|
var parts = input.value.split('\\');
|
|
return parts[parts.length - 1];
|
|
}
|
|
|
|
return input.value;
|
|
};
|
|
|
|
_proto.render = function render() {
|
|
var _this$props = this.props,
|
|
className = _this$props.className,
|
|
label = _this$props.label,
|
|
valid = _this$props.valid,
|
|
invalid = _this$props.invalid,
|
|
cssModule = _this$props.cssModule,
|
|
children = _this$props.children,
|
|
bsSize = _this$props.bsSize,
|
|
innerRef = _this$props.innerRef,
|
|
htmlFor = _this$props.htmlFor,
|
|
type = _this$props.type,
|
|
onChange = _this$props.onChange,
|
|
dataBrowse = _this$props.dataBrowse,
|
|
attributes = _objectWithoutPropertiesLoose(_this$props, ["className", "label", "valid", "invalid", "cssModule", "children", "bsSize", "innerRef", "htmlFor", "type", "onChange", "dataBrowse"]);
|
|
|
|
var customClass = mapToCssModules(classNames(className, "custom-file"), cssModule);
|
|
var validationClassNames = mapToCssModules(classNames(invalid && 'is-invalid', valid && 'is-valid'), cssModule);
|
|
var labelHtmlFor = htmlFor || attributes.id;
|
|
var files = this.state.files;
|
|
return React.createElement("div", {
|
|
className: customClass
|
|
}, React.createElement("input", _extends({
|
|
type: "file"
|
|
}, attributes, {
|
|
ref: innerRef,
|
|
className: classNames(validationClassNames, mapToCssModules('custom-file-input', cssModule)),
|
|
onChange: this.onChange
|
|
})), React.createElement("label", {
|
|
className: mapToCssModules('custom-file-label', cssModule),
|
|
htmlFor: labelHtmlFor,
|
|
"data-browse": dataBrowse
|
|
}, files || label || 'Choose file'), children);
|
|
};
|
|
|
|
return CustomFileInput;
|
|
}(React.Component);
|
|
|
|
CustomFileInput.propTypes = propTypes;
|
|
export default CustomFileInput; |