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

125 lines
2.8 KiB
JavaScript
Raw Normal View History

import { CX } from '../constants'
import Icon from './icon'
2020-03-05 15:44:17 +00:00
import Image from './image'
import Text from './text'
export default class FileInput extends PureComponent {
2020-03-05 15:44:17 +00:00
static propTypes = {
onChange: PropTypes.func,
file: PropTypes.any,
fileType: PropTypes.string,
disabled: PropTypes.bool,
title: PropTypes.string,
id: PropTypes.string.isRequired,
2020-03-05 15:44:17 +00:00
height: PropTypes.string,
width: PropTypes.string,
isBordered: PropTypes.bool,
className: PropTypes.string,
2020-03-05 15:44:17 +00:00
}
static defaultProps = {
fileType: 'image',
isBordered: false,
2020-03-05 15:44:17 +00:00
}
state = {
file: this.props.file,
hovering: false,
2020-03-05 15:44:17 +00:00
}
handleOnChange = (e) => {
this.props.onChange(e)
this.setState({
file: URL.createObjectURL(e.target.files[0])
})
}
handleMouseLeave = () => {
this.setState({ hovering: false })
}
handleMouseEnter = () => {
this.setState({ hovering: true })
}
2020-03-05 15:44:17 +00:00
render() {
const {
id,
2020-03-05 15:44:17 +00:00
fileType,
disabled,
title,
height,
width,
className,
isBordered,
2020-03-05 15:44:17 +00:00
} = this.props
const { file, hovering } = this.state
2020-03-05 15:44:17 +00:00
const containerClasses = CX(className, {
default: 1,
alignItemsCenter: 1,
cursorPointer: 1,
justifyContentCenter: 1,
overflowHidden: true,
radiusSmall: isBordered,
px10: isBordered,
py10: isBordered,
border2PX: isBordered,
borderColorSecondary: isBordered,
borderDashed: isBordered,
})
const iconClasses = CX({
fillSecondary: !hovering,
fillWhite: hovering,
})
2020-03-05 15:44:17 +00:00
return (
<div
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
>
2020-03-05 15:44:17 +00:00
{
!!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
className={containerClasses}
htmlFor={`file-input-${id}`}
2020-03-05 15:44:17 +00:00
style={{
width,
height,
}}
>
<Image
alt={title || id}
className={[_s.height100PC, _s.width100PC].join(' ')}
2020-03-05 15:44:17 +00:00
src={fileType === 'image' ? file : null}
/>
{
(!file || hovering) &&
<div className={[_s.default, _s.posAbs, _s.cursorPointer, _s.top0, _s.bottom0, _s.left0, _s.right0, _s.alignItemsCenter, _s.justifyContentCenter, _s.bgBlackOpaquest_onHover].join(' ')}>
<Icon id='add-image' size='32px' className={iconClasses} />
2020-03-05 15:44:17 +00:00
</div>
}
</label>
<input
id={`file-input-${id}`}
2020-03-05 15:44:17 +00:00
className={_s.displayNone}
disabled={disabled}
onChange={this.handleOnChange}
type='file'
/>
</div>
)
}
2020-03-05 15:44:17 +00:00
}