2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-07-14 22:37:46 +01:00
|
|
|
import { CX } from '../constants'
|
|
|
|
import Icon from './icon'
|
2020-03-05 15:44:17 +00:00
|
|
|
import Image from './image'
|
|
|
|
import Text from './text'
|
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
class FileInput extends React.PureComponent {
|
2020-03-05 15:44:17 +00:00
|
|
|
|
|
|
|
state = {
|
2020-06-11 03:16:23 +01:00
|
|
|
file: this.props.file,
|
2020-07-17 21:20:04 +01:00
|
|
|
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])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-17 21:20:04 +01:00
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovering: false })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovering: true })
|
|
|
|
}
|
|
|
|
|
2020-03-05 15:44:17 +00:00
|
|
|
render() {
|
|
|
|
const {
|
2020-07-14 22:37:46 +01:00
|
|
|
id,
|
2020-03-05 15:44:17 +00:00
|
|
|
fileType,
|
|
|
|
disabled,
|
|
|
|
title,
|
|
|
|
height,
|
|
|
|
width,
|
2020-07-14 22:37:46 +01:00
|
|
|
className,
|
|
|
|
isBordered,
|
2020-03-05 15:44:17 +00:00
|
|
|
} = this.props
|
2020-07-17 21:20:04 +01:00
|
|
|
const { file, hovering } = this.state
|
2020-03-05 15:44:17 +00:00
|
|
|
|
2020-07-14 22:37:46 +01:00
|
|
|
const containerClasses = CX(className, {
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
aiCenter: 1,
|
2020-07-14 22:37:46 +01:00
|
|
|
cursorPointer: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
jcCenter: 1,
|
2020-07-14 22:37:46 +01:00
|
|
|
overflowHidden: true,
|
|
|
|
radiusSmall: isBordered,
|
|
|
|
px10: isBordered,
|
|
|
|
py10: isBordered,
|
|
|
|
border2PX: isBordered,
|
|
|
|
borderColorSecondary: isBordered,
|
|
|
|
borderDashed: isBordered,
|
|
|
|
})
|
|
|
|
|
2020-07-17 21:20:04 +01:00
|
|
|
const iconClasses = CX({
|
2020-08-18 21:43:06 +01:00
|
|
|
cSecondary: !hovering,
|
|
|
|
cWhite: hovering,
|
2020-07-17 21:20:04 +01:00
|
|
|
})
|
|
|
|
|
2020-03-05 15:44:17 +00:00
|
|
|
return (
|
2020-07-17 21:20:04 +01:00
|
|
|
<div
|
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
|
|
|
>
|
2020-03-05 15:44:17 +00:00
|
|
|
{
|
|
|
|
!!title &&
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _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-07-14 22:37:46 +01:00
|
|
|
className={containerClasses}
|
|
|
|
htmlFor={`file-input-${id}`}
|
2020-03-05 15:44:17 +00:00
|
|
|
style={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Image
|
2020-07-14 22:41:21 +01:00
|
|
|
alt={title || id}
|
2020-08-18 21:43:06 +01:00
|
|
|
className={[_s.h100PC, _s.w100PC].join(' ')}
|
2020-03-05 15:44:17 +00:00
|
|
|
src={fileType === 'image' ? file : null}
|
|
|
|
/>
|
|
|
|
{
|
2020-07-17 21:20:04 +01:00
|
|
|
(!file || hovering) &&
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.posAbs, _s.cursorPointer, _s.top0, _s.bottom0, _s.left0, _s.right0, _s.aiCenter, _s.jcCenter, _s.bgBlackOpaquest_onHover].join(' ')}>
|
2020-07-17 21:20:04 +01:00
|
|
|
<Icon id='add-image' size='32px' className={iconClasses} />
|
2020-03-05 15:44:17 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
2020-07-14 22:37:46 +01:00
|
|
|
id={`file-input-${id}`}
|
2020-03-05 15:44:17 +00:00
|
|
|
className={_s.displayNone}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={this.handleOnChange}
|
|
|
|
type='file'
|
|
|
|
/>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2020-07-07 22:02:09 +01:00
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileInput.propTypes = {
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
file: PropTypes.any,
|
|
|
|
fileType: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
title: PropTypes.string,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
height: PropTypes.string,
|
|
|
|
width: PropTypes.string,
|
|
|
|
isBordered: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInput.defaultProps = {
|
|
|
|
fileType: 'image',
|
|
|
|
isBordered: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileInput
|