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';
|
2017-05-29 02:15:35 +09:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-11-18 23:36:16 +09:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2018-08-27 20:05:27 +09:00
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media (JPEG, PNG, GIF, WebM, MP4, MOV)' },
|
2016-11-18 23:36:16 +09:00
|
|
|
});
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-05-08 23:49:53 +09:00
|
|
|
const makeMapStateToProps = () => {
|
2017-06-23 23:05:04 +09:00
|
|
|
const mapStateToProps = state => ({
|
2017-05-29 02:15:35 +09:00
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
2017-05-08 23:49:53 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
2017-04-23 21:18:58 +09:00
|
|
|
|
|
|
|
const iconStyle = {
|
|
|
|
height: null,
|
2017-05-21 00:31:47 +09:00
|
|
|
lineHeight: '27px',
|
|
|
|
};
|
2017-04-23 21:18:58 +09:00
|
|
|
|
2018-09-15 00:59:48 +09:00
|
|
|
export default @connect(makeMapStateToProps)
|
2017-06-24 02:36:54 +09:00
|
|
|
@injectIntl
|
2018-09-15 00:59:48 +09:00
|
|
|
class UploadButton extends ImmutablePureComponent {
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
2019-03-06 12:53:37 +09:00
|
|
|
unavailable: PropTypes.bool,
|
2017-05-12 21:44:10 +09:00
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2017-05-29 02:15:35 +09:00
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
2017-05-21 00:31:47 +09:00
|
|
|
intl: PropTypes.object.isRequired,
|
2017-05-12 21:44:10 +09:00
|
|
|
};
|
2016-09-08 01:17:15 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
handleChange = (e) => {
|
2016-09-08 01:17:15 +09:00
|
|
|
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
|
|
|
|
2017-05-12 21:44:10 +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
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
setRef = (c) => {
|
2016-11-14 03:08:52 +09:00
|
|
|
this.fileElement = c;
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-09-08 01:17:15 +09:00
|
|
|
|
|
|
|
render () {
|
2019-03-06 12:53:37 +09:00
|
|
|
const { intl, resetFileKey, unavailable, disabled, acceptContentTypes } = this.props;
|
2017-04-23 21:18:58 +09:00
|
|
|
|
2019-03-06 12:53:37 +09:00
|
|
|
if (unavailable) {
|
|
|
|
return null;
|
|
|
|
}
|
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-06-06 20:20:07 +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-07-28 07:54:48 +09:00
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.upload)}</span>
|
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
2019-02-03 04:22:05 +09:00
|
|
|
multiple
|
2017-07-28 07:54:48 +09:00
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
</label>
|
2016-09-08 01:17:15 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|