-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/migrate to ts #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||||||||||||||
| import React, { Component, createRef } from 'react'; | ||||||||||||||||||
| import { MapContainer, TileLayer, FeatureGroup } from 'react-leaflet'; | ||||||||||||||||||
| import * as L from 'leaflet'; | ||||||||||||||||||
| import { EditControl } from '../../src'; | ||||||||||||||||||
|
|
||||||||||||||||||
| delete (L.Icon.Default.prototype as any)._getIconUrl; | ||||||||||||||||||
| L.Icon.Default.mergeOptions({ | ||||||||||||||||||
| iconRetinaUrl: | ||||||||||||||||||
| 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0/images/marker-icon.png', | ||||||||||||||||||
| iconUrl: | ||||||||||||||||||
| 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0/images/marker-icon.png', | ||||||||||||||||||
| shadowUrl: | ||||||||||||||||||
| 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0/images/marker-shadow.png', | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| export default class EditControlExample extends Component<{ onChange?: (g: any) => void }> { | ||||||||||||||||||
| featureGroupRef = createRef<L.FeatureGroup>(); | ||||||||||||||||||
|
|
||||||||||||||||||
| _onEdited = (e: L.DrawEvents.Edited) => { | ||||||||||||||||||
| let numEdited = 0; | ||||||||||||||||||
| e.layers.eachLayer(() => { | ||||||||||||||||||
| numEdited += 1; | ||||||||||||||||||
| }); | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log(`_onEdited: edited ${numEdited} layers`, e); | ||||||||||||||||||
| this._onChange(); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onCreated = (e: L.DrawEvents.Created) => { | ||||||||||||||||||
| const type = e.layerType; | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onCreated:', type, e); | ||||||||||||||||||
| this._onChange(); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onDeleted = (e: L.DrawEvents.Deleted) => { | ||||||||||||||||||
| let numDeleted = 0; | ||||||||||||||||||
| e.layers.eachLayer(() => { | ||||||||||||||||||
| numDeleted += 1; | ||||||||||||||||||
| }); | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log(`onDeleted: removed ${numDeleted} layers`, e); | ||||||||||||||||||
| this._onChange(); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onMounted = (drawControl: L.Control.Draw) => { | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onMounted', drawControl); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onEditStart = (e: L.DrawEvents.EditStart) => { | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onEditStart', e); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onEditStop = (e: L.DrawEvents.EditStop) => { | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onEditStop', e); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onDeleteStart = (e: L.DrawEvents.DeleteStart) => { | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onDeleteStart', e); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| _onDeleteStop = (e: L.DrawEvents.DeleteStop) => { | ||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||
| console.log('_onDeleteStop', e); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| render() { | ||||||||||||||||||
| const fgInstance = this.featureGroupRef.current as unknown as L.FeatureGroup | undefined; | ||||||||||||||||||
| return ( | ||||||||||||||||||
| <MapContainer center={[37.8189, -122.4786]} zoom={13} zoomControl={false}> | ||||||||||||||||||
| <TileLayer | ||||||||||||||||||
| attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' | ||||||||||||||||||
| url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" | ||||||||||||||||||
| /> | ||||||||||||||||||
| <FeatureGroup ref={this.featureGroupRef as any}> | ||||||||||||||||||
|
||||||||||||||||||
| <FeatureGroup ref={this.featureGroupRef as any}> | |
| <FeatureGroup | |
| ref={(ref) => { | |
| // Assign both the class ref and _editableFG for compatibility | |
| (this.featureGroupRef as React.MutableRefObject<L.FeatureGroup | null>).current = ref; | |
| this._editableFG = ref; | |
| }} | |
| > |
Copilot
AI
Aug 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using (fgInstance as any)._leaflet_id to access private Leaflet properties is fragile and defeats TypeScript's type safety. Consider using a proper type guard or checking if the instance has the expected properties through a safer approach.
| {fgInstance && (fgInstance as any)._leaflet_id && ( | |
| {fgInstance && '_leaflet_id' in fgInstance && ( |
Copilot
AI
Aug 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using as any to access the toGeoJSON() method bypasses TypeScript's type checking. Consider properly typing _editableFG or using a type assertion that maintains some level of type safety.
| const geojsonData = (this._editableFG as any).toGeoJSON(); | |
| const geojsonData = (this._editableFG as L.FeatureGroup).toGeoJSON(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "module": "ESNext", | ||
| "moduleResolution": "node", | ||
| "target": "ES2020", | ||
| "jsx": "react-jsx", | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "esModuleInterop": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "sourceMap": true | ||
| }, | ||
| "include": ["./**/*"], | ||
| "exclude": ["dist", "node_modules"] | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type assertion
as unknown as L.FeatureGroup | undefinedis overly broad and unsafe. Consider using a more specific type assertion or checking the actual type at runtime to ensure type safety.