forked from PaulLeCam/react-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
32 lines (24 loc) · 1.07 KB
/
context.js
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
// @flow
import hoistNonReactStatics from 'hoist-non-react-statics'
import React, { createContext, forwardRef, type AbstractComponent } from 'react'
import type { LeafletContext } from './types'
const { Consumer, Provider } = createContext<LeafletContext>({})
export const LeafletConsumer = Consumer
export const LeafletProvider = Provider
export const withLeaflet = <Config: { leaflet: LeafletContext }, Instance>(
WrappedComponent: AbstractComponent<Config, Instance>,
): AbstractComponent<$Diff<Config, { leaflet: LeafletContext }>, Instance> => {
const WithLeafletComponent = (props, ref) => (
<Consumer>
{(leaflet: LeafletContext) => (
<WrappedComponent {...props} leaflet={leaflet} ref={ref} />
)}
</Consumer>
)
const name = // flowlint-next-line sketchy-null-string:off
WrappedComponent.displayName || WrappedComponent.name || 'Component'
WithLeafletComponent.displayName = `Leaflet(${name})`
const LeafletComponent = forwardRef(WithLeafletComponent)
hoistNonReactStatics(LeafletComponent, WrappedComponent)
return LeafletComponent
}