2017-05-03 09:04:16 +09:00
|
|
|
import React from 'react';
|
2016-11-14 03:08:52 +09:00
|
|
|
import IconButton from '../../../components/icon_button';
|
2017-04-22 03:05:35 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2016-11-18 23:36:16 +09:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-05-08 23:49:53 +09:00
|
|
|
import { connect } from 'react-redux';
|
2016-11-18 23:36:16 +09:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media' }
|
|
|
|
});
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-05-08 23:49:53 +09:00
|
|
|
const makeMapStateToProps = () => {
|
|
|
|
const mapStateToProps = (state, props) => ({
|
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']).toArray(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
|
|
|
}
|
2017-04-23 21:18:58 +09:00
|
|
|
|
|
|
|
const iconStyle = {
|
|
|
|
height: null,
|
|
|
|
lineHeight: '27px'
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
class UploadButton extends React.PureComponent {
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
constructor (props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.setRef = this.setRef.bind(this);
|
|
|
|
}
|
2016-09-08 01:17:15 +09:00
|
|
|
|
|
|
|
handleChange (e) {
|
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-09-08 01:17:15 +09:00
|
|
|
|
|
|
|
handleClick () {
|
2016-11-14 03:08:52 +09:00
|
|
|
this.fileElement.click();
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-11-14 03:08:52 +09:00
|
|
|
|
|
|
|
setRef (c) {
|
|
|
|
this.fileElement = c;
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-09-08 01:17:15 +09:00
|
|
|
|
|
|
|
render () {
|
2017-04-23 21:18:58 +09:00
|
|
|
|
2017-05-08 23:49:53 +09:00
|
|
|
const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
|
2016-11-17 01:20:52 +09:00
|
|
|
|
2016-09-08 01:17:15 +09:00
|
|
|
return (
|
2017-04-23 11:26:55 +09:00
|
|
|
<div className='compose-form__upload-button'>
|
2017-04-23 21:18:58 +09: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 23:49:53 +09:00
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
|
|
|
multiple={false}
|
|
|
|
accept={ acceptContentTypes.join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
2016-09-08 01:17:15 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
UploadButton.propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2017-05-08 23:49:53 +09:00
|
|
|
acceptContentTypes: PropTypes.arrayOf(PropTypes.string).isRequired,
|
2017-04-22 03:05:35 +09:00
|
|
|
intl: PropTypes.object.isRequired
|
|
|
|
};
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-05-08 23:49:53 +09:00
|
|
|
export default connect(makeMapStateToProps)(injectIntl(UploadButton));
|