Skip to content

Commit 706dac4

Browse files
committed
simplify
1 parent 76becce commit 706dac4

File tree

12 files changed

+42
-64
lines changed

12 files changed

+42
-64
lines changed

modules/react-mapbox/src/components/marker.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,8 @@ export const Marker = memo(
126126
}
127127
const classNameDiff = compareClassNames(oldProps.className, props.className);
128128
if (classNameDiff) {
129-
for (const c of classNameDiff[0]) {
130-
marker.addClassName(c);
131-
}
132-
for (const c of classNameDiff[1]) {
133-
marker.removeClassName(c);
129+
for (const c of classNameDiff) {
130+
marker.toggleClassName(c);
134131
}
135132
}
136133

modules/react-mapbox/src/components/popup.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,16 @@ export const Popup = memo(
7575
popup.setLngLat([props.longitude, props.latitude]);
7676
}
7777
if (props.offset && !deepEqual(oldProps.offset, props.offset)) {
78+
popup.options.anchor = props.anchor;
7879
popup.setOffset(props.offset);
7980
}
8081
if (oldProps.anchor !== props.anchor || oldProps.maxWidth !== props.maxWidth) {
8182
popup.setMaxWidth(props.maxWidth);
8283
}
8384
const classNameDiff = compareClassNames(oldProps.className, props.className);
8485
if (classNameDiff) {
85-
for (const c of classNameDiff[0]) {
86-
popup.addClassName(c);
87-
}
88-
for (const c of classNameDiff[1]) {
89-
popup.removeClassName(c);
86+
for (const c of classNameDiff) {
87+
popup.toggleClassName(c);
9088
}
9189
}
9290
thisRef.current.props = props;

modules/react-mapbox/src/utils/compare-class-names.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
export function compareClassNames(
33
prevClassName: string | undefined,
44
nextClassName: string | undefined
5-
): [added: string[], removed: string[]] | null {
5+
): string[] | null {
66
if (prevClassName === nextClassName) {
77
return null;
88
}
99

1010
const prevClassList = getClassList(prevClassName);
1111
const nextClassList = getClassList(nextClassName);
12-
const added: string[] = [];
13-
const removed: string[] = [];
14-
15-
for (const c of prevClassList) {
16-
if (!nextClassList.has(c)) {
17-
removed.push(c);
18-
}
19-
}
20-
21-
if (removed.length === 0 && prevClassList.size === nextClassList.size) {
22-
return null;
23-
}
12+
const diff: string[] = [];
2413

2514
for (const c of nextClassList) {
2615
if (!prevClassList.has(c)) {
27-
added.push(c);
16+
diff.push(c);
2817
}
2918
}
30-
31-
return [added, removed];
19+
for (const c of prevClassList) {
20+
if (!nextClassList.has(c)) {
21+
diff.push(c);
22+
}
23+
}
24+
return diff.length === 0 ? null : diff;
3225
}
3326

3427
function getClassList(className: string | undefined) {

modules/react-mapbox/test/components/marker.spec.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test('Marker', async t => {
4949
offset={[0, 1]}
5050
rotation={30}
5151
draggable
52+
className="classA"
5253
pitchAlignment="map"
5354
rotationAlignment="map"
5455
onDragStart={() => (callbackType = 'dragstart')}
@@ -64,6 +65,7 @@ test('Marker', async t => {
6465
t.not(rotation, marker.getRotation(), 'rotation is updated');
6566
t.not(pitchAlignment, marker.getPitchAlignment(), 'pitchAlignment is updated');
6667
t.not(rotationAlignment, marker.getRotationAlignment(), 'rotationAlignment is updated');
68+
t.ok(marker._element.classList.contains('classA'), 'className is updated');
6769

6870
marker.fire('dragstart');
6971
t.is(callbackType, 'dragstart', 'onDragStart called');

modules/react-mapbox/test/components/popup.spec.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ test('Popup', async t => {
6767
</Map>
6868
);
6969
await sleep(1);
70-
71-
t.is(popup.options.className, 'classA', 'className is updated');
70+
t.ok(popup._container.classList.contains('classA'), 'className is updated');
7271

7372
root.unmount();
7473
t.end();

modules/react-mapbox/test/utils/compare-class-names.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ test('compareClassNames', t => {
1919
title: 'Addition',
2020
prevClassName: undefined,
2121
nextClassName: 'marker',
22-
output: [['marker'], []]
22+
output: ['marker']
2323
},
2424
{
2525
title: 'Addition',
2626
prevClassName: 'marker',
2727
nextClassName: 'marker active',
28-
output: [['active'], []]
28+
output: ['active']
2929
},
3030
{
3131
title: 'Removal',
3232
prevClassName: 'marker active',
3333
nextClassName: 'marker',
34-
output: [[], ['active']]
34+
output: ['active']
3535
},
3636
{
3737
title: 'Multiple addition & removal',
3838
prevClassName: 'marker active',
3939
nextClassName: 'marker hovered hidden',
40-
output: [['hovered', 'hidden'], ['active']]
40+
output: ['hovered', 'hidden', 'active']
4141
}
4242
];
4343

modules/react-maplibre/src/components/marker.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,8 @@ export const Marker = memo(
126126
}
127127
const classNameDiff = compareClassNames(oldProps.className, props.className);
128128
if (classNameDiff) {
129-
for (const c of classNameDiff[0]) {
130-
marker.addClassName(c);
131-
}
132-
for (const c of classNameDiff[1]) {
133-
marker.removeClassName(c);
129+
for (const c of classNameDiff) {
130+
marker.toggleClassName(c);
134131
}
135132
}
136133

modules/react-maplibre/src/components/popup.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ export const Popup = memo(
7878
popup.setOffset(props.offset);
7979
}
8080
if (oldProps.anchor !== props.anchor || oldProps.maxWidth !== props.maxWidth) {
81+
popup.options.anchor = props.anchor;
8182
popup.setMaxWidth(props.maxWidth);
8283
}
8384
const classNameDiff = compareClassNames(oldProps.className, props.className);
8485
if (classNameDiff) {
85-
for (const c of classNameDiff[0]) {
86-
popup.addClassName(c);
87-
}
88-
for (const c of classNameDiff[1]) {
89-
popup.removeClassName(c);
86+
for (const c of classNameDiff) {
87+
popup.toggleClassName(c);
9088
}
9189
}
9290
thisRef.current.props = props;

modules/react-maplibre/src/utils/compare-class-names.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
export function compareClassNames(
33
prevClassName: string | undefined,
44
nextClassName: string | undefined
5-
): [added: string[], removed: string[]] | null {
5+
): string[] | null {
66
if (prevClassName === nextClassName) {
77
return null;
88
}
99

1010
const prevClassList = getClassList(prevClassName);
1111
const nextClassList = getClassList(nextClassName);
12-
const added: string[] = [];
13-
const removed: string[] = [];
14-
15-
for (const c of prevClassList) {
16-
if (!nextClassList.has(c)) {
17-
removed.push(c);
18-
}
19-
}
20-
21-
if (removed.length === 0 && prevClassList.size === nextClassList.size) {
22-
return null;
23-
}
12+
const diff: string[] = [];
2413

2514
for (const c of nextClassList) {
2615
if (!prevClassList.has(c)) {
27-
added.push(c);
16+
diff.push(c);
2817
}
2918
}
30-
31-
return [added, removed];
19+
for (const c of prevClassList) {
20+
if (!nextClassList.has(c)) {
21+
diff.push(c);
22+
}
23+
}
24+
return diff.length === 0 ? null : diff;
3225
}
3326

3427
function getClassList(className: string | undefined) {

modules/react-maplibre/test/components/marker.spec.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test('Marker', async t => {
4848
offset={[0, 1]}
4949
rotation={30}
5050
draggable
51+
className="classA"
5152
pitchAlignment="viewport"
5253
rotationAlignment="viewport"
5354
onDragStart={() => (callbackType = 'dragstart')}
@@ -63,6 +64,7 @@ test('Marker', async t => {
6364
t.not(rotation, marker.getRotation(), 'rotation is updated');
6465
t.not(pitchAlignment, marker.getPitchAlignment(), 'pitchAlignment is updated');
6566
t.not(rotationAlignment, marker.getRotationAlignment(), 'rotationAlignment is updated');
67+
t.ok(marker._element.classList.contains('classA'), 'className is updated');
6668

6769
marker.fire('dragstart');
6870
t.is(callbackType, 'dragstart', 'onDragStart called');

0 commit comments

Comments
 (0)