File size: 916 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React from 'react';
import GoogleMapsContext from './GoogleMapsContext';

type Subtract<TProps, TSubstractedProps> = Omit<
  TProps,
  keyof TSubstractedProps
>;

export interface WithGoogleMapsProps {
  google: typeof google;
  googleMapsInstance: google.maps.Map;
}

const withGoogleMaps = <TProps extends WithGoogleMapsProps>(
  Wrapped: React.ComponentType<TProps>
) => {
  const WithGoogleMaps: React.FC<Subtract<TProps, WithGoogleMapsProps>> = (
    props
  ) => (
    <GoogleMapsContext.Consumer>
      {({ google, instance }) => (
        <Wrapped
          // @TODO: remove the cast once TypeScript fixes the issue
          // https://github.com/Microsoft/TypeScript/issues/28938
          {...(props as TProps)}
          google={google}
          googleMapsInstance={instance}
        />
      )}
    </GoogleMapsContext.Consumer>
  );

  return WithGoogleMaps;
};

export default withGoogleMaps;