All files / src/components AddressMinimap.tsx

97.05% Statements 33/34
95.23% Branches 20/21
100% Functions 10/10
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 20114x   4x                                                                                                                                                                                                                                                                                     6x 6x     6x 5x 5x 3x   2x         6x 5x       6x 5x       6x 5x       6x 6x       6x 6x 3x       6x 5x       6x 5x     6x                    
/* eslint-disable @typescript-eslint/no-namespace */
 
import React, { useRef, useEffect } from 'react';
 
import {
  Anchor,
  MapboxAddressMinimap,
  MapStyleMode,
  Theme
} from '@mapbox/search-js-web';
 
declare global {
  namespace JSX {
    interface IntrinsicElements {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      'mapbox-address-minimap': any;
    }
  }
}
 
/**
 * @typedef AddressMinimapProps
 */
export interface AddressMinimapProps {
  /**
   * If `true`, the marker can be moved around the map. Defaults to `false`.
   *
   * When editable, the marker can be moved around the map and the updated
   * location can be referenced from the {@link AddressMinimapProps#onSaveMarkerLocation} callback.
   */
  canAdjustMarker?: boolean;
  /**
   * If `true`, the map when panned moves around the marker, keeping the marker
   * centered. Defaults to `false`.
   */
  keepMarkerCentered?: boolean;
  /**
   * The anchor of the marker, relative to center of the expanded size. Defaults to `'bottom'`.
   */
  markerAnchor?: Anchor;
  /**
   * A client-defined callback that is triggered when the "Save" button is clicked in the editing interface,
   * and gives access to the adjusted marker coordinate.
   */
  onSaveMarkerLocation?: (coordinate: [number, number]) => void;
 
  /**
   * Must be `true` for the minimap to be shown, in addition to {@link AddressMinimapProps#feature}
   * being present.
   */
  show?: boolean;
  /**
   * The [Mapbox access token](https://docs.mapbox.com/help/glossary/access-token/) to use for all requests.
   *
   * If not explicitly set on the component, this will reference the value in the global config object.
   */
  accessToken?: string;
  /**
   * A [GeoJSON](https://docs.mapbox.com/help/glossary/geojson/) Feature representing
   * a [Point](https://geojson.org/geojson-spec.html#point) geometry.
   *
   * The minimap is hidden unless {@link AddressMinimapProps#feature} is truthy.
   */
  feature?: GeoJSON.Feature<GeoJSON.Point>;
 
  /**
   * If `true`, the map will have an image toggle between Map and Satellite styles.
   */
  satelliteToggle?: boolean;
  /**
   * The {@link Theme} to use for styling interface buttons.
   * @example
   * ```typescript
   * <AddressMinimap theme={{
   *   variables: {
   *     colorPrimary: 'myBrandRed'
   *   }
   * }}>
   * ```
   */
  theme?: Theme;
  /**
   * The map style to use, either `'default'` or `'satellite'`. The default map
   * style is configurable with {@link AddressMinimapProps#defaultMapStyle}.
   */
  mapStyleMode?: MapStyleMode;
  /**
   * The map style to use for the default map style. Defaults to `['mapbox', 'streets-v11']`.
   */
  defaultMapStyle?: [string, string];
  /**
   * Custom footer text appearing below the map, when marker adjustment is enabled.
   * If `true` or left undefined, the default footer text will be used.
   * If `false`, the footer will not be shown.
   */
  footer?: boolean | string;
}
 
/**
 * `AddressMinimap` is a React component that displays a marker for confirmation purposes.
 *
 * Optionally, this marker is editable. When editable, the marker can be moved
 * around the map and the updated location is sent back to the Mapbox Contribute
 * workflow.
 *
 * The goal of `AddressMinimap` is to reduce delivery or geolocation error in shipping and
 * local dispatching contexts.
 *
 * `AddressMinimap` expands to fill its container, and is hidden unless
 * {@link AddressMinimapProps#feature} and {@link AddressMinimapProps#show} are truthy.
 *
 * Internally, this wraps the [`<mapbox-address-minimap>`](https://docs.mapbox.com/mapbox-search-js/api/web/minimap/#mapboxaddressminimap) element.
 *
 * @class AddressMinimap
 * @param {AddressMinimapProps} props
 * @example
 * ```typescript
 * export function Component() {
 *   return (
 *     <AddressMinimap accessToken={<your access token here>}>
 *     </AddressMinimap>
 *   );
 * }
 * ```
 */
export function AddressMinimap(props: AddressMinimapProps): React.ReactElement {
  const {
    canAdjustMarker = false,
    keepMarkerCentered = false,
    markerAnchor = 'bottom',
    onSaveMarkerLocation,
 
    show = false,
    accessToken,
    feature = null,
 
    satelliteToggle = false,
    theme,
    mapStyleMode = 'default',
    defaultMapStyle = ['mapbox', 'streets-v11'],
    footer
  } = props;
  const ref = useRef<MapboxAddressMinimap>();
 
  // Update show.
  useEffect(() => {
    Iif (!ref.current) return;
    if (show) {
      ref.current.show();
    } else {
      ref.current.hide();
    }
  }, [ref.current, show]);
 
  // Update theme.
  useEffect(() => {
    if (ref.current) ref.current.theme = theme;
  }, [ref.current, theme]);
 
  // Update feature.
  useEffect(() => {
    if (ref.current) ref.current.feature = show ? feature : null;
  }, [ref.current, feature, show]);
 
  // Update mapStyleMode.
  useEffect(() => {
    if (ref.current) ref.current.mapStyleMode = mapStyleMode;
  }, [ref.current, mapStyleMode]);
 
  // Update defaultMapStyle.
  useEffect(() => {
    if (ref.current) ref.current.defaultMapStyle = defaultMapStyle;
  }, [ref.current, defaultMapStyle]);
 
  // Update footer.
  useEffect(() => {
    if (footer === undefined) return;
    if (ref.current) ref.current.footer = footer;
  }, [ref.current, footer]);
 
  // Update accessToken.
  useEffect(() => {
    if (ref.current) ref.current.accessToken = accessToken;
  }, [ref.current, accessToken]);
 
  // Update onSaveMarkerLocation callback
  useEffect(() => {
    if (ref.current) ref.current.onSaveMarkerLocation = onSaveMarkerLocation;
  }, [ref.current, onSaveMarkerLocation]);
 
  return (
    <mapbox-address-minimap
      ref={ref}
      can-adjust-marker={canAdjustMarker}
      keep-marker-centered={keepMarkerCentered}
      marker-anchor={markerAnchor}
      satellite-toggle={satelliteToggle}
    />
  );
}