index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import PathNavigator from './PathNavigator';
  3. interface PathSimplifierProps {
  4. __map__?: any;
  5. options?: Omit<PathSimplifierOptions, 'map'>;
  6. pathData?: PathDataItemType[];
  7. children?: React.ReactNode;
  8. onCreated?: (nav: PathSimplifier) => void;
  9. }
  10. const PathSimplifier = (props: PathSimplifierProps) => {
  11. const { __map__, onCreated, options } = props;
  12. const pathSimplifierRef = useRef<PathSimplifier | null>(null);
  13. const [loading, setLoading] = useState(false);
  14. const pathSimplifier = useCallback(
  15. (PathObj: PathSimplifier) => {
  16. pathSimplifierRef.current = new PathObj({
  17. zIndex: 100,
  18. getPath: (_pathData: any) => {
  19. return _pathData.path;
  20. },
  21. getHoverTitle: (_pathData: any) => {
  22. return _pathData.name;
  23. },
  24. map: __map__,
  25. ...options,
  26. });
  27. if (onCreated) {
  28. onCreated(pathSimplifierRef.current!);
  29. }
  30. if (props.pathData) {
  31. console.log(props.pathData.map((item) => ({ name: item.name || '路线', path: item.path })));
  32. pathSimplifierRef.current?.setData(
  33. props.pathData.map((item) => ({ name: item.name || '路线', path: item.path })),
  34. );
  35. setLoading(true);
  36. }
  37. },
  38. [props],
  39. );
  40. const loadUI = () => {
  41. if ((window as any).AMapUI) {
  42. (window as any).AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], (path: PathSimplifier) => {
  43. if (!path.supportCanvas) {
  44. console.warn('当前环境不支持 Canvas!');
  45. return;
  46. }
  47. pathSimplifier(path);
  48. });
  49. }
  50. };
  51. const renderChildren = () => {
  52. return React.Children.map(props.children, (child, index) => {
  53. if (child) {
  54. if (typeof child === 'string') {
  55. return child;
  56. } else {
  57. return React.cloneElement(child as any, {
  58. __pathSimplifier__: pathSimplifierRef.current,
  59. navKey: index,
  60. });
  61. }
  62. }
  63. return child;
  64. });
  65. };
  66. const clear = () => {
  67. if (pathSimplifierRef.current) {
  68. setLoading(false);
  69. pathSimplifierRef.current!.clearPathNavigators();
  70. pathSimplifierRef.current?.setData([]);
  71. props.__map__.remove(pathSimplifierRef.current);
  72. }
  73. };
  74. useEffect(() => {
  75. if (pathSimplifierRef.current) {
  76. if (props.pathData && props.pathData.length) {
  77. setLoading(false);
  78. setTimeout(() => {
  79. pathSimplifierRef.current?.setData(
  80. props.pathData!.map((item) => ({ name: item.name || '路线', path: item.path })),
  81. );
  82. setLoading(true);
  83. }, 10);
  84. } else {
  85. setLoading(false);
  86. pathSimplifierRef.current.setData([]);
  87. }
  88. }
  89. }, [props.pathData]);
  90. useEffect(() => {
  91. if (__map__) {
  92. loadUI();
  93. }
  94. }, [__map__]);
  95. useEffect(() => {
  96. return () => {
  97. if (props.__map__) {
  98. clear();
  99. }
  100. };
  101. }, []);
  102. return <>{loading && renderChildren()}</>;
  103. };
  104. PathSimplifier.PathNavigator = PathNavigator;
  105. export default PathSimplifier;