Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to react 16 and rewrote FileInput with ES6 class #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 47 additions & 57 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,55 @@
var React = require('react');
import React, { Component } from 'react';
import PropTypes from 'prop-types';

var FileInput = React.createClass({
getInitialState: function() {
return {
value: '',
styles: {
parent: {
position: 'relative'
},
file: {
position: 'absolute',
top: 0,
left: 0,
opacity: 0,
width: '100%',
zIndex: 1
},
text: {
position: 'relative',
zIndex: -1
}
}
};
},
class FileInput extends Component {
static propTypes={
name: PropTypes.string,
className: PropTypes.string,
onChange: PropTypes.func,
disabled: PropTypes.bool,
accept: PropTypes.string,
onChange: PropTypes.func,
};

handleChange: function(e) {
state={
value: '',
};

handleChange(e) {
this.setState({
value: e.target.value.split(/(\\|\/)/g).pop()
value: e.target.value.split(/(\\|\/)/g).pop(),
});
if (this.props.onChange) this.props.onChange(e);
},

render: function() {
return React.DOM.div({
style: this.state.styles.parent
},

// Actual file input
React.DOM.input({
type: 'file',
name: this.props.name,
className: this.props.className,
onChange: this.handleChange,
disabled: this.props.disabled,
accept: this.props.accept,
style: this.state.styles.file
}),
}

// Emulated file input
React.DOM.input({
type: 'text',
tabIndex: -1,
name: this.props.name + '_filename',
value: this.state.value,
className: this.props.className,
onChange: function() {},
placeholder: this.props.placeholder,
disabled: this.props.disabled,
style: this.state.styles.text
}));
render() {
return (
<div>
<input
type="file"
style={{ cursor: 'pointer',
position: 'absolute',
top: 0,
left: 0,
opacity: 0,
width: '100%',
zIndex: 1 }}
name={this.props.name}
accept={this.props.accept}
className={this.props.className}
onChange={e => this.handleChange(e)}
/>
<input
type="text"
tabIndex="-1"
value=""
name={this.props.name}
className={this.props.className}
style={{ position: 'relative', zIndex: -1 }}
/>
</div>
);
}
});
}

module.exports = FileInput;
export default FileInput;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"homepage": "https://github.com/captivationsoftware/react-file-input",
"peerDependencies": {
"react": "0.x"
},
"dependencies": {
"react": "^16.2.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we need to add react 16 as a dependency or you are somewhere taking it from the project installing this lib.

}
}