Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search input clearable component #3465

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion joi/src/core/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ import React, { ReactNode, forwardRef } from 'react'
import { twMerge } from 'tailwind-merge'

import './styles.scss'
import { Cross2Icon } from '@radix-ui/react-icons'

export interface Props extends React.InputHTMLAttributes<HTMLInputElement> {
textAlign?: 'left' | 'right'
prefixIcon?: ReactNode
suffixIcon?: ReactNode
onCLick?: () => void
clearable?: boolean
onClear?: () => void
}

const Input = forwardRef<HTMLInputElement, Props>(
(
{ className, type, textAlign, prefixIcon, suffixIcon, onClick, ...props },
{
className,
type,
textAlign,
prefixIcon,
suffixIcon,
onClick,
onClear,
clearable,
...props
},
ref
) => {
return (
Expand All @@ -27,6 +40,11 @@ const Input = forwardRef<HTMLInputElement, Props>(
{suffixIcon}
</div>
)}
{clearable && (
<div className="input__clear-icon" onClick={onClear}>
<Cross2Icon className="text-red-200" />
</div>
)}
<input
type={type}
className={twMerge(
Expand Down
7 changes: 7 additions & 0 deletions joi/src/core/Input/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@
padding-right: 32px;
}
}
&__clear-icon {
@apply absolute right-3 top-1/2 -translate-y-1/2 cursor-pointer;
color: hsla(var(--input-icon));
+ .input {
padding: 0 32px;
}
}
}
8 changes: 8 additions & 0 deletions web/containers/ModelSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const ModelSearch = ({ onSearchLocal }: Props) => {
[debounced]
)

const onClear = useCallback(() => {
setSearchText('')
debounced()
}, [debounced])

const onKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
Expand All @@ -80,6 +85,9 @@ const ModelSearch = ({ onSearchLocal }: Props) => {
placeholder="Search or paste Hugging Face URL"
onChange={onSearchChanged}
onKeyDown={onKeyDown}
value={searchText}
clearable={searchText.length > 0}
onClear={onClear}
/>
)
}
Expand Down
3 changes: 3 additions & 0 deletions web/screens/Settings/CoreExtensions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ const ExtensionCatalog = () => {
<Input
prefixIcon={<SearchIcon size={16} />}
placeholder="Search"
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
clearable={searchText.length > 0}
onClear={() => setSearchText('')}
/>
</div>
<div>
Expand Down
Loading