You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

  1. import IconButton from '../../../components/icon_button';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl } from 'react-intl';
  4. const messages = defineMessages({
  5. upload: { id: 'upload_button.label', defaultMessage: 'Add media' }
  6. });
  7. class UploadButton extends React.PureComponent {
  8. constructor (props, context) {
  9. super(props, context);
  10. this.handleChange = this.handleChange.bind(this);
  11. this.handleClick = this.handleClick.bind(this);
  12. this.setRef = this.setRef.bind(this);
  13. }
  14. handleChange (e) {
  15. if (e.target.files.length > 0) {
  16. this.props.onSelectFile(e.target.files);
  17. }
  18. }
  19. handleClick () {
  20. this.fileElement.click();
  21. }
  22. setRef (c) {
  23. this.fileElement = c;
  24. }
  25. render () {
  26. const { intl, resetFileKey, disabled } = this.props;
  27. return (
  28. <div className='compose-form__upload-button'>
  29. <IconButton icon='camera' title={intl.formatMessage(messages.upload)} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted />
  30. <input key={resetFileKey} ref={this.setRef} type='file' multiple={false} onChange={this.handleChange} disabled={disabled} style={{ display: 'none' }} />
  31. </div>
  32. );
  33. }
  34. }
  35. UploadButton.propTypes = {
  36. disabled: PropTypes.bool,
  37. onSelectFile: PropTypes.func.isRequired,
  38. style: PropTypes.object,
  39. resetFileKey: PropTypes.number,
  40. intl: PropTypes.object.isRequired
  41. };
  42. export default injectIntl(UploadButton);