esrieact 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ import React from "react";
2
+ import EsriGeoJSONLayer from "@arcgis/core/layers/GeoJSONLayer";
3
+ /**
4
+ * An event handler function map specific to a GeoJSONLayer
5
+ */
6
+ export type GeoJSONLayerEventHandlerFnMap = Partial<{
7
+ refresh: __esri.GeoJSONLayerRefreshEventHandler;
8
+ edits: __esri.GeoJSONLayerEditsEventHandler;
9
+ "layerview-create": __esri.GeoJSONLayerLayerviewCreateEventHandler;
10
+ "layerview-create-error": __esri.GeoJSONLayerLayerviewCreateErrorEventHandler;
11
+ "layerview-destroy": __esri.GeoJSONLayerLayerviewDestroyEventHandler;
12
+ }>;
13
+ /**
14
+ * The GeoJSONLayer React component
15
+ */
16
+ export declare const GeoJSONLayer: React.ForwardRefExoticComponent<__esri.GeoJSONLayerProperties & {
17
+ children?: React.ReactNode;
18
+ } & {
19
+ events?: Partial<{
20
+ refresh: __esri.GeoJSONLayerRefreshEventHandler;
21
+ edits: __esri.GeoJSONLayerEditsEventHandler;
22
+ "layerview-create": __esri.GeoJSONLayerLayerviewCreateEventHandler;
23
+ "layerview-create-error": __esri.GeoJSONLayerLayerviewCreateErrorEventHandler;
24
+ "layerview-destroy": __esri.GeoJSONLayerLayerviewDestroyEventHandler;
25
+ }> | undefined;
26
+ layerOrder?: number | undefined;
27
+ } & React.RefAttributes<EsriGeoJSONLayer>>;
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ import EsriGeoJSONLayer from "@arcgis/core/layers/GeoJSONLayer";
3
+ import { createLayerComponent, } from "./createLayerComponent";
4
+ /**
5
+ * Function that takes in properties of a GeoJSONLayer, and returns the GeoJSONLayer instance
6
+ */
7
+ const createLayer = (properties) => {
8
+ return new EsriGeoJSONLayer(properties);
9
+ };
10
+ /**
11
+ * The GeoJSONLayer React component
12
+ */
13
+ export const GeoJSONLayer = React.forwardRef((properties, ref) => createLayerComponent(createLayer, ref, properties));
@@ -1,5 +1,12 @@
1
1
  import React from "react";
2
2
  import EsriGraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
