Invoke setter with default when prop missing (#2560)

This commit is contained in:
felixpalmer 2025-12-17 05:53:06 +01:00 committed by GitHub
parent 465bd4bbc7
commit e7d2ee7142
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 4 deletions

View File

@ -85,6 +85,16 @@ export type MapboxProps = Partial<ViewState> &
const DEFAULT_STYLE = {version: 8, sources: {}, layers: []} as StyleSpecification;
const DEFAULT_SETTINGS = {
minZoom: 0,
maxZoom: 22,
minPitch: 0,
maxPitch: 85,
maxBounds: [-180, -85.051129, 180, 85.051129],
projection: 'mercator',
renderWorldCopies: true
};
const pointerEvents = {
mousedown: 'onMouseDown',
mouseup: 'onMouseUp',
@ -461,10 +471,13 @@ export default class Mapbox {
const map = this._map;
let changed = false;
for (const propName of settingNames) {
if (propName in nextProps && !deepEqual(nextProps[propName], currProps[propName])) {
const propPresent = propName in nextProps || propName in currProps;
if (propPresent && !deepEqual(nextProps[propName], currProps[propName])) {
changed = true;
const nextValue = propName in nextProps ? nextProps[propName] : DEFAULT_SETTINGS[propName];
const setter = map[`set${propName[0].toUpperCase()}${propName.slice(1)}`];
setter?.call(map, nextProps[propName]);
setter?.call(map, nextValue);
}
}
return changed;

View File

@ -80,6 +80,16 @@ export type MaplibreProps = Partial<ViewState> &
const DEFAULT_STYLE = {version: 8, sources: {}, layers: []} as StyleSpecification;
const DEFAULT_SETTINGS = {
minZoom: 0,
maxZoom: 22,
minPitch: 0,
maxPitch: 85,
maxBounds: [-180, -85.051129, 180, 85.051129],
projection: 'mercator',
renderWorldCopies: true
};
const pointerEvents = {
mousedown: 'onMouseDown',
mouseup: 'onMouseUp',
@ -413,10 +423,13 @@ export default class Maplibre {
const map = this._map;
let changed = false;
for (const propName of settingNames) {
if (propName in nextProps && !deepEqual(nextProps[propName], currProps[propName])) {
const propPresent = propName in nextProps || propName in currProps;
if (propPresent && !deepEqual(nextProps[propName], currProps[propName])) {
changed = true;
const nextValue = propName in nextProps ? nextProps[propName] : DEFAULT_SETTINGS[propName];
const setter = map[`set${propName[0].toUpperCase()}${propName.slice(1)}`];
setter?.call(map, nextProps[propName]);
setter?.call(map, nextValue);
}
}
return changed;