react-simple-file-input
Advanced tools
+42
-34
@@ -44,2 +44,3 @@ 'use strict'; | ||
| onCancel: _reactAddons2['default'].PropTypes.func, | ||
| onChange: _reactAddons2['default'].PropTypes.func, | ||
| onError: _reactAddons2['default'].PropTypes.func, | ||
@@ -51,8 +52,2 @@ onProgress: _reactAddons2['default'].PropTypes.func, | ||
| getDefaultProps: function getDefaultProps() { | ||
| return { | ||
| readAs: 'text' | ||
| }; | ||
| }, | ||
| componentWillMount: function componentWillMount() { | ||
@@ -73,42 +68,55 @@ if (!window.File || !window.FileReader) { | ||
| var abortIf = _props.abortIf; | ||
| var onChange = _props.onChange; | ||
| var fileReader = new window.FileReader(); | ||
| var file = event.target.files[0]; | ||
| if (cancelIf && cancelIf(file)) { | ||
| if (onCancel) { | ||
| onCancel(file); | ||
| } | ||
| return; | ||
| if (onChange) { | ||
| onChange(file); | ||
| } | ||
| var _loop = function (i) { | ||
| var handlerName = SUPPORTED_EVENTS[i]; | ||
| if (readAs) { | ||
| var _ret = (function () { | ||
| var fileReader = new window.FileReader(); | ||
| if (_this.props[handlerName]) { | ||
| fileReader[handlerName.toLowerCase()] = function (fileReadEvent) { | ||
| _this.props[handlerName](fileReadEvent, file); | ||
| if (cancelIf && cancelIf(file)) { | ||
| if (onCancel) { | ||
| onCancel(file); | ||
| } | ||
| return { | ||
| v: undefined | ||
| }; | ||
| } | ||
| var _loop = function (i) { | ||
| var handlerName = SUPPORTED_EVENTS[i]; | ||
| if (_this.props[handlerName]) { | ||
| fileReader[handlerName.toLowerCase()] = function (fileReadEvent) { | ||
| _this.props[handlerName](fileReadEvent, file); | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
| for (var i = 0; i < SUPPORTED_EVENTS.length; i++) { | ||
| _loop(i); | ||
| } | ||
| for (var i = 0; i < SUPPORTED_EVENTS.length; i++) { | ||
| _loop(i); | ||
| } | ||
| if (typeof abortIf !== 'undefined') { | ||
| fileReader.onprogress = function (event) { | ||
| if (abortIf(event, file)) { | ||
| fileReader.abort(); | ||
| if (typeof abortIf !== 'undefined') { | ||
| fileReader.onprogress = function (event) { | ||
| if (abortIf(event, file)) { | ||
| fileReader.abort(); | ||
| } else if (onProgress) { | ||
| onProgress(event, file); | ||
| } | ||
| }; | ||
| } else if (onProgress) { | ||
| onProgress(event, file); | ||
| fileReader.onprogress = onProgress; | ||
| } | ||
| }; | ||
| } else if (onProgress) { | ||
| fileReader.onprogress = onProgress; | ||
| fileReader[READ_METHOD_ALIASES[readAs]](file); | ||
| })(); | ||
| if (typeof _ret === 'object') return _ret.v; | ||
| } | ||
| fileReader[READ_METHOD_ALIASES[readAs]](file); | ||
| }, | ||
@@ -115,0 +123,0 @@ |
+1
-1
| { | ||
| "name": "react-simple-file-input", | ||
| "version": "0.0.4", | ||
| "version": "0.1.0", | ||
| "description": "Simple wrapper for the HTML input tag and HTML5 FileReader API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
+11
-2
@@ -9,5 +9,5 @@ # react-simple-file-input | ||
| react-simple-file-input expects a readAs option to indicate how the file should be read. Valid options are: | ||
| react-simple-file-input expects a `readAs` option to indicate how the file should be read. Valid options are: | ||
| - 'text' (Text) (Default) | ||
| - 'text' (Text) | ||
| - 'buffer' (Array Buffer) | ||
@@ -17,2 +17,4 @@ - 'binary' (Binary String) | ||
| By default the `readAs` option is `undefined`. If left undefined, the file will not be read from disk and only the `onChange` event will be triggered. | ||
| ### Events | ||
@@ -35,6 +37,13 @@ | ||
| - onChange | ||
| - onCancel | ||
| The `onChange` handler is called whenever the file is changed and occurs before the file is read from disk. It receives a `File` object as its only argument. | ||
| The `onCancel` handler receives the `File` object corresponding to the file the user attempted to read from the file system. | ||
| ### Skipping file reads | ||
| If the `readAs` option is not specified, the file will not be read from disk and only `onChange` will be triggered. All other events as skipped. | ||
| ### Aborting \& cancelling file reads | ||
@@ -41,0 +50,0 @@ |
8973
9.92%118
6.31%128
7.56%