-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2317 from terrestris/split-digitize-button
Add SelectFeaturesButton
- Loading branch information
Showing
6 changed files
with
363 additions
and
35 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
76 changes: 76 additions & 0 deletions
76
src/Button/SelectFeaturesButton/SelectFeaturesButton.example.md
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,76 @@ | ||
This demonstrates the use of the SelectFeaturesButton. | ||
|
||
```jsx | ||
import { useEffect, useState } from 'react'; | ||
|
||
import OlMap from 'ol/Map'; | ||
import OlView from 'ol/View'; | ||
import OlLayerTile from 'ol/layer/Tile'; | ||
import OlSourceOsm from 'ol/source/OSM'; | ||
import OlVectorLayer from 'ol/layer/Vector'; | ||
import OlVectorSource from 'ol/source/Vector'; | ||
import OlFormatGeoJSON from 'ol/format/GeoJSON'; | ||
import { fromLonLat } from 'ol/proj'; | ||
|
||
import MapContext from '@terrestris/react-geo/Context/MapContext/MapContext' | ||
import MapComponent from '@terrestris/react-geo/Map/MapComponent/MapComponent'; | ||
import SelectFeaturesButton from '@terrestris/react-geo/Button/SelectFeaturesButton/SelectFeaturesButton'; | ||
|
||
import federalStates from '../../../assets/federal-states-ger.json'; | ||
|
||
const format = new OlFormatGeoJSON(); | ||
const features = format.readFeatures(federalStates); | ||
|
||
const SelectFeaturesButtonExample = () => { | ||
|
||
const [map, setMap] = useState(); | ||
const [layer, setLayer] = useState(); | ||
|
||
useEffect(() => { | ||
const layer = new OlVectorLayer({ | ||
source: new OlVectorSource({ | ||
features | ||
}) | ||
}); | ||
|
||
setLayer(layer); | ||
|
||
setMap(new OlMap({ | ||
layers: [ | ||
new OlLayerTile({ | ||
name: 'OSM', | ||
source: new OlSourceOsm() | ||
}), | ||
layer | ||
], | ||
view: new OlView({ | ||
center: fromLonLat([8, 50]), | ||
zoom: 4 | ||
}) | ||
})); | ||
}, []) | ||
|
||
if (!map) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<MapContext.Provider value={map}> | ||
<MapComponent | ||
map={map} | ||
style={{ | ||
height: '400px' | ||
}} | ||
/> | ||
|
||
<SelectFeaturesButton layers={[layer]}> | ||
Select feature | ||
</SelectFeaturesButton> | ||
</MapContext.Provider> | ||
</div> | ||
); | ||
} | ||
|
||
<SelectFeaturesButtonExample /> | ||
``` |
85 changes: 85 additions & 0 deletions
85
src/Button/SelectFeaturesButton/SelectFeaturesButton.spec.tsx
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,85 @@ | ||
import * as React from 'react'; | ||
import { screen, within } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import OlMap from 'ol/Map'; | ||
import OlView from 'ol/View'; | ||
import OlFeature from 'ol/Feature'; | ||
import OlPoint from 'ol/geom/Point'; | ||
import OlVectorLayer from 'ol/layer/Vector'; | ||
import OlVectorSource from 'ol/source/Vector'; | ||
import { SelectEvent as OlSelectEvent } from 'ol/interaction/Select'; | ||
|
||
import DrawButton from '../DrawButton/DrawButton'; | ||
import { clickMap, renderInMapContext } from '../../Util/rtlTestUtils'; | ||
import SelectFeaturesButton from './SelectFeaturesButton'; | ||
|
||
describe('<SelectFeaturesButton />', () => { | ||
|
||
const coord = [829729, 6708850]; | ||
let map: OlMap; | ||
let layer: OlVectorLayer<OlVectorSource<OlPoint>>; | ||
let feature: OlFeature<OlPoint>; | ||
|
||
beforeEach(() => { | ||
feature = new OlFeature(new OlPoint(coord)); | ||
|
||
layer = new OlVectorLayer({ | ||
source: new OlVectorSource({ | ||
features: [feature] | ||
}) | ||
}); | ||
|
||
map = new OlMap({ | ||
view: new OlView({ | ||
center: coord, | ||
zoom: 10 | ||
}), | ||
controls: [], | ||
layers: [ | ||
layer | ||
] | ||
}); | ||
}); | ||
|
||
describe('#Basics', () => { | ||
|
||
it('is defined', () => { | ||
expect(DrawButton).not.toBeUndefined(); | ||
}); | ||
|
||
it('can be rendered', () => { | ||
const { container } = renderInMapContext(map, <SelectFeaturesButton layers={[layer]} />); | ||
|
||
const button = within(container).getByRole('button'); | ||
expect(button).toBeVisible(); | ||
}); | ||
}); | ||
|
||
describe('#Selection', () => { | ||
it('calls the listener', async () => { | ||
const selectSpy = jest.fn(); | ||
|
||
renderInMapContext(map, <SelectFeaturesButton layers={[layer]} onFeatureSelect={selectSpy} />); | ||
|
||
map.renderSync(); | ||
|
||
const button = screen.getByRole('button'); | ||
userEvent.click(button); | ||
|
||
const mock = jest.spyOn(map, 'forEachFeatureAtPixel').mockImplementation((pixel, callback) => { | ||
if (pixel[0] === 200 && pixel[1] === 200) { | ||
callback(feature, layer, null); | ||
} | ||
}); | ||
|
||
await clickMap(map, 200, 200); | ||
|
||
expect(selectSpy).toBeCalled(); | ||
const event: OlSelectEvent = selectSpy.mock.calls[0][0]; | ||
expect(event.selected).toEqual([feature]); | ||
|
||
mock.mockRestore(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.