(props: RectangleProps)
| 40 | export type RectangleRef = Ref<google.maps.Rectangle | null>; |
| 41 | |
| 42 | function useRectangle(props: RectangleProps) { |
| 43 | const { |
| 44 | onClick, |
| 45 | onDrag, |
| 46 | onDragStart, |
| 47 | onDragEnd, |
| 48 | onMouseOver, |
| 49 | onMouseOut, |
| 50 | onBoundsChanged, |
| 51 | bounds, |
| 52 | defaultBounds, |
| 53 | ...destructuredOptions |
| 54 | } = props; |
| 55 | |
| 56 | const [rectangle, setRectangle] = useState<google.maps.Rectangle | null>( |
| 57 | null |
| 58 | ); |
| 59 | const map = useMap(); |
| 60 | |
| 61 | // Memoize options with automatic inference of clickable/draggable/editable |
| 62 | const rectangleOptions = useMemoized( |
| 63 | { |
| 64 | ...destructuredOptions, |
| 65 | clickable: destructuredOptions.clickable ?? Boolean(onClick), |
| 66 | draggable: |
| 67 | destructuredOptions.draggable ?? |
| 68 | Boolean(onDrag || onDragStart || onDragEnd || onBoundsChanged), |
| 69 | editable: destructuredOptions.editable ?? Boolean(onBoundsChanged) |
| 70 | }, |
| 71 | isDeepEqual |
| 72 | ); |
| 73 | |
| 74 | useEffect(() => { |
| 75 | if (!map) { |
| 76 | if (map === undefined) |
| 77 | console.error('<Rectangle> has to be inside a Map component.'); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | const newRectangle = new google.maps.Rectangle({ |
| 83 | ...rectangleOptions, |
| 84 | bounds: bounds ?? defaultBounds |
| 85 | }); |
| 86 | newRectangle.setMap(map); |
| 87 | // eslint-disable-next-line react-hooks/set-state-in-effect -- intentional to sync the imperative instance with state |
| 88 | setRectangle(newRectangle); |
| 89 | |
| 90 | return () => { |
| 91 | newRectangle.setMap(null); |
| 92 | setRectangle(null); |
| 93 | }; |
| 94 | // eslint-disable-next-line react-hooks/exhaustive-deps -- rectangle options are handled separately to avoid recreating the instance |
| 95 | }, [map]); |
| 96 | |
| 97 | useMapsEventListener(rectangle, 'click', onClick); |
| 98 | useMapsEventListener(rectangle, 'drag', onDrag); |
| 99 | useMapsEventListener(rectangle, 'dragstart', onDragStart); |
no test coverage detected