3
+ export type GraphicsLayerEventHandlerFnMap = Partial<{
4
+ refresh: __esri.FeatureLayerRefreshEventHandler;
5
+ edits: __esri.FeatureLayerEditsEventHandler;
6
+ "layerview-create": __esri.FeatureLayerLayerviewCreateEventHandler;
7
+ "layerview-create-error": __esri.FeatureLayerLayerviewCreateErrorEventHandler;
8
+ "layerview-destroy": __esri.FeatureLayerLayerviewDestroyEventHandler;
9
+ }>;
3
10
  /**
4
11
  * A GraphicsLayer component
5
12
  *
@@ -9,6 +16,12 @@ import EsriGraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
9
16
  export declare const GraphicsLayer: React.ForwardRefExoticComponent<__esri.GraphicsLayerProperties & {
10
17
  children?: React.ReactNode;
11
18
  } & {
12
- events?: {} | undefined;
19
+ events?: Partial<{
20
+ refresh: __esri.FeatureLayerRefreshEventHandler;
21
+ edits: __esri.FeatureLayerEditsEventHandler;
22
+ "layerview-create": __esri.FeatureLayerLayerviewCreateEventHandler;
23
+ "layerview-create-error": __esri.FeatureLayerLayerviewCreateErrorEventHandler;
24
+ "layerview-destroy": __esri.FeatureLayerLayerviewDestroyEventHandler;
25
+ }> | undefined;
13
26
  layerOrder?: number | undefined;
14
27
  } & React.RefAttributes<EsriGraphicsLayer>>;
@@ -0,0 +1,14 @@
1
+ import React from "react";
2
+ import EsriMapImageLayer from "@arcgis/core/layers/MapImageLayer";
3
+ /**
4
+ * A MapImageLayer component
5
+ *
6
+ * ArcGIS JS API Source Components:
7
+ * - [MapImageLayer](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-MapImageLayer.html)
8
+ */
9
+ export declare const MapImageLayer: React.ForwardRefExoticComponent<__esri.MapImageLayerProperties & {
10
+ children?: React.ReactNode;
11
+ } & {
12
+ events?: {} | undefined;
13
+ layerOrder?: number | undefined;
14
+ } & React.RefAttributes<EsriMapImageLayer>>;
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ import EsriMapImageLayer from "@arcgis/core/layers/MapImageLayer";
3
+ import { createLayerComponent, } from "./createLayerComponent";
4
+ const createLayer = (properties) => {
5
+ return new EsriMapImageLayer(properties);
6
+ };
7
+ /**
8
+ * A MapImageLayer component
9
+ *
10
+ * ArcGIS JS API Source Components:
11
+ * - [MapImageLayer](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-MapImageLayer.html)
12
+ */
13
+ export const MapImageLayer = React.forwardRef((properties, ref) => createLayerComponent(createLayer, ref, properties));
@@ -23,7 +23,7 @@ export const createLayerComponent = (createLayer, ref, { children, events, layer
23
23
  */
24
24
  const instance = useMemo(() => {
25
25
  const layer = createLayer(properties);
26
- // If the parent in a GroupLayer (or any EsriInstance that has the .add(layer) method), add it to that,
26
+ // If the parent is a GroupLayer (or any EsriInstance that has the .add(layer) method), add it to that,
27
27
  // otherwise, add directly to the map
28
28
  if (parent && parent.add) {
29
29
  parent.add(layer, layerOrder);
@@ -44,7 +44,12 @@ export const createLayerComponent = (createLayer, ref, { children, events, layer
44
44
  */
45
45
  useEffect(() => {
46
46
  return () => {
47
- map.remove(instance);
47
+ if (parent && parent.remove) {
48
+ parent.remove(instance);
49
+ }
50
+ else {
51
+ map.remove(instance);
52
+ }
48
53
  };
49
54
  }, []);
50
55
  // If no children, there is no need to render a context provider
@@ -9,3 +9,5 @@ export * from "./LayerView";
9
9
  export * from "./WebTileLayer";
10
10
  export * from "./WMSLayer";
11
11
  export * from "./VectorTileLayer";
12
+ export * from "./MapImageLayer";
13
+ export * from "./GeoJSONLayer";
@@ -9,3 +9,5 @@ export * from "./LayerView";
9
9
  export * from "./WebTileLayer";
10
10
  export * from "./WMSLayer";
11
11
  export * from "./VectorTileLayer";
12
+ export * from "./MapImageLayer";
13
+ export * from "./GeoJSONLayer";
@@ -110,8 +110,9 @@ export const MapView = React.forwardRef((props, ref) => {
110
110
  * Utility hook to get the undnerlying `map` and `view` instances of a MapView
111
111
  */
112
112
  export const useMap = () => {
113
- const { map, view } = useContext(MapContext);
114
- if (!map || !view) {
113
+ const context = useContext(MapContext);
114
+ const { map, view } = context;
115
+ if (!context) {
115
116
  throw new Error("Cannot find esrieact map context, are you sure your component is a descendent of a MapContext?");
116
117
  }
117
118
  return { map, view };
@@ -3,7 +3,7 @@ interface Props {
3
3
  /**
4
4
  * The symbol to render in HTML
5
5
  */
6
- symbol: __esri.Symbol;
6
+ symbol: __esri.SymbolUnion;
7
7
  /**
8
8
  * HTML rendering options to be passed to `renderPreviewHTML`
9
9
  */
@@ -1,4 +1,4 @@
1
- import { useUpdateEffect } from "usehooks-ts";
1
+ import { useUpdateEffect } from "./useUpdateEffect";
2
2
  import { usePrevious } from "./usePrevious";
3
3
  import { getObjectDiff } from ".";
4
4
  /**
@@ -1,6 +1,6 @@
1
1
  import { useEffect, useRef } from "react";
2
2
  export function usePrevious(value) {
3
- const ref = useRef();
3
+ const ref = useRef(undefined);
4
4
  useEffect(() => {
5
5
  ref.current = value;
6
6
  }, [value]);
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ /**
3
+ * Like useEffect, but skips the initial mount.
4
+ */
5
+ export declare function useUpdateEffect(effect: React.EffectCallback, deps?: React.DependencyList): void;
@@ -0,0 +1,14 @@
1
+ import { useEffect, useRef } from "react";
2
+ /**
3
+ * Like useEffect, but skips the initial mount.
4
+ */
5
+ export function useUpdateEffect(effect, deps) {
6
+ const isFirst = useRef(true);
7
+ useEffect(() => {
8
+ if (isFirst.current) {
9
+ isFirst.current = false;
10
+ return;
11
+ }
12
+ return effect();
13
+ }, deps);
14
+ }
@@ -11,5 +11,6 @@ const createWidget = (properties) => {
11
11
  * - [Expand](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Expand.html)
12
12
  */
13
13
  export const Expand = React.forwardRef((properties, ref) => {
14
+ // @ts-expect-error internal mismatch of arcgis types?
14
15
  return createWidgetComponent(createWidget, ref, properties);
15
16
  });
@@ -11,5 +11,6 @@ const createWidget = (properties) => {
11
11
  * - [Legend](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Legend.html)
12
12
  */
13
13
  export const Legend = React.forwardRef((properties, ref) => {
14
+ // @ts-expect-error internal mismatch of arcgis types?
14
15
  return createWidgetComponent(createWidget, ref, properties);
15
16
  });
@@ -2,6 +2,7 @@ import React from "react";
2
2
  import EsriSearch from "@arcgis/core/widgets/Search";
3
3
  import { createWidgetComponent } from ".";
4
4
  const createSearchWidget = (properties) => {
5
+ // @ts-expect-error internal mismatch of arcgis types?
5
6
  return new EsriSearch(properties);
6
7
  };
7
8
  /**
@@ -4,7 +4,9 @@ import EsriWidget from "@arcgis/core/widgets/Widget";
4
4
  * Properties that can be applied to any ESRIEACT Widget component. Extends from
5
5
  * esri's WidgetProperties to include child components and the widget position
6
6
  */
7
- export type WidgetComponentProps<T extends __esri.WidgetProperties = __esri.WidgetProperties, E extends Record<string, Function> = {}> = React.PropsWithChildren<T> & {
7
+ export type WidgetComponentProps<T extends __esri.WidgetProperties = __esri.WidgetProperties & {
8
+ view?: __esri.MapView;
9
+ }, E extends Record<string, Function> = {}> = React.PropsWithChildren<T> & {
8
10
  events?: E;
9
11
  position?: string | __esri.UIAddPosition;
10
12
  };
@@ -26,4 +28,4 @@ export declare const WidgetContext: React.Context<EsriWidget>;
26
28
  * @param properties The widget properties
27
29
  * @returns A context provider whose context is the instance to be passed to children, or if there are no children, returns null
28
30
  */
29
- export declare function createWidgetComponent<P extends WidgetComponentProps>(createWidget: CreateWidgetFunction, ref: Ref<EsriWidget>, { children, events, position, ...properties }: P): import("react/jsx-runtime").JSX.Element | null;
31
+ export declare function createWidgetComponent<P extends WidgetComponentProps>(createWidget: CreateWidgetFunction, ref: Ref<EsriWidget>, { children, events, position, view, ...properties }: P): import("react/jsx-runtime").JSX.Element | null;
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createContext, useContext, useEffect, useImperativeHandle, useMemo, } from "react";
2
+ import React, { createContext, useContext, useEffect, useImperativeHandle, useMemo, useRef, } from "react";
3
3
  import Expand from "@arcgis/core/widgets/Expand";
4
4
  import { useMap } from "..";
5
5
  import { useEsriPropertyUpdates, useEvents } from "../utils";
@@ -16,9 +16,10 @@ export const WidgetContext = createContext({});
16
16
  * @param properties The widget properties
17
17
  * @returns A context provider whose context is the instance to be passed to children, or if there are no children, returns null
18
18
  */
19
- export function createWidgetComponent(createWidget, ref, { children, events, position, ...properties }) {
19
+ export function createWidgetComponent(createWidget, ref, { children, events, position, view: viewFromProps, ...properties }) {
20
20
  const { view } = useMap();
21
21
  const parentWidgetContext = useContext(WidgetContext);
22
+ const childrenRef = useRef(null);
22
23
  const instance = useMemo(() => {
23
24
  const instance = createWidget({ ...properties, view });
24
25
  if (parentWidgetContext instanceof Expand) {
@@ -29,10 +30,23 @@ export function createWidgetComponent(createWidget, ref, { children, events, pos
29
30
  view.ui.add(instance, position);
30
31
  }
31
32
  return instance;
32
- }, []);
33
+ }, [view]);
33
34
  useImperativeHandle(ref, () => instance);
34
35
  useEsriPropertyUpdates(instance, properties);
35
36
  useEvents(instance, events);
37
+ /**
38
+ * Check if children is a singular HTML element and assign its ref to instance.content,
39
+ * enables dev to make arbitrary UI elements children of an Expand widget
40
+ */
41
+ useEffect(() => {
42
+ if (children && childrenRef.current && instance) {
43
+ if (React.isValidElement(children)) {
44
+ if (instance instanceof Expand) {
45
+ instance.content = childrenRef.current;
46
+ }
47
+ }
48
+ }
49
+ }, [children, instance]);
36
50
  /**
37
51
  * Remove widget on unmount
38
52
  */
@@ -43,5 +57,8 @@ export function createWidgetComponent(createWidget, ref, { children, events, pos
43
57
  }, []);
44
58
  if (!children)
45
59
  return null;
46
- return (_jsx(WidgetContext.Provider, { value: instance, children: children }));
60
+ // Pass ref to the child element - works for both HTML elements and forwardRef components
61
+ return (_jsx(WidgetContext.Provider, { value: instance, children: React.cloneElement(children, {
62
+ ref: childrenRef,
63
+ }) }));
47
64
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "esrieact",
3
3
  "private": false,
4
- "version": "0.1.1",
4
+ "version": "0.2.1",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
@@ -23,17 +23,16 @@
23
23
  "./widgets/*": "./dist/widgets/*"
24
24
  },
25
25
  "dependencies": {
26
- "lodash.isequal": "^4.5.0",
27
- "usehooks-ts": "^2.9.1"
26
+ "lodash.isequal": "^4.5.0"
28
27
  },
29
28
  "peerDependencies": {
30
- "@arcgis/core": "^4.28.10",
31
- "react": "^18.2.0",
32
- "react-dom": "^18.2.0"
29
+ "@arcgis/core": "^4.30.0",
30
+ "react": "^19.0.0",
31
+ "react-dom": "^19.0.0"
33
32
  },
34
33
  "devDependencies": {
35
- "@types/react": "^18.2.43",
36
- "@types/react-dom": "^18.2.17",
34
+ "@types/react": "^19.0.0",
35
+ "@types/react-dom": "^19.0.0",
37
36
  "typescript": "^5.2.2"
38
37
  }
39
38
  }