UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

75 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var styles_1 = require("./styles"); var BaseFileComponent = function (props) { var className = props.className, value = props.value; return React.createElement("input", { className: className, type: "file", value: value, accept: props.fileExtension, onChange: props.onChange }); }; var UpFile = (function (_super) { tslib_1.__extends(UpFile, _super); function UpFile(p, c) { var _this = _super.call(this, p, c) || this; _this.onChange = function (event) { if (!FileReader) { return null; } if (event.target.files.length == 0) { _this.props.onChange([]); return; } var size = event.target.files[0].size; if (size > _this.maxSizeb) { _this.props.onError("Plus de " + _this.maxSizeMb + "M"); return; } var reader = new FileReader(); reader.onloadend = function () { if (Array.isArray(reader.result)) { var array = new Uint8Array(reader.result); var a = []; for (var i = 0; i < array.length; i++) { a.push(array[i]); } _this.props.onChange(a); } }; reader.readAsArrayBuffer(event.target.files[0]); }; _this.state = { value: p.value }; return _this; } UpFile.prototype.componentWillReceiveProps = function (nextProps) { if (nextProps.value !== this.props.value) { this.setState({ value: nextProps.value }); } }; UpFile.prototype.render = function () { if (!FileReader) { return React.createElement("span", null, "Non support du navigateur"); } var _a = this.props, onChange = _a.onChange, onError = _a.onError, value = _a.value, others = tslib_1.__rest(_a, ["onChange", "onError", "value"]); return (React.createElement("div", { className: styles_1.getStyles(this.props) }, React.createElement(BaseFileComponent, tslib_1.__assign({ value: this.state.value }, others, { onChange: this.onChange })))); }; Object.defineProperty(UpFile.prototype, "maxSizeb", { get: function () { return 1048576 * this.maxSizeMb; }, enumerable: true, configurable: true }); Object.defineProperty(UpFile.prototype, "maxSizeMb", { get: function () { return this.props.maxSize || 5; }, enumerable: true, configurable: true }); return UpFile; }(React.Component)); exports.default = UpFile; //# sourceMappingURL=UpFile.js.map