gab-social/app/javascript/gabsocial/components/file_input.js

87 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-03-05 15:44:17 +00:00
import classNames from 'classnames/bind'
import Image from './image'
import Text from './text'
const cx = classNames.bind(_s)
export default class FileInput extends PureComponent {
static propTypes = {
onChange: PropTypes.func,
file: PropTypes.any,
fileType: PropTypes.string,
disabled: PropTypes.bool,
title: PropTypes.string,
height: PropTypes.string,
width: PropTypes.string,
}
static defaultProps = {
fileType: 'image'
}
state = {
file: null,
}
handleOnChange = (e) => {
this.props.onChange(e)
this.setState({
file: URL.createObjectURL(e.target.files[0])
})
}
render() {
const {
fileType,
disabled,
title,
height,
width,
} = this.props
const { file } = this.state
return (
<div>
{
!!title &&
2020-03-11 23:56:18 +00:00
<div className={[_s.default, _s.mb10, _s.pl15].join(' ')}>
2020-03-05 15:44:17 +00:00
<Text size='small' weight='medium' color='secondary'>
{title}
</Text>
</div>
}
<label
2020-04-08 02:06:59 +01:00
className={[_s.default, _s.alignItemsCenter, _s.radiusSmall, _s.cursorPointer, _s.px10, _s.py10, _s.justifyContentCenter, _s.border2PX, _s.borderColorSecondary, _s.borderDashed].join(' ')}
2020-03-05 15:44:17 +00:00
htmlFor={`file-input-${title}`}
style={{
width,
height,
}}
>
<Image
2020-04-08 02:06:59 +01:00
className={[_s.height100PC, _s.width100PC, _s.radiusSmall].join(' ')}
2020-03-05 15:44:17 +00:00
src={fileType === 'image' ? file : null}
/>
{
!file &&
<div className={[_s.positionAbsolute, _s.cursorPointer].join(' ')}>
<Text size='medium' color='secondary'>
Click Here to Upload
</Text>
</div>
}
</label>
<input
id={`file-input-${title}`}
className={_s.displayNone}
disabled={disabled}
onChange={this.handleOnChange}
type='file'
/>
</div>
)
}
}