wix-style-react
Version:
wix-style-react
155 lines (127 loc) • 5.33 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import PropTypes from 'prop-types';
import styles from './FilePicker.scss';
import WixComponent from '../BaseComponents/WixComponent';
import Add from '../new-icons/Add';
import uniqueId from 'lodash/uniqueId';
/**
* File picker component
*/
var FilePicker = function (_WixComponent) {
_inherits(FilePicker, _WixComponent);
function FilePicker(props) {
_classCallCheck(this, FilePicker);
var _this = _possibleConstructorReturn(this, (FilePicker.__proto__ || Object.getPrototypeOf(FilePicker)).call(this, props));
_this.state = {
selectedFileName: props.subLabel
};
_this.id = props.id || uniqueId('file_picker_input_');
return _this;
}
_createClass(FilePicker, [{
key: 'onChooseFile',
value: function onChooseFile(file) {
var _props = this.props,
maxSize = _props.maxSize,
onChange = _props.onChange;
if (file) {
onChange(file);
if (file.size <= maxSize) {
this.setState({ selectedFileName: file.name });
}
}
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props2 = this.props,
header = _props2.header,
mainLabel = _props2.mainLabel,
supportedFormats = _props2.supportedFormats,
error = _props2.error,
errorMessage = _props2.errorMessage;
return React.createElement(
'div',
null,
header && React.createElement(
'span',
{ className: styles.header },
header
),
React.createElement(
'label',
{ className: styles.label, htmlFor: this.id },
React.createElement(
'div',
{ className: styles.icon },
React.createElement(Add, null)
),
React.createElement(
'div',
{ className: styles.content },
React.createElement(
'span',
{ className: styles.cta, 'data-hook': 'main-label' },
mainLabel
),
React.createElement(
'span',
{ className: styles.info, 'data-hook': 'sub-label' },
this.state.selectedFileName
),
error && React.createElement(
'span',
{ className: styles.error, 'data-hook': 'filePicker-error' },
errorMessage
)
)
),
React.createElement('input', {
id: this.id,
className: styles.input,
type: 'file',
accept: supportedFormats,
onChange: function onChange(e) {
return _this2.onChooseFile(e.target.files[0]);
}
})
);
}
}]);
return FilePicker;
}(WixComponent);
FilePicker.displayName = 'FilePicker';
FilePicker.defaultProps = {
mainLabel: 'Choose File',
subLabel: 'No file chosen (Max size 5 MB)',
onChange: function onChange() {},
supportedFormats: '*',
errorMessage: '',
maxSize: 5000000 //5MB
};
FilePicker.propTypes = {
/** Some text that will appear above the Icon */
header: PropTypes.string,
/** Callback function for when a file is uploaded */
onChange: PropTypes.func,
/** Some text that will appear as a main label besides the Icon */
mainLabel: PropTypes.string,
/** Some text that will appear as a sub label besides the Icon */
subLabel: PropTypes.string,
/** supported formats seperated by comma (.png, .pdf) */
supportedFormats: PropTypes.string,
/** Max size of file allowed */
maxSize: PropTypes.number,
/** should present error */
error: PropTypes.bool,
/** error message to present */
errorMessage: PropTypes.string,
/** id for the filePicker */
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};
export default FilePicker;