Skip to content

Commit ee8c7ff

Browse files
authored
fix: improve useFeatureEditor change handler to correctly process features from events (#243)
1 parent c68a495 commit ee8c7ff

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

packages/react-maplibre/src/components/MlGlobeButton/MlGlobeButton.cy.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@ describe('MlGlobeButton', () => {
2121

2222
it('shows MapIcon as start state and toggles between MapIcon and PublicIcon', () => {
2323
mount(<CatalogueDemo />);
24-
cy.window().should((win) => expect((win as any)._map).to.exist);
24+
cy.window()
25+
.should((win) => expect((win as any)._map).to.exist)
26+
.then((win) => {
27+
const map = (win as any)._map;
28+
return new Cypress.Promise((resolve) => {
29+
if (map.loaded()) {
30+
resolve();
31+
} else {
32+
map.on('load', resolve);
33+
}
34+
});
35+
});
2536

26-
cy.get('[data-testid="MapIcon"]').should('exist');
37+
cy.get('[data-testid="MapIcon"]', { timeout: 10000 }).should('exist');
2738
cy.get('[data-testid="PublicIcon"]').should('not.exist');
2839
cy.get('button').click();
2940
cy.get('[data-testid="PublicIcon"]').should('exist');
@@ -35,7 +46,18 @@ describe('MlGlobeButton', () => {
3546

3647
it('changes the projection on the map instance attached to window', () => {
3748
mount(<CatalogueDemo />);
38-
cy.window().should((win) => expect((win as any)._map).to.exist);
49+
cy.window()
50+
.should((win) => expect((win as any)._map).to.exist)
51+
.then((win) => {
52+
const map = (win as any)._map;
53+
return new Cypress.Promise((resolve) => {
54+
if (map.loaded()) {
55+
resolve();
56+
} else {
57+
map.on('load', resolve);
58+
}
59+
});
60+
});
3961

4062
cy.window().should((win) => {
4163
const map = (win as any)._map;

packages/react-maplibre/src/components/MlNavigationCompass/MlNavigationCompass.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface MlNavigationCompassProps {
7272
needleStyle?: CSSProperties;
7373
}
7474
/**
75-
* Navigation component that displays a compass component which indicates the current oriantation of the map it is registered for and offers controls to turn the bearing 90° left/right or reset north to point up.
75+
* Navigation component that displays a compass component which indicates the current orientation of the map it is registered for and offers controls to turn the bearing 90° left/right or reset north to point up.
7676
*
7777
* All style props are applied using @mui/material/styled to allow more complex css selectors.
7878
*

packages/react-maplibre/src/hooks/useFeatureEditor/useFeatureEditor.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ const useFeatureEditor = (props: useFeatureEditorProps) => {
112112
useEffect(() => {
113113
if (!mapHook.map || !drawToolsReady) return;
114114

115-
const changeHandler = () => {
116-
if (draw.current) {
117-
// update drawnFeatures state object
118-
const currentFeatureCollection = draw.current.getAll?.();
119-
setFeature(currentFeatureCollection?.features);
120-
if (typeof props.onChange === 'function') {
121-
props.onChange(currentFeatureCollection?.features);
122-
}
115+
const changeHandler = (ev: unknown) => {
116+
let features: Feature[] = [];
117+
118+
if (ev && typeof ev === 'object' && 'features' in ev && ev.features) {
119+
features = ev.features as Feature[];
120+
}
121+
122+
setFeature(features);
123+
124+
if (props.onChange) {
125+
props.onChange(features);
123126
}
124127
};
125128

0 commit comments

Comments
 (0)