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.

58 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. const iconStyle = {
  8. lineHeight: '27px',
  9. height: null
  10. };
  11. class UploadButton extends React.PureComponent {
  12. constructor (props, context) {
  13. super(props, context);
  14. this.handleChange = this.handleChange.bind(this);
  15. this.handleClick = this.handleClick.bind(this);
  16. this.setRef = this.setRef.bind(this);
  17. }
  18. handleChange (e) {
  19. if (e.target.files.length > 0) {
  20. this.props.onSelectFile(e.target.files);
  21. }
  22. }
  23. handleClick () {
  24. this.fileElement.click();
  25. }
  26. setRef (c) {
  27. this.fileElement = c;
  28. }
  29. render () {
  30. const { intl, resetFileKey, disabled } = this.props;
  31. return (
  32. <div style={this.props.style}>
  33. <IconButton icon='camera' title={intl.formatMessage(messages.upload)} disabled={disabled} onClick={this.handleClick} style={iconStyle} size={18} inverted />
  34. <input key={resetFileKey} ref={this.setRef} type='file' multiple={false} onChange={this.handleChange} disabled={disabled} style={{ display: 'none' }} />
  35. </div>
  36. );
  37. }
  38. }
  39. UploadButton.propTypes = {
  40. disabled: PropTypes.bool,
  41. onSelectFile: PropTypes.func.isRequired,
  42. style: PropTypes.object,
  43. resetFileKey: PropTypes.number,
  44. intl: PropTypes.object.isRequired
  45. };
  46. export default injectIntl(UploadButton);