Polyline.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createPolyline, updatePolyline } from './api';
  4. const __com__ = 'Polyline';
  5. //const debug = console.log;
  6. const debug = () => {};
  7. export class Polyline 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.props.children, this._entity);
  22. }
  23. componentDidMount() {
  24. debug(__com__, 'componentDidMount', this.props.children, 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 = createPolyline(AMap, opts, events);
  29. }
  30. componentWillReceiveProps(nextProps) {
  31. debug(__com__, 'componentWillReceiveProps', this.props.children, this._entity);
  32. }
  33. componentWillUpdate() {
  34. debug(__com__, 'componentWillUpdate', this.props.children, this._entity);
  35. }
  36. componentDidUpdate(prevProps) {
  37. debug(__com__, 'componentDidUpdate', this.props.children, 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 = createPolyline(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. updatePolyline(this._entity, opts, events, oldOpts, prevProps.events);
  51. }
  52. componentWillUnmount() {
  53. debug(__com__, 'componentWillUnmount', this.props.children, 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. render() {
  63. debug(__com__, 'render', this.props.children, this._entity);
  64. let {
  65. AMap,
  66. options,
  67. events,
  68. match,
  69. location,
  70. history,
  71. staticContext,
  72. ...rest
  73. } = this.props;
  74. return null;
  75. // return (
  76. // <React.Fragment>
  77. // </React.Fragment>
  78. // )
  79. }
  80. }
  81. export default Polyline;