• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

terrestris / react-geo / 16338835141

17 Jul 2025 07:22AM UTC coverage: 57.562%. Remained the same
16338835141

push

github

web-flow
Merge pull request #4344 from terrestris/dependabot/npm_and_yarn/typescript-eslint-8.37.0

build(deps-dev): bump typescript-eslint from 8.35.0 to 8.37.0

532 of 1032 branches covered (51.55%)

Branch coverage included in aggregate %.

1036 of 1692 relevant lines covered (61.23%)

10.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Field/SearchField/SearchField.tsx
1
import './SearchField.less';
2

3
import React, { ReactElement, useCallback, useMemo, useState } from 'react';
4

5
import { AutoComplete, Spin } from 'antd';
6

7
import { AutoCompleteProps } from 'antd/lib/auto-complete';
8
import { Feature,FeatureCollection, GeoJsonProperties, Geometry } from 'geojson';
9
import { Extent } from 'ol/extent';
10
import OlFormatGeoJSON from 'ol/format/GeoJSON';
11
import { transformExtent } from 'ol/proj';
12

13
import { SearchFunction, SearchOptions,useSearch } from '@terrestris/react-util';
14
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
15

16

17
import { CSS_PREFIX } from '../../constants';
18

19
export type SearchProps<
20
  G extends Geometry = Geometry,
21
  T extends NonNullable<GeoJsonProperties> = Record<string, any>,
22
  C extends FeatureCollection<G, T> = FeatureCollection<G, T>
23
> = {
24
  searchFunction: SearchFunction<G, T, C>;
25
  searchOptions?: SearchOptions<G, T, C>;
26
  getValue: (feature: Feature<G, T>) => string;
27
  /**
28
   * An onSelect function which gets called with the selected item as it is
29
   * returned by nominatim.
30
   */
31
  onSelect?: (feature: Feature<G, T>) => void;
32
  /**
33
   * An optional CSS class which should be added.
34
   */
35
  className?: string;
36
  /**
37
   * A function that gets called when the clear Button is pressed or the input
38
   * value is empty.
39
   */
40
  onClear?: () => void;
41
  zoomToFeature?: boolean;
42
  getExtent?: (feature: Feature<G, T>) => Extent;
43
} & Omit<AutoCompleteProps, 'onSelect'|'onSearch'|'onChange'|'onClear'|'notFoundContent'>;
44

45
/**
46
 * The NominatimSearch.
47
 */
48
export function SearchField<
49
  G extends Geometry = Geometry,
50
  T extends NonNullable<GeoJsonProperties> = Record<string, any>,
51
  C extends FeatureCollection<G, T> = FeatureCollection<G, T>
52
>({
53
  className = `${CSS_PREFIX}search`,
×
54
  onSelect,
55
  getValue,
56
  searchFunction,
57
  searchOptions = {},
×
58
  zoomToFeature = true,
×
59
  getExtent,
60
  ...passThroughProps
61
}: SearchProps<G, T, C>): ReactElement {
62

63
  const [searchTerm, setSearchTerm] = useState<string>('');
×
64
  const map = useMap();
×
65

66
  const {
67
    featureCollection,
68
    loading
69
  } = useSearch<G, T, C>(searchFunction, searchTerm, searchOptions);
×
70

71
  const options = useMemo(
×
72
    () => featureCollection?.features.map(f => ({
×
73
      label: getValue(f),
74
      value: getValue(f)
75
    })),
76
    [featureCollection, getValue]
77
  );
78

79
  const onMenuItemSelected = useCallback((value: string) => {
×
80
    const selected = featureCollection?.features.find(f => getValue(f) === value);
×
81
    if (selected && onSelect) {
×
82
      onSelect(selected);
×
83
    }
84
    if (selected && zoomToFeature) {
×
85
      if (!map) {
×
86
        return;
×
87
      }
88
      let extent: Extent;
89
      if (getExtent) {
×
90
        extent = getExtent(selected);
×
91
      } else {
92
        const olFormat = new OlFormatGeoJSON();
×
93
        const geometry = olFormat.readGeometry(selected.geometry);
×
94
        extent = geometry.getExtent();
×
95
      }
96

97
      const olView = map?.getView();
×
98

99
      extent = transformExtent(extent, 'EPSG:4326', olView.getProjection());
×
100

101
      olView.fit(extent, {
×
102
        duration: 500
103
      });
104
    }
105
  }, [map, onSelect, getValue, getExtent, featureCollection?.features, zoomToFeature]);
106

107
  return (
×
108
    <AutoComplete
109
      className={className}
110
      allowClear={true}
111
      onSearch={text =>
112
        setSearchTerm(text)
×
113
      }
114
      onClear={() =>
115
        setSearchTerm('')
×
116
      }
117
      onSelect={onMenuItemSelected}
118
      options={options}
119
      notFoundContent={loading ? <Spin size="small" /> : null}
×
120
      {...passThroughProps}
121
    />
122
  );
123
}
124

125
export default SearchField;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc