2017-05-03 00:04:16 +00:00
|
|
|
import React from 'react';
|
2016-11-13 18:08:52 +00:00
|
|
|
import IconButton from '../../../components/icon_button';
|
2017-04-21 18:05:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2016-11-18 14:36:16 +00:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-05-08 14:49:53 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-05-28 17:15:35 +00:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-11-18 14:36:16 +00:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2017-05-20 15:31:47 +00:00
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media' },
|
2016-11-18 14:36:16 +00:00
|
|
|
});
|
2016-09-07 16:17:15 +00:00
|
|
|
|
2017-05-08 14:49:53 +00:00
|
|
|
const makeMapStateToProps = () => {
|
2017-06-23 14:05:04 +00:00
|
|
|
const mapStateToProps = state => ({
|
2017-05-28 17:15:35 +00:00
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
2017-05-08 14:49:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
2017-05-20 15:31:47 +00:00
|
|
|
};
|
2017-04-23 12:18:58 +00:00
|
|
|
|
|
|
|
const iconStyle = {
|
|
|
|
height: null,
|
2017-05-20 15:31:47 +00:00
|
|
|
lineHeight: '27px',
|
|
|
|
};
|
2017-04-23 12:18:58 +00:00
|
|
|
|
2017-06-23 17:36:54 +00:00
|
|
|
@connect(makeMapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
export default class UploadButton extends ImmutablePureComponent {
|
2016-09-07 16:17:15 +00:00
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2017-05-28 17:15:35 +00:00
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
2017-05-20 15:31:47 +00:00
|
|
|
intl: PropTypes.object.isRequired,
|
2017-05-12 12:44:10 +00:00
|
|
|
};
|
2016-09-07 16:17:15 +00:00
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
handleChange = (e) => {
|
2016-09-07 16:17:15 +00:00
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-09-07 16:17:15 +00:00
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
handleClick = () => {
|
2016-11-13 18:08:52 +00:00
|
|
|
this.fileElement.click();
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-13 18:08:52 +00:00
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
setRef = (c) => {
|
2016-11-13 18:08:52 +00:00
|
|
|
this.fileElement = c;
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-09-07 16:17:15 +00:00
|
|
|
|
|
|
|
render () {
|
2017-04-23 12:18:58 +00:00
|
|
|
|
2017-05-08 14:49:53 +00:00
|
|
|
const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
|
2016-11-16 16:20:52 +00:00
|
|
|
|
2016-09-07 16:17:15 +00:00
|
|
|
return (
|
2017-04-23 02:26:55 +00:00
|
|
|
<div className='compose-form__upload-button'>
|
2017-06-06 11:20:07 +00:00
|
|
|
<IconButton icon='camera' title={intl.formatMessage(messages.upload)} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted style={iconStyle} />
|
2017-05-08 14:49:53 +00:00
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
|
|
|
multiple={false}
|
2017-06-06 11:20:07 +00:00
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
2017-05-08 14:49:53 +00:00
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
2016-09-07 16:17:15 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|