闭社主体 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.

77 lines
2.1 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 => ({
  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. @connect(makeMapStateToProps)
  22. @injectIntl
  23. export default class UploadButton extends ImmutablePureComponent {
  24. static propTypes = {
  25. disabled: PropTypes.bool,
  26. onSelectFile: PropTypes.func.isRequired,
  27. style: PropTypes.object,
  28. resetFileKey: PropTypes.number,
  29. acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
  30. intl: PropTypes.object.isRequired,
  31. };
  32. handleChange = (e) => {
  33. if (e.target.files.length > 0) {
  34. this.props.onSelectFile(e.target.files);
  35. }
  36. }
  37. handleClick = () => {
  38. this.fileElement.click();
  39. }
  40. setRef = (c) => {
  41. this.fileElement = c;
  42. }
  43. render () {
  44. const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
  45. return (
  46. <div className='compose-form__upload-button'>
  47. <IconButton icon='camera' title={intl.formatMessage(messages.upload)} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted style={iconStyle} />
  48. <label>
  49. <span style={{ display: 'none' }}>{intl.formatMessage(messages.upload)}</span>
  50. <input
  51. key={resetFileKey}
  52. ref={this.setRef}
  53. type='file'
  54. multiple={false}
  55. accept={acceptContentTypes.toArray().join(',')}
  56. onChange={this.handleChange}
  57. disabled={disabled}
  58. style={{ display: 'none' }}
  59. />
  60. </label>
  61. </div>
  62. );
  63. }
  64. }