-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Atlas #4 - Adding PlaceLayersSelector component; Updating OverlayLaye…
…rs to allow GeoJSON data or URL
- Loading branch information
dleadbetter
committed
Apr 2, 2024
1 parent
e355286
commit 3fd0ccc
Showing
7 changed files
with
2,376 additions
and
7 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
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
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,96 @@ | ||
// @flow | ||
|
||
import * as Checkbox from '@radix-ui/react-checkbox'; | ||
import { CheckSquare2, Square } from 'lucide-react'; | ||
import React, { useCallback, useState } from 'react'; | ||
import _ from 'underscore'; | ||
import OverlayLayers from './OverlayLayers'; | ||
import type { Layer as LayerType } from '../types/RuntimeConfig'; | ||
|
||
type Props = { | ||
label: string, | ||
layers: Array<LayerType> | ||
}; | ||
|
||
const PlaceLayersSelector = (props: Props) => { | ||
const [selectedLayers, setSelectedLayers] = useState([]); | ||
|
||
/** | ||
* Returns true if the passed ID is in the collection of selected IDs. | ||
* | ||
* @type {function(*): boolean} | ||
*/ | ||
const isSelected = useCallback(({ name }) => _.findWhere(selectedLayers, { name }), [selectedLayers]); | ||
|
||
/** | ||
* Toggles selection for the passed ID. | ||
* | ||
* @type {(function(*): void)|*} | ||
*/ | ||
const onChange = useCallback((layer) => { | ||
if (isSelected(layer)) { | ||
setSelectedLayers((prevSelected) => _.filter(prevSelected, (l) => l.url !== layer.url)); | ||
} else { | ||
setSelectedLayers((prevSelected) => [...prevSelected, layer]); | ||
} | ||
}, [isSelected]); | ||
|
||
if (_.isEmpty(props.layers)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
className='px-3 pb-2 pt-4 text-sm border-t' | ||
> | ||
<OverlayLayers | ||
overlays={selectedLayers} | ||
/> | ||
<h2 | ||
className='font-medium' | ||
> | ||
{ props.label } | ||
</h2> | ||
<ul | ||
className='py-3 leading-relaxed' | ||
> | ||
{ _.map(props.layers, (layer, index) => ( | ||
<li | ||
className='py-1' | ||
key={index} | ||
> | ||
<div | ||
className='flex gap-1.5' | ||
> | ||
<Checkbox.Root | ||
className='hover:bg-transparent mb-0.5' | ||
id={layer.name} | ||
checked={isSelected(layer)} | ||
onCheckedChange={() => onChange(layer)} | ||
> | ||
{ isSelected(layer) && ( | ||
<CheckSquare2 | ||
className='w-5 h-5' | ||
/> | ||
)} | ||
{ !isSelected(layer) && ( | ||
<Square | ||
className='w-5 h-5' | ||
/> | ||
)} | ||
</Checkbox.Root> | ||
<label | ||
className='cursor-pointer grow' | ||
htmlFor={layer.url} | ||
> | ||
{ layer.name } | ||
</label> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PlaceLayersSelector; |
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
61 changes: 61 additions & 0 deletions
61
packages/storybook/src/core-data/PlaceLayersSelector.stories.js
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,61 @@ | ||
// @flow | ||
|
||
import { Map, Zoom } from '@peripleo/maplibre'; | ||
import { Controls, Peripleo } from '@peripleo/peripleo'; | ||
import React from 'react'; | ||
import LocationMarkers from '../../../geospatial/src/components/LocationMarkers'; | ||
import mapStyle from '../data/MapStyles.json'; | ||
import PlaceLayersSelector from '../../../core-data/src/components/PlaceLayersSelector'; | ||
import data from '../data/GeorgiaBorder.json'; | ||
|
||
export default { | ||
title: 'Components/Core Data/PlaceLayersSelector', | ||
component: PlaceLayersSelector | ||
}; | ||
|
||
export const Default = () => ( | ||
<Peripleo> | ||
<Map | ||
style={mapStyle} | ||
> | ||
<Controls | ||
position='topright' | ||
> | ||
<Zoom /> | ||
</Controls> | ||
<div | ||
style={{ | ||
width: '100%', | ||
height: '300px' | ||
}} | ||
> | ||
<LocationMarkers | ||
buffer={100} | ||
data={{ | ||
type: 'Point', | ||
coordinates: [ | ||
-81.2653727, | ||
31.4252249 | ||
] | ||
}} | ||
/> | ||
</div> | ||
</Map> | ||
<PlaceLayersSelector | ||
label='Layers' | ||
layers={[{ | ||
layer_type: 'raster', | ||
name: 'Georgia Area', | ||
url: 'https://mapwarper.net/maps/tile/51704/{z}/{x}/{y}.png' | ||
}, { | ||
layer_type: 'geojson', | ||
name: 'Georgia Border', | ||
data | ||
}, { | ||
layer_type: 'geojson', | ||
name: 'Chatham County', | ||
url: 'https://raw.githubusercontent.com/johan/world.geo.json/master/countries/USA/GA/Chatham.geo.json' | ||
}]} | ||
/> | ||
</Peripleo> | ||
); |
Oops, something went wrong.