-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c8673f
commit 687e6be
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './map'; | ||
export * from './marker'; | ||
export * from './source'; | ||
export * from './hooks/useMap'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { GeoJSONSource } from 'mapbox-gl'; | ||
|
||
export const setterMap = { | ||
data(data: any, source: GeoJSONSource) { | ||
if (!source) return; | ||
|
||
source.setData(data); | ||
}, | ||
}; | ||
|
||
export const converterMap = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Source } from './source'; | ||
export type { SourceProps } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { forwardRef, useEffect, useState, useImperativeHandle } from 'react'; | ||
|
||
import { useMap } from '@/hooks/useMap'; | ||
import { useReact } from '@/hooks/useReact'; | ||
|
||
import { setterMap, converterMap } from './config'; | ||
|
||
import type { FC } from 'react'; | ||
import type { GeoJSONSource } from 'mapbox-gl'; | ||
import type { SourceProps, EventMapping } from './types'; | ||
|
||
export const Source: FC<SourceProps> = forwardRef<GeoJSONSource, SourceProps>((props, ref) => { | ||
const { id, ...rest } = props; | ||
const map = useMap(); | ||
const [source, setSource] = useState<GeoJSONSource>(); | ||
|
||
useImperativeHandle(ref, () => source as GeoJSONSource, [source]); | ||
|
||
const { onInstanceCreated } = useReact<SourceProps, any, EventMapping>(props, { | ||
ins: source, | ||
events: {}, | ||
setterMap, | ||
converterMap, | ||
unmount: () => { | ||
map && map.removeSource(id); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (map) { | ||
map.addSource(props.id, { | ||
...rest, | ||
type: 'geojson', | ||
}); | ||
|
||
setSource(map.getSource(id) as GeoJSONSource); | ||
onInstanceCreated(); | ||
} | ||
}, [map]); | ||
|
||
return null; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { GeoJSONSourceOptions } from 'mapbox-gl'; | ||
|
||
export type SourceEvents = {}; | ||
|
||
export type EventMapping = { [T in keyof SourceEvents]: string }; | ||
|
||
export interface SourceProps extends GeoJSONSourceOptions { | ||
id: string; | ||
} |