Skip to content
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

Add point selection type to selection package #372

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
18 changes: 13 additions & 5 deletions src/packages/selection/Selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { TbPointerQuestion } from "react-icons/tb";
import { DragController } from "./selection-controller/DragController";
import { Map } from "ol";

type SelectionKind = "extent" | "point";
export type SelectionKind = "extent" | "point";

export interface ISelectionTypeHandler<T> {
new (map: Map, tooltip: string, disabledMessage: string, onExtentSelected: (geometry: Geometry) => void): T;
Expand All @@ -55,6 +55,11 @@ export interface SelectionProps extends CommonComponentProps, MapModelProps {
*/
sources: SelectionSource[];

/**
* Array of selection methods available for spatial selection.
*/
selectionMethods?: SelectionKind | SelectionKind[];

/**
* This handler is called whenever the user has successfully selected
* some items.
Expand Down Expand Up @@ -108,7 +113,7 @@ const COMMON_SELECT_PROPS: SelectProps<any, any, any> = {
*/
export const Selection: FC<SelectionProps> = (props) => {
const intl = useIntl();
const { sources, onSelectionComplete, onSelectionSourceChanged } = props;
const { sources, selectionMethods, onSelectionComplete, onSelectionSourceChanged } = props;
const { containerProps } = useCommonComponentProps("selection", props);
const defaultNotAvailableMessage = intl.formatMessage({ id: "sourceNotAvailable" });

Expand All @@ -118,7 +123,10 @@ export const Selection: FC<SelectionProps> = (props) => {
);
const currentSourceStatus = useSourceStatus(currentSource, defaultNotAvailableMessage);

const [selectionKind, setSelectionKind] = useState<SelectionKind>("extent");
const showSelectionButtons = Boolean(selectionMethods && Array.isArray(selectionMethods) && selectionMethods.length > 1);
HenFo marked this conversation as resolved.
Show resolved Hide resolved
let initialSelectionKind = showSelectionButtons ? (selectionMethods as SelectionKind[])[0] : selectionMethods as SelectionKind;
initialSelectionKind ??= "extent";
const [selectionKind, setSelectionKind] = useState<SelectionKind>(initialSelectionKind);

const mapState = useMapModel(props);
const { onExtentSelected } = useSelectionController(
Expand Down Expand Up @@ -166,7 +174,7 @@ export const Selection: FC<SelectionProps> = (props) => {

return (
<VStack {...containerProps} spacing={2}>
<FormControl>
{showSelectionButtons && <FormControl>
<FormLabel>{intl.formatMessage({ id: "selectionKind" })}</FormLabel>
<HStack gap={2}>
<ToolButton
Expand All @@ -180,7 +188,7 @@ export const Selection: FC<SelectionProps> = (props) => {
onClick={() => setSelectionKind("point")}
isActive={selectionKind === "point"}/>
</HStack>
</FormControl>
</FormControl>}
<FormControl>
<FormLabel>{intl.formatMessage({ id: "selectSource" })}</FormLabel>
<Select<SelectionOption>
Expand Down
1 change: 1 addition & 0 deletions src/samples/map-sample/ol-app/ui/Selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function SelectionComponent() {
>
<Selection
sources={sources}
selectionMethods={["point", "extent"]}
onSelectionComplete={onSelectionComplete}
onSelectionSourceChanged={onSelectionSourceChanged}
/>
Expand Down