ImageUpload.jsx 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react';
  2. import { Upload, Icon, message } from 'antd';
  3. import './style.less';
  4. import { uploaderProps } from '../../utils/upload';
  5. class ImageUpload extends React.Component {
  6. state = {
  7. loading: false,
  8. imageUrl: undefined,
  9. };
  10. handleChange = info => {
  11. if (info.file.status === "uploading") {
  12. this.setState({ loading: true });
  13. return;
  14. }
  15. if (info.file.status === 'removed') {
  16. this.props.onChange();
  17. }
  18. // if (info.file.status === "done") {
  19. // this.setState({
  20. // loading: false,
  21. // })
  22. // if (info.file.response && info.file.response.url) {
  23. // this.setState({
  24. // imageUrl: info.file.response.thumbUrl,
  25. // });
  26. // if (typeof this.props.onChange === 'function') {
  27. // this.props.onChange(info.file.response.url);
  28. // }
  29. // }
  30. // }
  31. };
  32. handleUploadSucess = (url) => {
  33. this.setState({ loading: false });
  34. if (typeof this.props.onChange === 'function') {
  35. this.props.onChange(url);
  36. }
  37. }
  38. render() {
  39. const uploadButton = (
  40. <div>
  41. <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"} />
  42. </div>
  43. );
  44. const value = this.props.value;
  45. return (
  46. <Upload
  47. listType="picture-card"
  48. className="avatar-uploader"
  49. showUploadList={false}
  50. beforeUpload={this.props.beforeUpload}
  51. onChange={this.handleChange}
  52. {...uploaderProps}
  53. disabled={this.props.disabled}
  54. onSuccess={this.handleUploadSucess}
  55. >
  56. {(this.state.imageUrl || value) ? (
  57. <img src={this.state.imageUrl || value} alt="avatar" style={{ width: "100%" }} />
  58. ) : (
  59. uploadButton
  60. )}
  61. </Upload>
  62. );
  63. }
  64. }
  65. export default ImageUpload;