forked from FuelLabs/fuel-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5852c5b
commit 3f2b986
Showing
27 changed files
with
1,310 additions
and
1,087 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
packages/app-explorer/src/systems/Core/components/Search/SearchInput/SearchInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
'use client'; | ||
|
||
import type { GQLSearchResult, Maybe } from '@fuel-explorer/graphql'; | ||
import type { BaseProps, InputProps } from '@fuels/ui'; | ||
import { Focus, Icon, IconButton, Input, Tooltip, VStack } from '@fuels/ui'; | ||
import { IconCheck, IconSearch, IconX } from '@tabler/icons-react'; | ||
import type { KeyboardEvent } from 'react'; | ||
import { useContext, useRef, useState } from 'react'; | ||
import { useFormStatus } from 'react-dom'; | ||
|
||
import { cx } from '../../../utils/cx'; | ||
|
||
import { SearchResultDropdown } from '../SearchResultDropdown'; | ||
import { SearchContext } from '../SearchWidget'; | ||
import { usePropagateInputMouseClick } from '../hooks/usePropagateInputMouseClick'; | ||
import { DEFAULT_SEARCH_INPUT_WIDTH } from './constants'; | ||
import { styles } from './styles'; | ||
|
||
type SearchInputProps = BaseProps<InputProps> & { | ||
onSubmit?: (value: string) => void; | ||
searchResult?: Maybe<GQLSearchResult>; | ||
alwaysDisplayActionButtons?: boolean; | ||
variablePosition?: boolean; | ||
}; | ||
|
||
export function SearchInput({ | ||
value: initialValue = '', | ||
className, | ||
autoFocus, | ||
placeholder = 'Search here...', | ||
searchResult, | ||
variablePosition, | ||
...props | ||
}: SearchInputProps) { | ||
const classes = styles(); | ||
const [value, setValue] = useState<string>(initialValue as string); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [hasSubmitted, setHasSubmitted] = useState(false); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const { pending } = useFormStatus(); | ||
const { dropdownRef } = useContext(SearchContext); | ||
const openDropdown = hasSubmitted | ||
? !pending | ||
: isOpen && !pending && !!searchResult; | ||
|
||
usePropagateInputMouseClick({ | ||
containerRef, | ||
inputRef, | ||
enabled: openDropdown, | ||
}); | ||
|
||
function handleChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
setValue(event.target.value); | ||
} | ||
|
||
function handleSubmit() { | ||
setIsOpen(true); | ||
setHasSubmitted(true); | ||
} | ||
|
||
function close() { | ||
setIsOpen(false); | ||
setHasSubmitted(false); | ||
} | ||
|
||
function handleClear() { | ||
setValue(''); | ||
close(); | ||
} | ||
|
||
function handleFocus() { | ||
setIsFocused(true); | ||
} | ||
|
||
function handleBlur() { | ||
setIsFocused(false); | ||
} | ||
|
||
function onKeyDown(e: KeyboardEvent<HTMLInputElement>) { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
(e.target as HTMLFormElement).form?.dispatchEvent( | ||
new Event('submit', { cancelable: true, bubbles: true }), | ||
); | ||
handleSubmit(); | ||
} | ||
} | ||
|
||
return ( | ||
<div className="relative"> | ||
<VStack | ||
gap="0" | ||
data-variable-position={variablePosition} | ||
className={classes.searchBox()} | ||
data-active={isFocused || openDropdown} | ||
> | ||
<Focus.ArrowNavigator autoFocus={autoFocus}> | ||
<div ref={containerRef} className={classes.inputContainer()}> | ||
<Input | ||
{...props} | ||
ref={inputRef} | ||
name="query" | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={handleChange} | ||
variant="surface" | ||
radius="large" | ||
size="3" | ||
data-active={isFocused || openDropdown} | ||
className={cx(className, classes.inputWrapper())} | ||
type="search" | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
onKeyDown={onKeyDown} | ||
> | ||
<div | ||
data-show={isFocused} | ||
className={classes.inputActionsContainer()} | ||
> | ||
<Input.Slot side="right"> | ||
<Tooltip content="Submit"> | ||
<IconButton | ||
type="submit" | ||
aria-label="Submit" | ||
icon={IconCheck} | ||
iconColor="text-brand" | ||
variant="link" | ||
className="!ml-0 tablet:ml-2" | ||
isLoading={pending} | ||
onClick={handleSubmit} | ||
/> | ||
</Tooltip> | ||
<IconButton | ||
aria-label="Clear" | ||
icon={IconX} | ||
iconColor="text-gray-11" | ||
variant="link" | ||
className="m-0" | ||
onClick={handleClear} | ||
/> | ||
</Input.Slot> | ||
</div> | ||
|
||
<Input.Slot | ||
data-show={!isFocused} | ||
side="right" | ||
className="[&[data-show=false]]:hidden" | ||
> | ||
<Icon icon={IconSearch} size={16} /> | ||
</Input.Slot> | ||
</Input> | ||
</div> | ||
</Focus.ArrowNavigator> | ||
<SearchResultDropdown | ||
ref={dropdownRef} | ||
width={ | ||
containerRef.current?.offsetWidth || DEFAULT_SEARCH_INPUT_WIDTH | ||
} | ||
searchResult={searchResult} | ||
searchValue={value} | ||
openDropdown={openDropdown} | ||
isFocused={isFocused} | ||
onSelectItem={() => { | ||
handleClear(); | ||
}} | ||
onOpenChange={(open) => { | ||
setIsOpen(open); | ||
if (!open) { | ||
close(); | ||
} | ||
}} | ||
/> | ||
</VStack> | ||
</div> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/app-explorer/src/systems/Core/components/Search/SearchInput/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEFAULT_SEARCH_INPUT_WIDTH = 400; |
1 change: 1 addition & 0 deletions
1
packages/app-explorer/src/systems/Core/components/Search/SearchInput/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './SearchInput'; |
28 changes: 28 additions & 0 deletions
28
packages/app-explorer/src/systems/Core/components/Search/SearchInput/styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { tv } from 'tailwind-variants'; | ||
|
||
export const styles = tv({ | ||
slots: { | ||
searchBox: [ | ||
'transition-all duration-200 [&[data-active=false]]:ease-in [&[data-active=true]]:ease-out', | ||
'group justify-center items-center', | ||
'block left-0 w-full', // needed for properly execution of transitions | ||
'[&[data-variable-position=true]]:[&[data-active=true]]:w-[calc(100vw+1px)] [&[data-variable-position=true]]:[&[data-active=true]]:left-[-64px] tablet:[&[data-active=true]]:w-full', | ||
'[&[data-active=true]]:absolute tablet:[&[data-active=true]]:left-0 [&[data-active=true]]:right-0', | ||
'[&[data-variable-position=true]]:[&[data-active=true]]:top-[-14px] [&[data-variable-position=true]]:tablet:[&[data-active=true]]:top-[-4px] [&[data-variable-position=true]]:desktop:[&[data-active=true]]:top-[-20px]', | ||
'[&[data-active=true]]:z-50', | ||
], | ||
inputContainer: 'w-full', | ||
inputWrapper: [ | ||
'outline-none h-[40px] group-[&[data-active=true]]:h-[60px] tablet:group-[&[data-active=true]]:h-[40px]', | ||
'group-[&[data-active=true]]:rounded-none tablet:group-[&[data-active=true]]:rounded-[var(--text-field-border-radius)] ', | ||
'border-x-[1px] border-y-[1px] group-[&[data-active=true]]:border-x-0 group-[&[data-active=true]]:border-y-0 tablet:group-[&[data-active=true]]:border-x-[1px] tablet:group-[&[data-active=true]]:border-y-[1px]', | ||
'border-[var(--color-border)] shadow-none', | ||
'bg-none dark:bg-[var(--color-surface)] group-[&[data-active=true]]:bg-[var(--color-panel-solid)]', | ||
'[&_.rt-TextFieldChrome]:bg-gray-1 [&_.rt-TextFieldChrome]:outline-none', | ||
'[&_.rt-TextFieldChrome]:[&[data-opened=true]]:rounded-b-none', | ||
'group-[&[data-active=true]]:[&_.rt-TextFieldChrome]:shadow-none', | ||
], | ||
inputActionsContainer: | ||
'[&[data-show=false]]:hidden absolute flex items-center h-full right-0 top-0 transform', | ||
}, | ||
}); |
Oops, something went wrong.