2025-03-13 15:52:43 +08:00
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
2025-03-12 17:36:44 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2025-03-13 15:52:43 +08:00
|
|
|
import { useDebounceFn } from 'ahooks'
|
|
|
|
import { useEducation } from './hooks'
|
2025-03-11 15:49:41 +08:00
|
|
|
import Input from '@/app/components/base/input'
|
|
|
|
import {
|
|
|
|
PortalToFollowElem,
|
|
|
|
PortalToFollowElemContent,
|
|
|
|
PortalToFollowElemTrigger,
|
|
|
|
} from '@/app/components/base/portal-to-follow-elem'
|
|
|
|
|
2025-03-13 15:52:43 +08:00
|
|
|
type SearchInputProps = {
|
|
|
|
value?: string
|
|
|
|
onChange: (value: string) => void
|
|
|
|
}
|
|
|
|
const SearchInput = ({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
}: SearchInputProps) => {
|
2025-03-12 17:36:44 +08:00
|
|
|
const { t } = useTranslation()
|
2025-03-13 15:52:43 +08:00
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const {
|
|
|
|
schools,
|
|
|
|
isLoading,
|
|
|
|
querySchoolsWithDebounced,
|
|
|
|
} = useEducation()
|
|
|
|
|
|
|
|
const {
|
|
|
|
run: handleSearch,
|
|
|
|
} = useDebounceFn(() => {
|
|
|
|
querySchoolsWithDebounced({
|
|
|
|
keywords: value,
|
|
|
|
page: 1,
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
wait: 300,
|
|
|
|
})
|
|
|
|
const handleValueChange = useCallback((e: { target: { value: string } }) => {
|
|
|
|
onChange(e.target.value)
|
|
|
|
handleSearch()
|
|
|
|
}, [handleSearch, onChange])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isLoading && !open && schools.length)
|
|
|
|
setOpen(true)
|
|
|
|
}, [isLoading, open, schools])
|
2025-03-12 17:36:44 +08:00
|
|
|
|
2025-03-11 15:49:41 +08:00
|
|
|
return (
|
2025-03-13 15:52:43 +08:00
|
|
|
<PortalToFollowElem
|
|
|
|
open={open}
|
|
|
|
onOpenChange={setOpen}
|
|
|
|
placement='bottom'
|
|
|
|
offset={4}
|
|
|
|
>
|
2025-03-12 10:05:10 +08:00
|
|
|
<PortalToFollowElemTrigger className='block w-full'>
|
|
|
|
<Input
|
|
|
|
className='w-full'
|
2025-03-12 17:36:44 +08:00
|
|
|
placeholder={t('education.form.schoolName.placeholder')}
|
2025-03-13 15:52:43 +08:00
|
|
|
value={value}
|
|
|
|
onChange={handleValueChange}
|
2025-03-12 10:05:10 +08:00
|
|
|
/>
|
2025-03-11 15:49:41 +08:00
|
|
|
</PortalToFollowElemTrigger>
|
|
|
|
<PortalToFollowElemContent>
|
|
|
|
<div className='p-1 border-[0.5px] border-components-panel-border bg-components-panel-bg-blur rounded-xl'>
|
2025-03-13 15:52:43 +08:00
|
|
|
{
|
|
|
|
schools.map((school, index) => (
|
|
|
|
<div
|
|
|
|
key={index}
|
|
|
|
className='flex items-center px-2 py-1.5 h-8 system-md-regular text-text-secondary truncate'
|
|
|
|
title={school}
|
|
|
|
>
|
|
|
|
{school}
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
}
|
2025-03-11 15:49:41 +08:00
|
|
|
</div>
|
|
|
|
</PortalToFollowElemContent>
|
|
|
|
</PortalToFollowElem>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SearchInput
|