Skip to content

Commit

Permalink
Hooks FTW
Browse files Browse the repository at this point in the history
  • Loading branch information
sunify committed Jan 9, 2019
1 parent f5f188c commit 71170cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .babelrc
@@ -1,3 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
"presets": [["@babel/preset-env", { "loose": true }], "@babel/preset-react"]
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"peerDependencies": {
"prop-types": "^15.6.2",
"react": "15.x || 16.x"
"react": "^16.7.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
96 changes: 39 additions & 57 deletions src/index.js
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState, useRef } from 'react';
import PropTypes from 'prop-types';

const hiddenInputStyle = {
Expand All @@ -11,72 +11,54 @@ const hiddenInputStyle = {
height: '100%',
};

class FileInput extends React.Component {
constructor(props) {
super(props);
this.state = {
files: [],
};
this.handleChange = this.handleChange.bind(this);
this.handleKey = this.handleKey.bind(this);
this.handleInputRef = this.handleInputRef.bind(this);
}
function FileInput({ renderButton, style, className, onChange, ...props }) {
const [value, setValue] = useState('');
const [files, setFiles] = useState([]);
const inputRef = useRef(null);

handleChange(e) {
const { onChange } = this.props;
const handleChange = e => {
if (onChange) {
onChange(e);
}
this.setState({
value: e.target.value,
files: e.target.files,
});
}
setValue(e.target.value);
setFiles(e.target.files);
};

handleKey(e) {
const handleKey = e => {
if (e.which === 32 || e.which === 13) {
e.preventDefault();
if (this.inputRef) {
this.inputRef.click();
if (inputRef.current) {
inputRef.current.click();
}
}
}
};

handleInputRef(ref) {
this.inputRef = ref;
}

render() {
const { value, files } = this.state;
const { renderButton, style, className, ...props } = this.props;

return (
<label
style={Object.assign(
{
position: 'relative',
overflow: 'hidden',
},
style
)}
className={className}
tabIndex={0}
onKeyPress={this.handleKey}
onKeyUp={this.handleKey}
>
<input
{...props}
ref={this.handleInputRef}
style={renderButton ? hiddenInputStyle : {}}
type="file"
onChange={this.handleChange}
readOnly
tabIndex={-1}
/>
{renderButton && renderButton(value, files)}
</label>
);
}
return (
<label
style={Object.assign(
{
position: 'relative',
overflow: 'hidden',
},
style
)}
className={className}
tabIndex={0}
onKeyPress={handleKey}
onKeyUp={handleKey}
>
<input
{...props}
ref={inputRef}
style={renderButton ? hiddenInputStyle : {}}
type="file"
onChange={handleChange}
readOnly
tabIndex={-1}
/>
{renderButton && renderButton(value, files)}
</label>
);
}

FileInput.propTypes = {
Expand Down

0 comments on commit 71170cd

Please sign in to comment.