Skip to content

Commit

Permalink
feat(components): add debounced-textfield (#3)
Browse files Browse the repository at this point in the history
* feat: add a debounced-textfield componet

* feat: add a debounced-textfield componet

* feat: add a debounced-textfield componet

* feat: add a debounced-textfield componet
  • Loading branch information
iseki0 authored Jan 30, 2025
1 parent 430820f commit 4bf8b18
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/components/debounced-textfield.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { ChangeEventHandler, FC, FocusEventHandler, ReactNode } from 'react'

import { TextField } from '@radix-ui/themes'
import { useCallback, useState } from 'react'

interface WithOnBlurValueProps {
onBlurValueChange?: (text: string) => undefined | void
}

interface WithOnBlurValueRequiredProps {
children?: ReactNode
onBlur?: FocusEventHandler<HTMLInputElement>
onChange?: ChangeEventHandler<HTMLInputElement>
value?: number | string
}

const withOnBlurValue = <P extends WithOnBlurValueRequiredProps>(Component: FC<P>): FC<P & WithOnBlurValueProps> => ({
onBlur,
onBlurValueChange,
onChange,
value,
...rest
}) => {
const [indeterminate, setIndeterminate] = useState<typeof value>(undefined)
const [prevValue, setPrevValue] = useState(value)
if (prevValue !== value) {
setPrevValue(value)
setIndeterminate(undefined)
}
const _onBlur = useCallback<Exclude<typeof onBlur, undefined>>((e) => {
onBlur?.(e)
if (indeterminate !== undefined) {
onBlurValueChange?.(indeterminate.toString())
setIndeterminate(undefined)
}
}, [onBlur, onBlurValueChange, indeterminate, setIndeterminate])
const _onChange = useCallback<Exclude<typeof onChange, undefined>>((e) => {
onChange?.(e)
setIndeterminate(e.currentTarget.value)
}, [onChange, setIndeterminate])
const p = { onBlur: _onBlur, onChange: _onChange, value: indeterminate ?? value, ...rest }
return <Component {...p as P} />
}

export const DebouncedTextField = withOnBlurValue(TextField.Root)
9 changes: 5 additions & 4 deletions src/components/settings/settings-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useOnline } from '@uiw/react-use-online'
import { useChatModel, useEmbedModel } from '../../hooks/use-model'
import { useListModels } from '../../hooks/xsai/use-list-models'
import * as Sheet from '../ui/sheet'
import {DebouncedTextField} from "../debounced-textfield.tsx";

export const SettingsChat = () => {
const [chatModel, setChatModel] = useChatModel()
Expand Down Expand Up @@ -59,18 +60,18 @@ export const SettingsChat = () => {
<Text as="div" mb="1" weight="bold">
Base URL
</Text>
<TextField.Root disabled placeholder="https://openai.com/v1/">
<DebouncedTextField disabled placeholder="https://openai.com/v1/">
<TextField.Slot />
</TextField.Root>
</DebouncedTextField>
</label>

<label>
<Text as="div" mb="1" weight="bold">
API Key
</Text>
<TextField.Root disabled placeholder="sk-abcdefghijklmnop123">
<DebouncedTextField disabled placeholder="sk-abcdefghijklmnop123">
<TextField.Slot />
</TextField.Root>
</DebouncedTextField>
</label>

<Separator size="3" />
Expand Down

0 comments on commit 4bf8b18

Please sign in to comment.