Polygon.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createPolygon, updatePolygon } from './api';
  4. const __com__ = 'Polygon';
  5. //const debug = console.log;
  6. const debug = () => {};
  7. export class Polygon extends Component {
  8. static propTypes = {
  9. AMap: PropTypes.object,
  10. __map__: PropTypes.object,
  11. options: PropTypes.object,
  12. events: PropTypes.object
  13. };
  14. constructor() {
  15. super();
  16. this.refElement = null;
  17. this._entity = null;
  18. debug(__com__, 'constructor', this._entity);
  19. }
  20. componentWillMount() {
  21. debug(__com__, 'componentWillMount', this._entity);
  22. }
  23. componentDidMount() {
  24. debug(__com__, 'componentDidMount', this._entity);
  25. let { AMap, __map__, options, events, children } = this.props;
  26. //let opts = { ...(options || {}), map: __map__, content: children };
  27. let opts = { ...(options || {}), map: __map__ };
  28. this._entity = createPolygon(AMap, opts, events);
  29. }
  30. componentWillReceiveProps(nextProps) {
  31. debug(__com__, 'componentWillReceiveProps', this._entity);
  32. }
  33. componentWillUpdate() {
  34. debug(__com__, 'componentWillUpdate', this._entity);
  35. }
  36. componentDidUpdate(prevProps) {
  37. debug(__com__, 'componentDidUpdate', this._entity);
  38. let { AMap, __map__, options, events, children } = this.props;
  39. //let opts = { ...(options || {}), map: __map__, content: children };
  40. let opts = { ...(options || {}), map: __map__ };
  41. if (!this._entity) {
  42. this._entity = createPolygon(AMap, opts, events);
  43. return;
  44. }
  45. // need check props changes, then update.
  46. let oldOpts = {
  47. ...(prevProps.options || {}),
  48. map: prevProps.__map__
  49. };
  50. updatePolygon(this._entity, opts, events, oldOpts, prevProps.events);
  51. }
  52. componentWillUnmount() {
  53. debug(__com__, 'componentWillUnmount', this._entity);
  54. if (this._entity) {
  55. // this._entity.clearMap();
  56. this._entity.setMap(null);
  57. delete this._entity;
  58. // delete this._entity;
  59. this._entity = null;
  60. }
  61. }
  62. // shouldComponentUpdate(nextProps, nextState) {
  63. // debug(__com__, 'shouldComponentUpdate', this._entity);
  64. // return false;
  65. // }
  66. render() {
  67. debug(__com__, 'render', this._entity);
  68. let {
  69. AMap,
  70. options,
  71. events,
  72. match,
  73. location,
  74. history,
  75. staticContext,
  76. ...rest
  77. } = this.props;
  78. return null;
  79. // return (
  80. // <React.Fragment>
  81. // </React.Fragment>
  82. // )
  83. }
  84. }
  85. export default Polygon;