1
- import { useCallback , useContext , useEffect , useRef , useState } from 'react' ;
1
+ import {
2
+ MouseEventHandler ,
3
+ useCallback ,
4
+ useContext ,
5
+ useEffect ,
6
+ useRef ,
7
+ useState ,
8
+ } from 'react' ;
2
9
import { styled } from 'styled-components' ;
3
10
import { removeCachedSearchResults , useResource , useStore } from '@tomic/react' ;
4
11
import { DropdownPortalContext } from '../../Dropdown/dropdownContext' ;
5
12
import * as RadixPopover from '@radix-ui/react-popover' ;
6
13
import { SearchBoxWindow } from './SearchBoxWindow' ;
7
- import { FaSearch , FaTimes } from 'react-icons/fa' ;
14
+ import { FaExternalLinkAlt , FaSearch , FaTimes } from 'react-icons/fa' ;
8
15
import { ErrorChip } from '../ErrorChip' ;
9
16
import { useValidation } from '../formValidation/useValidation' ;
17
+ import { constructOpenURL } from '../../../helpers/navigation' ;
18
+ import { useNavigateWithTransition } from '../../../hooks/useNavigateWithTransition' ;
10
19
11
20
interface SearchBoxProps {
12
21
autoFocus ?: boolean ;
@@ -37,6 +46,7 @@ export function SearchBox({
37
46
onClose,
38
47
} : React . PropsWithChildren < SearchBoxProps > ) : JSX . Element {
39
48
const store = useStore ( ) ;
49
+ const navigate = useNavigateWithTransition ( ) ;
40
50
const selectedResource = useResource ( value ) ;
41
51
const triggerRef = useRef < HTMLButtonElement > ( null ) ;
42
52
const [ inputValue , setInputValue ] = useState ( '' ) ;
@@ -111,6 +121,21 @@ export function SearchBox({
111
121
setError ( undefined ) ;
112
122
} , [ setError , required , value , selectedResource ] ) ;
113
123
124
+ const openLink =
125
+ ! value || selectedResource . error
126
+ ? '#'
127
+ : constructOpenURL ( selectedResource . getSubject ( ) ) ;
128
+
129
+ const navigateToSelectedResource : MouseEventHandler < HTMLAnchorElement > =
130
+ e => {
131
+ e . preventDefault ( ) ;
132
+ navigate ( openLink ) ;
133
+ } ;
134
+
135
+ const title = selectedResource . error
136
+ ? selectedResource . getSubject ( )
137
+ : selectedResource . title ;
138
+
114
139
return (
115
140
< RadixPopover . Root open = { open } >
116
141
< RadixPopover . Anchor >
@@ -133,11 +158,7 @@ export function SearchBox({
133
158
} }
134
159
>
135
160
{ value ? (
136
- < ResourceTitle >
137
- { selectedResource . error
138
- ? selectedResource . getSubject ( )
139
- : selectedResource . title }
140
- </ ResourceTitle >
161
+ < ResourceTitle > { title } </ ResourceTitle >
141
162
) : (
142
163
< >
143
164
< FaSearch />
@@ -146,13 +167,24 @@ export function SearchBox({
146
167
) }
147
168
</ TriggerButton >
148
169
{ value && (
149
- < SearchBoxButton
150
- title = 'clear'
151
- onClick = { ( ) => onChange ( undefined ) }
152
- type = 'button'
153
- >
154
- < FaTimes />
155
- </ SearchBoxButton >
170
+ < >
171
+ < SearchBoxButton
172
+ as = 'a'
173
+ href = { openLink }
174
+ title = { `go to ${ title } ` }
175
+ onClick = { navigateToSelectedResource }
176
+ type = 'button'
177
+ >
178
+ < FaExternalLinkAlt />
179
+ </ SearchBoxButton >
180
+ < SearchBoxButton
181
+ title = 'clear'
182
+ onClick = { ( ) => onChange ( undefined ) }
183
+ type = 'button'
184
+ >
185
+ < FaTimes />
186
+ </ SearchBoxButton >
187
+ </ >
156
188
) }
157
189
{ children }
158
190
{ error && (
0 commit comments