闭社主体 forked from https://github.com/tootsuite/mastodon
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.

74 lines
2.0 KiB

  1. import React from 'react';
  2. import IconButton from '../../../components/icon_button';
  3. import PropTypes from 'prop-types';
  4. import { defineMessages, injectIntl } from 'react-intl';
  5. import { connect } from 'react-redux';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import ImmutablePropTypes from 'react-immutable-proptypes';
  8. const messages = defineMessages({
  9. upload: { id: 'upload_button.label', defaultMessage: 'Add media' },
  10. });
  11. const makeMapStateToProps = () => {
  12. const mapStateToProps = (state, props) => ({
  13. acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
  14. });
  15. return mapStateToProps;
  16. };
  17. const iconStyle = {
  18. height: null,
  19. lineHeight: '27px',
  20. };
  21. class UploadButton extends ImmutablePureComponent {
  22. static propTypes = {
  23. disabled: PropTypes.bool,
  24. onSelectFile: PropTypes.func.isRequired,
  25. style: PropTypes.object,
  26. resetFileKey: PropTypes.number,
  27. acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
  28. intl: PropTypes.object.isRequired,
  29. };
  30. handleChange = (e) => {
  31. if (e.target.files.length > 0) {
  32. this.props.onSelectFile(e.target.files);
  33. }
  34. }
  35. handleClick = () => {
  36. this.fileElement.click();
  37. }
  38. setRef = (c) => {
  39. this.fileElement = c;
  40. }
  41. render () {
  42. const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
  43. return (
  44. <div className='compose-form__upload-button'>
  45. <IconButton icon='camera' title={intl.formatMessage(messages.upload)} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted style={iconStyle}/>
  46. <input
  47. key={resetFileKey}
  48. ref={this.setRef}
  49. type='file'
  50. multiple={false}
  51. accept={ acceptContentTypes.toArray().join(',')}
  52. onChange={this.handleChange}
  53. disabled={disabled}
  54. style={{ display: 'none' }}
  55. />
  56. </div>
  57. );
  58. }
  59. }
  60. export default connect(makeMapStateToProps)(injectIntl(UploadButton));