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

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