@open-pioneer/search 1.4.0 → 1.5.0-dev.20260910103330
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.
- package/CHANGELOG.md +38 -27
- package/package.json +13 -9
- package/ui/Search.js.map +1 -1
- package/ui/useSearchState.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @open-pioneer/search
|
|
2
2
|
|
|
3
|
+
## 1.5.0-dev.20260910103330
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f790fda: Update to Chakra 3.37.0
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 3203815: Update code to match new react linting rules from oxlint.
|
|
12
|
+
- 09ec7c7: Updated dependencies
|
|
13
|
+
|
|
3
14
|
## 1.4.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
|
@@ -10,8 +21,8 @@
|
|
|
10
21
|
The method returns the selected `SearchResult` (and its `SearchSource`) or `undefined` when no match is found.
|
|
11
22
|
- 0b9143d: The `onSelect` callback now receives an additional property `trigger` (`"user"` or `"api-select"`) support different reactions based on the way a result was selected:
|
|
12
23
|
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
- `user` indicates that event was triggered by the end user
|
|
25
|
+
- `api-select` indicates that the selection was made via the API (i.e. `searchAndSelect`).
|
|
15
26
|
|
|
16
27
|
### Patch Changes
|
|
17
28
|
|
|
@@ -24,8 +35,8 @@
|
|
|
24
35
|
- 9b5d5f3: Support for new common container props (role, aria-\*, data-\* and css)
|
|
25
36
|
- d05a3b8: Refactor user interface: now based on chakra's combobox instead of react-select.
|
|
26
37
|
|
|
27
|
-
|
|
28
|
-
|
|
38
|
+
NOTE: This causes changes to many internal CSS classes and attributes.
|
|
39
|
+
The UX of the control should be the same.
|
|
29
40
|
|
|
30
41
|
- d54ccfd: Update to Chakra UI 3.35.0
|
|
31
42
|
- 206b397: Update to trails core packages 4.6.0
|
|
@@ -62,8 +73,8 @@
|
|
|
62
73
|
|
|
63
74
|
- 0485014: Introduce search API and `resetInput` method (see [PR](https://github.com/open-pioneer/trails-openlayers-base-packages/pull/493)).
|
|
64
75
|
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
The search API allows programmatic access to the search component.
|
|
77
|
+
Currently, it provides a method to reset the search input field.
|
|
67
78
|
|
|
68
79
|
- 2732052: Icons have been changed to unify the appearance of the components. Preferably, Lucide react-icons are used.
|
|
69
80
|
- 2abcaaf: Update to chakra-ui 3.28.0
|
|
@@ -86,16 +97,16 @@
|
|
|
86
97
|
|
|
87
98
|
- 9376a74: Ensure that icons and other decorative elements are hidden from the screen reader using the `aria-hidden="true"` attribute.
|
|
88
99
|
|
|
89
|
-
|
|
100
|
+
The easiest way to do that is to wrap icons into chakra's `<Icon />` component, for example:
|
|
90
101
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
```tsx
|
|
103
|
+
import { Icon } from "@chakra-ui/react";
|
|
104
|
+
import { FiX } from "react-icons/fi";
|
|
94
105
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
106
|
+
<Icon>
|
|
107
|
+
<FiX />
|
|
108
|
+
</Icon>;
|
|
109
|
+
```
|
|
99
110
|
|
|
100
111
|
## 0.10.0
|
|
101
112
|
|
|
@@ -104,17 +115,17 @@
|
|
|
104
115
|
- 193068a: Deprecate the `mapId` property on React components.
|
|
105
116
|
Use the `MapModel` directly instead to pass a reference to the map.
|
|
106
117
|
|
|
107
|
-
|
|
118
|
+
Example:
|
|
108
119
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
```tsx
|
|
121
|
+
// Default map for entire component tree
|
|
122
|
+
<DefaultMapProvider map={mapModel}>
|
|
123
|
+
<Toc />
|
|
124
|
+
</DefaultMapProvider>
|
|
114
125
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
// Map for specific component
|
|
127
|
+
<Toc map={mapModel} />
|
|
128
|
+
```
|
|
118
129
|
|
|
119
130
|
### Patch Changes
|
|
120
131
|
|
|
@@ -138,9 +149,9 @@
|
|
|
138
149
|
### Minor Changes
|
|
139
150
|
|
|
140
151
|
- 2fa8020: Update trails core package dependencies.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
152
|
+
- Also updates Chakra UI to the latest 2.x version and Chakra React Select to version 5.
|
|
153
|
+
- Removes any obsolete references to `@chakra-ui/system`.
|
|
154
|
+
This dependency seems to be no longer required and may lead to duplicate packages in your dependency tree.
|
|
144
155
|
|
|
145
156
|
### Patch Changes
|
|
146
157
|
|
|
@@ -159,7 +170,7 @@
|
|
|
159
170
|
- 583f1d6: The `mapId` or `map` properties are now optional on individual components.
|
|
160
171
|
You can use the `DefaultMapProvider` to configure an implicit default value.
|
|
161
172
|
|
|
162
|
-
|
|
173
|
+
Note that configuring _neither_ a default _nor_ an explicit `map` or `mapId` will trigger a runtime error.
|
|
163
174
|
|
|
164
175
|
- 583f1d6: All UI components in this project now accept the `mapId` (a `string`) _or_ the `map` (a `MapModel`) directly.
|
|
165
176
|
- a8b3449: Switch to a new versioning strategy.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/search",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0-dev.20260910103330",
|
|
5
5
|
"description": "This package provides a UI component to perform a search on given search sources.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -14,15 +14,19 @@
|
|
|
14
14
|
"directory": "src/packages/search"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@chakra-ui/react": "^3.
|
|
18
|
-
"@open-pioneer/chakra-snippets": "
|
|
19
|
-
"@open-pioneer/core": "
|
|
20
|
-
"@open-pioneer/
|
|
21
|
-
"@open-pioneer/
|
|
22
|
-
"
|
|
17
|
+
"@chakra-ui/react": "^3.37.0",
|
|
18
|
+
"@open-pioneer/chakra-snippets": "4.8.0-dev.20260910101405",
|
|
19
|
+
"@open-pioneer/core": "4.8.0-dev.20260910101405",
|
|
20
|
+
"@open-pioneer/map": "1.5.0-dev.20260910103330",
|
|
21
|
+
"@open-pioneer/react-utils": "4.8.0-dev.20260910101405",
|
|
22
|
+
"@open-pioneer/runtime": "4.8.0-dev.20260910101405",
|
|
23
|
+
"ol": "^10.10.0",
|
|
23
24
|
"react": "^19.2.8",
|
|
24
|
-
"react-icons": "^5.7.0"
|
|
25
|
-
|
|
25
|
+
"react-icons": "^5.7.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"directory": "dist",
|
|
29
|
+
"linkDirectory": false
|
|
26
30
|
},
|
|
27
31
|
"exports": {
|
|
28
32
|
"./package.json": "./package.json",
|
package/ui/Search.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Search.js","sources":["Search.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Box } from \"@chakra-ui/react\";\nimport { MapModelProps, useMapModelValue } from \"@open-pioneer/map\";\nimport { CommonComponentProps, useCommonComponentProps, useEvent } from \"@open-pioneer/react-utils\";\nimport { FC, useEffect, useRef } from \"react\";\nimport {\n SearchApi,\n SearchClearEvent,\n SearchDisposedEvent,\n SearchReadyEvent,\n SearchSelectEvent,\n SearchSource,\n SelectResult\n} from \"../api\";\nimport { SearchApiImpl } from \"./SearchApiImpl\";\nimport { SearchInput } from \"./SearchInput\";\nimport { useSearchState } from \"./useSearchState\";\n\n/**\n * Properties supported by the {@link Search} component.\n */\nexport interface SearchProps extends CommonComponentProps, MapModelProps {\n /**\n * Data sources to be searched on.\n */\n sources: SearchSource[];\n\n /**\n * Typing delay (in milliseconds) before the async search query starts after the user types in the search term.\n * Defaults to `200`.\n */\n searchTypingDelay?: number;\n\n /**\n * The maximum number of results shown per group.\n * Defaults to `5`.\n */\n maxResultsPerGroup?: number;\n\n /**\n * The placeholder text shown in the search input field when it is empty.\n * Defaults to a generic (and localized) hint.\n */\n placeholder?: string;\n\n /**\n * This event handler will be called when a search result has been selected.\n */\n onSelect?: (event: SearchSelectEvent) => void;\n\n /**\n * This event handler will be called when the search input has been cleared.\n */\n onClear?: (event: SearchClearEvent) => void;\n\n /**\n * Callback that is triggered once when the search is initialized.\n * The search API can be accessed by the `api` property of the {@link SearchReadyEvent}.\n */\n onReady?: (event: SearchReadyEvent) => void;\n\n /**\n * Callback that is triggered once when the search is disposed and unmounted.\n */\n onDisposed?: (event: SearchDisposedEvent) => void;\n}\n\n/**\n * A component that allows the user to search a given set of {@link SearchSource | SearchSources}.\n */\nexport const Search: FC<SearchProps> = (props) => {\n const {\n sources,\n searchTypingDelay,\n maxResultsPerGroup,\n placeholder,\n onSelect,\n onClear,\n onReady,\n onDisposed\n } = props;\n const { containerProps } = useCommonComponentProps(\"search\", props);\n const map = useMapModelValue(props);\n const { input, results, onInputChanged, onOptionConfirmed, selectedOption, searchAndSelect } =\n useSearchState(sources, searchTypingDelay, maxResultsPerGroup, map, onSelect);\n\n // api trigger hooks\n useSearchApi(onReady, onDisposed, onInputChanged, onClear, searchAndSelect);\n\n return (\n <Box {...containerProps} width={\"100%\"}>\n <SearchInput\n input={input}\n selectedOption={selectedOption}\n results={results}\n placeholder={placeholder}\n onClear={onClear}\n onSelect={onSelect}\n onInputChanged={onInputChanged}\n onOptionConfirmed={onOptionConfirmed}\n />\n </Box>\n );\n};\n\n// Note: `clearInput` and `setInputValue` must be stable because only the initial value is used to construct the API instance at this time.\n// oxlint-disable-next-line max-params\nfunction useSearchApi(\n onReady: ((event: SearchReadyEvent) => void) | undefined,\n onDisposed: ((event: SearchDisposedEvent) => void) | undefined,\n onInputChanged: (newValue: string) => void,\n onClear: ((event: SearchClearEvent) => void) | undefined,\n searchAndSelect: (inputValue: string) => Promise<SelectResult | undefined>\n) {\n const clearInput = useEvent(() => {\n onInputChanged(\"\");\n onClear?.({ trigger: \"api-reset\" });\n });\n\n const apiRef = useRef<SearchApi>(null);\n if (!apiRef.current) {\n // NOTE: The API object is only created once.\n // This is why the functions must currently be stable!\n apiRef.current = new SearchApiImpl(clearInput, onInputChanged, searchAndSelect);\n }\n\n const api = apiRef.current;\n\n const readyTrigger = useEvent(() => {\n onReady?.({\n api\n });\n });\n\n const disposeTrigger = useEvent(() => {\n onDisposed?.({});\n });\n\n // Trigger ready / dispose on mount / unmount, but if the callbacks change.\n // useEvent() returns a stable function reference.\n useEffect(() => {\n readyTrigger();\n return disposeTrigger;\n }, [readyTrigger, disposeTrigger]);\n}\n"],"names":[],"mappings":";;;;;;;;;AAwEO,MAAM,MAAA,GAA0B,CAAC,KAAA,KAAU;AAC9C,EAAA,MAAM;AAAA,IACF,OAAA;AAAA,IACA,iBAAA;AAAA,IACA,kBAAA;AAAA,IACA,WAAA;AAAA,IACA,QAAA;AAAA,IACA,OAAA;AAAA,IACA,OAAA;AAAA,IACA;AAAA,GACJ,GAAI,KAAA;AACJ,EAAA,MAAM,EAAE,cAAA,EAAe,GAAI,uBAAA,CAAwB,UAAU,KAAK,CAAA;AAClE,EAAA,MAAM,GAAA,GAAM,iBAAiB,KAAK,CAAA;AAClC,EAAA,MAAM,EAAE,KAAA,EAAO,OAAA,EAAS,cAAA,EAAgB,iBAAA,EAAmB,cAAA,EAAgB,eAAA,EAAgB,GACvF,cAAA,CAAe,OAAA,EAAS,iBAAA,EAAmB,kBAAA,EAAoB,KAAK,QAAQ,CAAA;AAGhF,EAAA,YAAA,CAAa,OAAA,EAAS,UAAA,EAAY,cAAA,EAAgB,OAAA,EAAS,eAAe,CAAA;AAE1E,EAAA,uBACI,GAAA,CAAC,GAAA,EAAA,EAAK,GAAG,cAAA,EAAgB,OAAO,MAAA,EAC5B,QAAA,kBAAA,GAAA;AAAA,IAAC,WAAA;AAAA,IAAA;AAAA,MACG,KAAA;AAAA,MACA,cAAA;AAAA,MACA,OAAA;AAAA,MACA,WAAA;AAAA,MACA,OAAA;AAAA,MACA,QAAA;AAAA,MACA,cAAA;AAAA,MACA;AAAA;AAAA,GACJ,EACJ,CAAA;AAER;AAIA,SAAS,YAAA,CACL,OAAA,EACA,UAAA,EACA,cAAA,EACA,SACA,eAAA,EACF;AACE,EAAA,MAAM,UAAA,GAAa,SAAS,MAAM;AAC9B,IAAA,cAAA,CAAe,EAAE,CAAA;AACjB,IAAA,OAAA,GAAU,EAAE,OAAA,EAAS,WAAA,EAAa,CAAA;AAAA,EACtC,CAAC,CAAA;AAED,EAAA,MAAM,MAAA,GAAS,OAAkB,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"Search.js","sources":["Search.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Box } from \"@chakra-ui/react\";\nimport { MapModelProps, useMapModelValue } from \"@open-pioneer/map\";\nimport { CommonComponentProps, useCommonComponentProps, useEvent } from \"@open-pioneer/react-utils\";\nimport { FC, useEffect, useRef } from \"react\";\nimport {\n SearchApi,\n SearchClearEvent,\n SearchDisposedEvent,\n SearchReadyEvent,\n SearchSelectEvent,\n SearchSource,\n SelectResult\n} from \"../api\";\nimport { SearchApiImpl } from \"./SearchApiImpl\";\nimport { SearchInput } from \"./SearchInput\";\nimport { useSearchState } from \"./useSearchState\";\n\n/**\n * Properties supported by the {@link Search} component.\n */\nexport interface SearchProps extends CommonComponentProps, MapModelProps {\n /**\n * Data sources to be searched on.\n */\n sources: SearchSource[];\n\n /**\n * Typing delay (in milliseconds) before the async search query starts after the user types in the search term.\n * Defaults to `200`.\n */\n searchTypingDelay?: number;\n\n /**\n * The maximum number of results shown per group.\n * Defaults to `5`.\n */\n maxResultsPerGroup?: number;\n\n /**\n * The placeholder text shown in the search input field when it is empty.\n * Defaults to a generic (and localized) hint.\n */\n placeholder?: string;\n\n /**\n * This event handler will be called when a search result has been selected.\n */\n onSelect?: (event: SearchSelectEvent) => void;\n\n /**\n * This event handler will be called when the search input has been cleared.\n */\n onClear?: (event: SearchClearEvent) => void;\n\n /**\n * Callback that is triggered once when the search is initialized.\n * The search API can be accessed by the `api` property of the {@link SearchReadyEvent}.\n */\n onReady?: (event: SearchReadyEvent) => void;\n\n /**\n * Callback that is triggered once when the search is disposed and unmounted.\n */\n onDisposed?: (event: SearchDisposedEvent) => void;\n}\n\n/**\n * A component that allows the user to search a given set of {@link SearchSource | SearchSources}.\n */\nexport const Search: FC<SearchProps> = (props) => {\n const {\n sources,\n searchTypingDelay,\n maxResultsPerGroup,\n placeholder,\n onSelect,\n onClear,\n onReady,\n onDisposed\n } = props;\n const { containerProps } = useCommonComponentProps(\"search\", props);\n const map = useMapModelValue(props);\n const { input, results, onInputChanged, onOptionConfirmed, selectedOption, searchAndSelect } =\n useSearchState(sources, searchTypingDelay, maxResultsPerGroup, map, onSelect);\n\n // api trigger hooks\n useSearchApi(onReady, onDisposed, onInputChanged, onClear, searchAndSelect);\n\n return (\n <Box {...containerProps} width={\"100%\"}>\n <SearchInput\n input={input}\n selectedOption={selectedOption}\n results={results}\n placeholder={placeholder}\n onClear={onClear}\n onSelect={onSelect}\n onInputChanged={onInputChanged}\n onOptionConfirmed={onOptionConfirmed}\n />\n </Box>\n );\n};\n\n// Note: `clearInput` and `setInputValue` must be stable because only the initial value is used to construct the API instance at this time.\n// oxlint-disable-next-line max-params\nfunction useSearchApi(\n onReady: ((event: SearchReadyEvent) => void) | undefined,\n onDisposed: ((event: SearchDisposedEvent) => void) | undefined,\n onInputChanged: (newValue: string) => void,\n onClear: ((event: SearchClearEvent) => void) | undefined,\n searchAndSelect: (inputValue: string) => Promise<SelectResult | undefined>\n) {\n const clearInput = useEvent(() => {\n onInputChanged(\"\");\n onClear?.({ trigger: \"api-reset\" });\n });\n\n const apiRef = useRef<SearchApi>(null);\n // oxlint-disable-next-line react/refs\n if (!apiRef.current) {\n // NOTE: The API object is only created once.\n // This is why the functions must currently be stable!\n apiRef.current = new SearchApiImpl(clearInput, onInputChanged, searchAndSelect);\n }\n\n const api = apiRef.current;\n\n const readyTrigger = useEvent(() => {\n onReady?.({\n api\n });\n });\n\n const disposeTrigger = useEvent(() => {\n onDisposed?.({});\n });\n\n // Trigger ready / dispose on mount / unmount, but if the callbacks change.\n // useEvent() returns a stable function reference.\n useEffect(() => {\n readyTrigger();\n return disposeTrigger;\n }, [readyTrigger, disposeTrigger]);\n}\n"],"names":[],"mappings":";;;;;;;;;AAwEO,MAAM,MAAA,GAA0B,CAAC,KAAA,KAAU;AAC9C,EAAA,MAAM;AAAA,IACF,OAAA;AAAA,IACA,iBAAA;AAAA,IACA,kBAAA;AAAA,IACA,WAAA;AAAA,IACA,QAAA;AAAA,IACA,OAAA;AAAA,IACA,OAAA;AAAA,IACA;AAAA,GACJ,GAAI,KAAA;AACJ,EAAA,MAAM,EAAE,cAAA,EAAe,GAAI,uBAAA,CAAwB,UAAU,KAAK,CAAA;AAClE,EAAA,MAAM,GAAA,GAAM,iBAAiB,KAAK,CAAA;AAClC,EAAA,MAAM,EAAE,KAAA,EAAO,OAAA,EAAS,cAAA,EAAgB,iBAAA,EAAmB,cAAA,EAAgB,eAAA,EAAgB,GACvF,cAAA,CAAe,OAAA,EAAS,iBAAA,EAAmB,kBAAA,EAAoB,KAAK,QAAQ,CAAA;AAGhF,EAAA,YAAA,CAAa,OAAA,EAAS,UAAA,EAAY,cAAA,EAAgB,OAAA,EAAS,eAAe,CAAA;AAE1E,EAAA,uBACI,GAAA,CAAC,GAAA,EAAA,EAAK,GAAG,cAAA,EAAgB,OAAO,MAAA,EAC5B,QAAA,kBAAA,GAAA;AAAA,IAAC,WAAA;AAAA,IAAA;AAAA,MACG,KAAA;AAAA,MACA,cAAA;AAAA,MACA,OAAA;AAAA,MACA,WAAA;AAAA,MACA,OAAA;AAAA,MACA,QAAA;AAAA,MACA,cAAA;AAAA,MACA;AAAA;AAAA,GACJ,EACJ,CAAA;AAER;AAIA,SAAS,YAAA,CACL,OAAA,EACA,UAAA,EACA,cAAA,EACA,SACA,eAAA,EACF;AACE,EAAA,MAAM,UAAA,GAAa,SAAS,MAAM;AAC9B,IAAA,cAAA,CAAe,EAAE,CAAA;AACjB,IAAA,OAAA,GAAU,EAAE,OAAA,EAAS,WAAA,EAAa,CAAA;AAAA,EACtC,CAAC,CAAA;AAED,EAAA,MAAM,MAAA,GAAS,OAAkB,IAAI,CAAA;AAErC,EAAA,IAAI,CAAC,OAAO,OAAA,EAAS;AAGjB,IAAA,MAAA,CAAO,OAAA,GAAU,IAAI,aAAA,CAAc,UAAA,EAAY,gBAAgB,eAAe,CAAA;AAAA,EAClF;AAEA,EAAA,MAAM,MAAM,MAAA,CAAO,OAAA;AAEnB,EAAA,MAAM,YAAA,GAAe,SAAS,MAAM;AAChC,IAAA,OAAA,GAAU;AAAA,MACN;AAAA,KACH,CAAA;AAAA,EACL,CAAC,CAAA;AAED,EAAA,MAAM,cAAA,GAAiB,SAAS,MAAM;AAClC,IAAA,UAAA,GAAa,EAAE,CAAA;AAAA,EACnB,CAAC,CAAA;AAID,EAAA,SAAA,CAAU,MAAM;AACZ,IAAA,YAAA,EAAa;AACb,IAAA,OAAO,cAAA;AAAA,EACX,CAAA,EAAG,CAAC,YAAA,EAAc,cAAc,CAAC,CAAA;AACrC;;;;"}
|
package/ui/useSearchState.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSearchState.js","sources":["useSearchState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createLogger, isAbortError } from \"@open-pioneer/core\";\nimport { MapModel } from \"@open-pioneer/map\";\nimport { useEvent } from \"@open-pioneer/react-utils\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { useCallback, useEffect, useReducer, useRef, useState } from \"react\";\nimport { SearchResult, SearchSource, SelectResult } from \"../api\";\nimport { SearchController, ResultGroup } from \"../model/SearchController\";\nimport { SearchProps } from \"./Search\";\n\nconst LOG = createLogger(sourceId);\n\nexport interface SearchOptionGroup {\n /** Unique id of this group. */\n id: string;\n\n /** Display text shown in menu. */\n label: string;\n\n /** Set of options that belong to this group. */\n options: SearchOption[];\n}\n\nexport interface SearchOption {\n /** Unique value for this option. */\n value: string;\n\n /** Display text shown in menu. */\n label: string;\n\n /** Search source that returned the suggestion. */\n source: SearchSource;\n\n /** Links to the parent group. */\n group: SearchOptionGroup;\n\n /** The raw result from the search source. */\n result: SearchResult;\n}\n\nexport interface SearchState {\n input: string;\n results: SearchResultsState;\n selectedOption: SearchOption | undefined;\n\n // NOTE: Stable functions\n onOptionConfirmed: (newOption: SearchOption) => void;\n onInputChanged: (newInput: string) => void;\n\n searchAndSelect: (query: string) => Promise<SelectResult | undefined>;\n}\n\nexport type SearchResultsReady = {\n kind: \"ready\";\n results: SearchOptionGroup[];\n};\n\nexport type SearchResultsLoading = {\n kind: \"loading\";\n};\n\nexport type SearchResultsState = SearchResultsReady | SearchResultsLoading;\n\n/**\n * Keeps track of the current input text, active searches and their results.\n *\n * NOTE: it would be great to merge this state handling with the search controller\n * in a future revision.\n */\n// oxlint-disable-next-line max-params\nexport function useSearchState(\n sources: SearchSource[],\n searchTypingDelay: number | undefined,\n maxResultsPerGroup: number | undefined,\n map: MapModel,\n onSelect: SearchProps[\"onSelect\"] | undefined // TODO: refactor --> model\n): SearchState {\n interface FullSearchState {\n query: string;\n selectedOption: SearchOption | undefined;\n search: SearchResultsState;\n }\n\n const getId = useSearchSourceId();\n const controller = useController(sources, searchTypingDelay, maxResultsPerGroup, map);\n\n type Action =\n | { kind: \"input\"; query: string }\n | { kind: \"select-option\"; option: SearchOption }\n | { kind: \"load-results\" }\n | { kind: \"accept-results\"; results: SearchOptionGroup[] };\n\n const [state, dispatch] = useReducer(\n (current: FullSearchState, action: Action): FullSearchState => {\n switch (action.kind) {\n case \"input\":\n return {\n ...current,\n query: action.query,\n selectedOption: undefined\n };\n case \"select-option\":\n return {\n ...current,\n selectedOption: action.option,\n query: action.option.label\n };\n case \"load-results\":\n return {\n ...current,\n search: {\n kind: \"loading\"\n }\n };\n case \"accept-results\":\n return {\n ...current,\n search: {\n kind: \"ready\",\n results: action.results\n }\n };\n }\n },\n undefined,\n (): FullSearchState => ({\n query: \"\",\n selectedOption: undefined,\n search: {\n kind: \"ready\",\n results: []\n }\n })\n );\n\n // Stores the promise for the current search.\n // Any results from outdated searches are ignored.\n const currentSearch = useRef<Promise<unknown>>(undefined);\n const startSearch = useEvent((query: string) => {\n if (!controller) {\n currentSearch.current = undefined;\n dispatch({ kind: \"accept-results\", results: [] });\n return;\n }\n\n LOG.isDebug() && LOG.debug(`Starting new search for query ${JSON.stringify(query)}.`);\n dispatch({ kind: \"load-results\" });\n const promise = (currentSearch.current = search(controller, query, getId).then(\n (results) => {\n // Check if this job is still current\n if (currentSearch.current === promise) {\n dispatch({ kind: \"accept-results\", results });\n }\n }\n ));\n });\n\n /**\n * Searches for the specified query and automatically selects the first matching result.\n *\n * If the search controller is unavailable or the search returns no results,\n * the promise resolves to `undefined`.\n *\n * When a match is found, this function:\n * - Executes the search using the provided query.\n * - Updates the UI with the returned search results.\n * - Selects the first available search option and notifies the component's callback function (if any).\n *\n * @param query - The search query to execute.\n * @returns A promise that resolves to the selected {@link SearchResult} and its source,\n * or `undefined` if no result is found or the search cannot be performed.\n */\n const searchAndSelect = useEvent(async (query: string): Promise<SelectResult | undefined> => {\n if (!controller) {\n return undefined;\n }\n\n const groups = await search(controller, query, getId);\n const firstOption = groups.flatMap((g) => g.options)[0];\n if (!firstOption) {\n return undefined;\n }\n\n const { result, source } = firstOption;\n\n // update UI\n dispatch({\n kind: \"accept-results\",\n results: groups\n });\n dispatch({\n kind: \"select-option\",\n option: firstOption\n });\n\n // notify component callback\n onSelect?.({ result, source, trigger: \"api-select\" });\n return { result, source };\n });\n\n // Called when the user confirms a search result\n const onOptionConfirmed = useEvent((option: SearchOption) => {\n // Do not start a new search when the user confirms a result\n dispatch({ kind: \"select-option\", option });\n });\n\n // Called when a user types into the input field\n const onInputChanged = useEvent((newValue: string) => {\n // Trigger a new search if the user changes the query by typing\n dispatch({ kind: \"input\", query: newValue });\n startSearch(newValue);\n });\n\n return {\n input: state.query,\n results: state.search,\n selectedOption: state.selectedOption,\n onOptionConfirmed,\n onInputChanged,\n searchAndSelect\n };\n}\n\nasync function search(\n controller: SearchController,\n query: string,\n getId: GetSearchSourceId\n): Promise<SearchOptionGroup[]> {\n let suggestions: ResultGroup[];\n try {\n suggestions = await controller.search(query);\n } catch (error) {\n if (!isAbortError(error)) {\n LOG.error(`Search failed`, error);\n }\n suggestions = [];\n }\n return mapSuggestions(suggestions, getId);\n}\n\n/**\n * Creates a controller to search on the given sources.\n */\nfunction useController(\n sources: SearchSource[],\n searchTypingDelay: number | undefined,\n maxResultsPerGroup: number | undefined,\n map: MapModel\n) {\n const [controller, setController] = useState<SearchController | undefined>(undefined);\n useEffect(() => {\n const controller = new SearchController(map, sources);\n setController(controller);\n return () => {\n controller.destroy();\n setController(undefined);\n };\n }, [map, sources]);\n\n useEffect(() => {\n if (controller) {\n controller.searchTypingDelay = searchTypingDelay;\n }\n }, [controller, searchTypingDelay]);\n useEffect(() => {\n if (controller) {\n controller.maxResultsPerSource = maxResultsPerGroup;\n }\n }, [controller, maxResultsPerGroup]);\n return controller;\n}\n\n// Maps results from search source to objects that the UI can work with.\nfunction mapSuggestions(\n resultGroups: ResultGroup[],\n getId: GetSearchSourceId\n): SearchOptionGroup[] {\n return resultGroups.map((resultGroup): SearchOptionGroup => {\n const groupId = getId(resultGroup.source);\n const optionGroup: SearchOptionGroup = {\n id: groupId,\n label: resultGroup.label,\n options: []\n };\n\n for (const result of resultGroup.results) {\n optionGroup.options.push({\n // unique (in this component)\n value: `${groupId}-${result.id}`,\n label: result.label,\n group: optionGroup,\n source: resultGroup.source,\n result: result\n });\n }\n return optionGroup;\n });\n}\n\ntype GetSearchSourceId = (source: SearchSource) => string;\n\nfunction useSearchSourceId(): GetSearchSourceId {\n const sourceIds = useRef<WeakMap<SearchSource, string>>(undefined);\n const counter = useRef(0);\n if (!sourceIds.current) {\n sourceIds.current = new WeakMap();\n }\n\n return useCallback((searchSource: SearchSource) => {\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const ids = sourceIds.current!;\n if (!ids.has(searchSource)) {\n ids.set(searchSource, `source-${counter.current++}`);\n }\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n return ids.get(searchSource)!;\n }, []);\n}\n"],"names":["controller"],"mappings":";;;;;;AAYA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AA4D1B,SAAS,cAAA,CACZ,OAAA,EACA,iBAAA,EACA,kBAAA,EACA,KACA,QAAA,EACW;AAOX,EAAA,MAAM,QAAQ,iBAAA,EAAkB;AAChC,EAAA,MAAM,UAAA,GAAa,aAAA,CAAc,OAAA,EAAS,iBAAA,EAAmB,oBAAoB,GAAG,CAAA;AAQpF,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,UAAA;AAAA,IACtB,CAAC,SAA0B,MAAA,KAAoC;AAC3D,MAAA,QAAQ,OAAO,IAAA;AAAM,QACjB,KAAK,OAAA;AACD,UAAA,OAAO;AAAA,YACH,GAAG,OAAA;AAAA,YACH,OAAO,MAAA,CAAO,KAAA;AAAA,YACd,cAAA,EAAgB;AAAA,WACpB;AAAA,QACJ,KAAK,eAAA;AACD,UAAA,OAAO;AAAA,YACH,GAAG,OAAA;AAAA,YACH,gBAAgB,MAAA,CAAO,MAAA;AAAA,YACvB,KAAA,EAAO,OAAO,MAAA,CAAO;AAAA,WACzB;AAAA,QACJ,KAAK,cAAA;AACD,UAAA,OAAO;AAAA,YACH,GAAG,OAAA;AAAA,YACH,MAAA,EAAQ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EAAc,CAAA,OAAA,EAAU,OAAA,CAAQ,SAAS,CAAA,CAAE,CAAA;AAAA,IACvD;AAEA,IAAA,OAAO,GAAA,CAAI,IAAI,YAAY,CAAA;AAAA,EAC/B,CAAA,EAAG,EAAE,CAAA;AACT;;;;"}
|
|
1
|
+
{"version":3,"file":"useSearchState.js","sources":["useSearchState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createLogger, isAbortError } from \"@open-pioneer/core\";\nimport { MapModel } from \"@open-pioneer/map\";\nimport { useEvent } from \"@open-pioneer/react-utils\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { useCallback, useEffect, useReducer, useRef, useState } from \"react\";\nimport { SearchResult, SearchSource, SelectResult } from \"../api\";\nimport { SearchController, ResultGroup } from \"../model/SearchController\";\nimport { SearchProps } from \"./Search\";\n\nconst LOG = createLogger(sourceId);\n\nexport interface SearchOptionGroup {\n /** Unique id of this group. */\n id: string;\n\n /** Display text shown in menu. */\n label: string;\n\n /** Set of options that belong to this group. */\n options: SearchOption[];\n}\n\nexport interface SearchOption {\n /** Unique value for this option. */\n value: string;\n\n /** Display text shown in menu. */\n label: string;\n\n /** Search source that returned the suggestion. */\n source: SearchSource;\n\n /** Links to the parent group. */\n group: SearchOptionGroup;\n\n /** The raw result from the search source. */\n result: SearchResult;\n}\n\nexport interface SearchState {\n input: string;\n results: SearchResultsState;\n selectedOption: SearchOption | undefined;\n\n // NOTE: Stable functions\n onOptionConfirmed: (newOption: SearchOption) => void;\n onInputChanged: (newInput: string) => void;\n\n searchAndSelect: (query: string) => Promise<SelectResult | undefined>;\n}\n\nexport type SearchResultsReady = {\n kind: \"ready\";\n results: SearchOptionGroup[];\n};\n\nexport type SearchResultsLoading = {\n kind: \"loading\";\n};\n\nexport type SearchResultsState = SearchResultsReady | SearchResultsLoading;\n\n/**\n * Keeps track of the current input text, active searches and their results.\n *\n * NOTE: it would be great to merge this state handling with the search controller\n * in a future revision.\n */\n// oxlint-disable-next-line max-params\nexport function useSearchState(\n sources: SearchSource[],\n searchTypingDelay: number | undefined,\n maxResultsPerGroup: number | undefined,\n map: MapModel,\n onSelect: SearchProps[\"onSelect\"] | undefined // TODO: refactor --> model\n): SearchState {\n interface FullSearchState {\n query: string;\n selectedOption: SearchOption | undefined;\n search: SearchResultsState;\n }\n\n const getId = useSearchSourceId();\n const controller = useController(sources, searchTypingDelay, maxResultsPerGroup, map);\n\n type Action =\n | { kind: \"input\"; query: string }\n | { kind: \"select-option\"; option: SearchOption }\n | { kind: \"load-results\" }\n | { kind: \"accept-results\"; results: SearchOptionGroup[] };\n\n const [state, dispatch] = useReducer(\n (current: FullSearchState, action: Action): FullSearchState => {\n switch (action.kind) {\n case \"input\":\n return {\n ...current,\n query: action.query,\n selectedOption: undefined\n };\n case \"select-option\":\n return {\n ...current,\n selectedOption: action.option,\n query: action.option.label\n };\n case \"load-results\":\n return {\n ...current,\n search: {\n kind: \"loading\"\n }\n };\n case \"accept-results\":\n return {\n ...current,\n search: {\n kind: \"ready\",\n results: action.results\n }\n };\n }\n },\n undefined,\n (): FullSearchState => ({\n query: \"\",\n selectedOption: undefined,\n search: {\n kind: \"ready\",\n results: []\n }\n })\n );\n\n // Stores the promise for the current search.\n // Any results from outdated searches are ignored.\n const currentSearch = useRef<Promise<unknown>>(undefined);\n const startSearch = useEvent((query: string) => {\n if (!controller) {\n currentSearch.current = undefined;\n dispatch({ kind: \"accept-results\", results: [] });\n return;\n }\n\n LOG.isDebug() && LOG.debug(`Starting new search for query ${JSON.stringify(query)}.`);\n dispatch({ kind: \"load-results\" });\n const promise = (currentSearch.current = search(controller, query, getId).then(\n (results) => {\n // Check if this job is still current\n if (currentSearch.current === promise) {\n dispatch({ kind: \"accept-results\", results });\n }\n }\n ));\n });\n\n /**\n * Searches for the specified query and automatically selects the first matching result.\n *\n * If the search controller is unavailable or the search returns no results,\n * the promise resolves to `undefined`.\n *\n * When a match is found, this function:\n * - Executes the search using the provided query.\n * - Updates the UI with the returned search results.\n * - Selects the first available search option and notifies the component's callback function (if any).\n *\n * @param query - The search query to execute.\n * @returns A promise that resolves to the selected {@link SearchResult} and its source,\n * or `undefined` if no result is found or the search cannot be performed.\n */\n const searchAndSelect = useEvent(async (query: string): Promise<SelectResult | undefined> => {\n if (!controller) {\n return undefined;\n }\n\n const groups = await search(controller, query, getId);\n const firstOption = groups.flatMap((g) => g.options)[0];\n if (!firstOption) {\n return undefined;\n }\n\n const { result, source } = firstOption;\n\n // update UI\n dispatch({\n kind: \"accept-results\",\n results: groups\n });\n dispatch({\n kind: \"select-option\",\n option: firstOption\n });\n\n // notify component callback\n onSelect?.({ result, source, trigger: \"api-select\" });\n return { result, source };\n });\n\n // Called when the user confirms a search result\n const onOptionConfirmed = useEvent((option: SearchOption) => {\n // Do not start a new search when the user confirms a result\n dispatch({ kind: \"select-option\", option });\n });\n\n // Called when a user types into the input field\n const onInputChanged = useEvent((newValue: string) => {\n // Trigger a new search if the user changes the query by typing\n dispatch({ kind: \"input\", query: newValue });\n startSearch(newValue);\n });\n\n return {\n input: state.query,\n results: state.search,\n selectedOption: state.selectedOption,\n onOptionConfirmed,\n onInputChanged,\n searchAndSelect\n };\n}\n\nasync function search(\n controller: SearchController,\n query: string,\n getId: GetSearchSourceId\n): Promise<SearchOptionGroup[]> {\n let suggestions: ResultGroup[];\n try {\n suggestions = await controller.search(query);\n } catch (error) {\n if (!isAbortError(error)) {\n LOG.error(`Search failed`, error);\n }\n suggestions = [];\n }\n return mapSuggestions(suggestions, getId);\n}\n\n/**\n * Creates a controller to search on the given sources.\n */\nfunction useController(\n sources: SearchSource[],\n searchTypingDelay: number | undefined,\n maxResultsPerGroup: number | undefined,\n map: MapModel\n) {\n const [controller, setController] = useState<SearchController | undefined>(undefined);\n useEffect(() => {\n const controller = new SearchController(map, sources);\n setController(controller);\n return () => {\n controller.destroy();\n setController(undefined);\n };\n }, [map, sources]);\n\n useEffect(() => {\n if (controller) {\n controller.searchTypingDelay = searchTypingDelay;\n }\n }, [controller, searchTypingDelay]);\n useEffect(() => {\n if (controller) {\n controller.maxResultsPerSource = maxResultsPerGroup;\n }\n }, [controller, maxResultsPerGroup]);\n return controller;\n}\n\n// Maps results from search source to objects that the UI can work with.\nfunction mapSuggestions(\n resultGroups: ResultGroup[],\n getId: GetSearchSourceId\n): SearchOptionGroup[] {\n return resultGroups.map((resultGroup): SearchOptionGroup => {\n const groupId = getId(resultGroup.source);\n const optionGroup: SearchOptionGroup = {\n id: groupId,\n label: resultGroup.label,\n options: []\n };\n\n for (const result of resultGroup.results) {\n optionGroup.options.push({\n // unique (in this component)\n value: `${groupId}-${result.id}`,\n label: result.label,\n group: optionGroup,\n source: resultGroup.source,\n result: result\n });\n }\n return optionGroup;\n });\n}\n\ntype GetSearchSourceId = (source: SearchSource) => string;\n\nfunction useSearchSourceId(): GetSearchSourceId {\n const sourceIds = useRef<WeakMap<SearchSource, string>>(undefined);\n const counter = useRef(0);\n // oxlint-disable-next-line react/refs\n if (!sourceIds.current) {\n sourceIds.current = new WeakMap();\n }\n\n return useCallback((searchSource: SearchSource) => {\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n const ids = sourceIds.current!;\n if (!ids.has(searchSource)) {\n ids.set(searchSource, `source-${counter.current++}`);\n }\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n return ids.get(searchSource)!;\n }, []);\n}\n"],"names":["controller"],"mappings":";;;;;;AAYA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AA4D1B,SAAS,cAAA,CACZ,OAAA,EACA,iBAAA,EACA,kBAAA,EACA,KACA,QAAA,EACW;AAOX,EAAA,MAAM,QAAQ,iBAAA,EAAkB;AAChC,EAAA,MAAM,UAAA,GAAa,aAAA,CAAc,OAAA,EAAS,iBAAA,EAAmB,oBAAoB,GAAG,CAAA;AAQpF,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,UAA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