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' ;
29import { styled } from 'styled-components' ;
310import { removeCachedSearchResults , useResource , useStore } from '@tomic/react' ;
411import { DropdownPortalContext } from '../../Dropdown/dropdownContext' ;
512import * as RadixPopover from '@radix-ui/react-popover' ;
613import { SearchBoxWindow } from './SearchBoxWindow' ;
7- import { FaSearch , FaTimes } from 'react-icons/fa' ;
14+ import { FaExternalLinkAlt , FaSearch , FaTimes } from 'react-icons/fa' ;
815import { ErrorChip } from '../ErrorChip' ;
916import { useValidation } from '../formValidation/useValidation' ;
17+ import { constructOpenURL } from '../../../helpers/navigation' ;
18+ import { useNavigateWithTransition } from '../../../hooks/useNavigateWithTransition' ;
1019
1120interface SearchBoxProps {
1221 autoFocus ?: boolean ;
@@ -37,6 +46,7 @@ export function SearchBox({
3746 onClose,
3847} : React . PropsWithChildren < SearchBoxProps > ) : JSX . Element {
3948 const store = useStore ( ) ;
49+ const navigate = useNavigateWithTransition ( ) ;
4050 const selectedResource = useResource ( value ) ;
4151 const triggerRef = useRef < HTMLButtonElement > ( null ) ;
4252 const [ inputValue , setInputValue ] = useState ( '' ) ;
@@ -111,6 +121,21 @@ export function SearchBox({
111121 setError ( undefined ) ;
112122 } , [ setError , required , value , selectedResource ] ) ;
113123
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+
114139 return (
115140 < RadixPopover . Root open = { open } >
116141 < RadixPopover . Anchor >
@@ -133,11 +158,7 @@ export function SearchBox({
133158 } }
134159 >
135160 { value ? (
136- < ResourceTitle >
137- { selectedResource . error
138- ? selectedResource . getSubject ( )
139- : selectedResource . title }
140- </ ResourceTitle >
161+ < ResourceTitle > { title } </ ResourceTitle >
141162 ) : (
142163 < >
143164 < FaSearch />
@@ -146,13 +167,24 @@ export function SearchBox({
146167 ) }
147168 </ TriggerButton >
148169 { 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+ </ >
156188 ) }
157189 { children }
158190 { error && (
0 commit comments