-
Notifications
You must be signed in to change notification settings - Fork 51
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
0b591f6
commit e207dd6
Showing
8 changed files
with
145 additions
and
1 deletion.
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
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
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,19 @@ | ||
A `LanguageControl` that modifies the layers of the map style to use the 'text-field' that matches the browser language. | ||
⚠️ Require the package `@mapbox/mapbox-gl-language` (`npm install --save @mapbox/mapbox-gl-language`) | ||
|
||
```jsx | ||
import React from 'react'; | ||
import MapGL, { LangugageControl } from '@urbica/react-map-gl'; | ||
import 'mapbox-gl/dist/mapbox-gl.css'; | ||
|
||
<MapGL | ||
style={{ width: '100%', height: '400px' }} | ||
mapStyle='mapbox://styles/mapbox/light-v9' | ||
accessToken={MAPBOX_ACCESS_TOKEN} | ||
latitude={37.78} | ||
longitude={-122.41} | ||
zoom={11} | ||
> | ||
<LanguageControl options={{ defaultLanguage: 'fr' }} /> | ||
</MapGL>; | ||
``` |
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,74 @@ | ||
// @flow | ||
|
||
import { PureComponent, createElement } from 'react'; | ||
import type MapboxMap from 'mapbox-gl/src/ui/map'; | ||
import type MapboxLanguageControl from '@mapbox/mapbox-gl-language/index'; | ||
|
||
import MapboxLanguage from '@mapbox/mapbox-gl-language'; | ||
import MapContext from '../MapContext'; | ||
|
||
type Props = { | ||
/* Options to configure the plugin. */ | ||
options?: { | ||
/* List of supported languages */ | ||
supportedLanguages?: string[], | ||
/* Custom style transformation to apply */ | ||
languageTransform?: Function, | ||
/** | ||
* RegExp to match if a text-field is a language field | ||
* (optional, default /^\{name/) | ||
*/ | ||
languageField?: RegExp, | ||
/* Given a language choose the field in the vector tiles */ | ||
getLanguageField?: Function, | ||
/* Name of the source that contains the different languages. */ | ||
languageSource?: string, | ||
/* Name of the default language to initialize style after loading. */ | ||
defaultLanguage?: string | ||
} | ||
}; | ||
|
||
/** | ||
* Adds support for switching the language of your map style. | ||
*/ | ||
class LanguageControl extends PureComponent<Props> { | ||
_map: MapboxMap; | ||
|
||
_control: MapboxLanguageControl; | ||
|
||
static defaultProps = {}; | ||
|
||
componentDidMount() { | ||
const map: MapboxMap = this._map; | ||
|
||
const control: MapboxLanguageControl = new MapboxLanguage( | ||
this.props.options | ||
); | ||
|
||
map.addControl(control); | ||
this._control = control; | ||
} | ||
|
||
componentWillUnmount() { | ||
if (!this._map || !this._control) { | ||
return; | ||
} | ||
|
||
this._map.removeControl(this._control); | ||
} | ||
|
||
getControl() { | ||
return this._control; | ||
} | ||
|
||
render() { | ||
return createElement(MapContext.Consumer, {}, (map) => { | ||
if (map) { | ||
this._map = map; | ||
} | ||
return null; | ||
}); | ||
} | ||
} | ||
|
||
export default LanguageControl; |
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,25 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import MapGL, { LanguageControl } from '../..'; | ||
|
||
test('render', () => { | ||
const wrapper = mount( | ||
<MapGL latitude={0} longitude={0} zoom={0}> | ||
<LanguageControl /> | ||
</MapGL> | ||
); | ||
|
||
expect(wrapper.find('LanguageControl').exists()).toBe(true); | ||
|
||
wrapper.unmount(); | ||
expect(wrapper.find('LanguageControl').exists()).toBe(false); | ||
}); | ||
|
||
test('throws', () => { | ||
console.error = jest.fn(); | ||
|
||
expect(() => mount(<LanguageControl />)).toThrow(); | ||
expect(console.error).toHaveBeenCalled(); | ||
}); |
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
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
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