This works well for me. It will hide the ugly file input component, and allow you to trigger its click function when the button is clicked instead.
import React from 'react'
import {
Button,
Input,
Label
} from 'reactstrap'
class UploadButton extends React.Component {
constructor(props) {
super(props)
this.fileInput = React.createRef() // ref to fileInput
}
// when called, triggers fileInput click function
triggerInputFile = () => {
if (this.fileInput.current != undefined && this.fileInput.current.click != undefined)
this.fileInput.current.click()
}
// handle file upload
handleFileUpload = () => {
// handle click
}
render() {
var color = 'primary'
return (
<div className='content'>
<Button
color={color}
onClick={() => this.triggerInputFile()}
>
Upload
</Button>
<input
ref={this.fileInput}
type='file'
onClick={() => this.handleFileUpload()}
style={styles.input}
/>
</div>
)
}
}
const styles = {
input: {
opacity: '0%', // dont want to see it
position: 'absolute' // does not mess with other elements
}
}
export default UploadButton