index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. pathSimplifierRef.current?.setData(
  32. props.pathData.map((item) => ({ name: item.name || '路线', path: item.path })),
  33. );
  34. setLoading(true);
  35. }
  36. },
  37. [props],
  38. );
  39. const loadUI = () => {
  40. if ((window as any).AMapUI) {
  41. (window as any).AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], (path: PathSimplifier) => {
  42. if (!path.supportCanvas) {
  43. console.warn('当前环境不支持 Canvas!');
  44. return;
  45. }
  46. pathSimplifier(path);
  47. });
  48. }
  49. };
  50. const renderChildren = () => {
  51. return React.Children.map(props.children, (child, index) => {
  52. if (child) {
  53. if (typeof child === 'string') {
  54. return child;
  55. } else {
  56. return React.cloneElement(child as any, {
  57. __pathSimplifier__: pathSimplifierRef.current,
  58. navKey: index,
  59. });
  60. }
  61. }
  62. return child;
  63. });
  64. };
  65. const clear = () => {
  66. if (pathSimplifierRef.current) {
  67. setLoading(false);
  68. pathSimplifierRef.current!.clearPathNavigators();
  69. pathSimplifierRef.current?.setData([]);
  70. props.__map__.remove(pathSimplifierRef.current);
  71. }
  72. };
  73. useEffect(() => {
  74. if (pathSimplifierRef.current) {
  75. if (props.pathData && props.pathData.length) {
  76. setLoading(false);
  77. setTimeout(() => {
  78. pathSimplifierRef.current?.setData(
  79. props.pathData!.map((item) => ({ name: item.name || '路线', path: item.path })),
  80. );
  81. setLoading(true);
  82. }, 10);
  83. } else {
  84. setLoading(false);
  85. pathSimplifierRef.current.setData([]);
  86. }
  87. }
  88. }, [props.pathData]);
  89. useEffect(() => {
  90. if (__map__) {
  91. loadUI();
  92. }
  93. }, [__map__]);
  94. useEffect(() => {
  95. return () => {
  96. if (props.__map__) {
  97. clear();
  98. }
  99. };
  100. }, []);
  101. return <>{loading && renderChildren()}</>;
  102. };
  103. PathSimplifier.PathNavigator = PathNavigator;
  104. export default PathSimplifier;