edit combo box dynamic items

This commit is contained in:
Alihaghighattalab
2024-08-21 10:50:50 +03:30
parent 643f3cb497
commit 387a94e918
2 changed files with 14 additions and 16 deletions
+9 -16
View File
@@ -1,33 +1,27 @@
import { Combobox, ComboboxButton, ComboboxInput, ComboboxOption, ComboboxOptions } from '@headlessui/react' import { Combobox, ComboboxButton, ComboboxInput, ComboboxOption, ComboboxOptions } from '@headlessui/react'
import { ArrowDown2 } from 'iconsax-react' import { ArrowDown2 } from 'iconsax-react'
import { FC, useState } from 'react' import { FC, useState } from 'react'
import { ComboBoxItems } from '../../types'
const value = [
{ id: 1, name: 'تهران' },
{ id: 2, name: 'شیراز' },
{ id: 3, name: 'اصفهان' },
{ id: 4, name: 'اراک' },
{ id: 5, name: 'تبریز' },
]
type Props = { type Props = {
field?: any field?: any
placeholder?: string placeholder?: string
className?: string className?: string,
data?: ComboBoxItems[] | []
} }
export const ComboBox: FC<Props> = ({ field, placeholder, className }) => { export const ComboBox: FC<Props> = ({ field, placeholder, className, data = [] }) => {
const [query, setQuery] = useState('') const [query, setQuery] = useState('')
const filteredValue = const filteredValue =
query === '' query === ''
? value ? data
: value?.filter((value) => { : data?.filter((value) => {
return value.name.toLowerCase().includes(query.toLowerCase()) return value.name.toLowerCase().includes(query.toLowerCase())
}) })
return ( return (
<Combobox value={field && field.value} onChange={field && field.onChange} onClose={() => setQuery('')}> <Combobox value={field && field.value} onChange={field && field.onChange} onClose={() => setQuery('')} disabled={data?.length <= 0}>
<div className="relative w-full"> <div className="relative w-full">
<ComboboxInput <ComboboxInput
placeholder={placeholder} placeholder={placeholder}
@@ -42,8 +36,7 @@ export const ComboBox: FC<Props> = ({ field, placeholder, className }) => {
<ArrowDown2 className="size-5 absolute left-5 top-5 p-0 text-secondary-text-color/70" /> <ArrowDown2 className="size-5 absolute left-5 top-5 p-0 text-secondary-text-color/70" />
</ComboboxButton> </ComboboxButton>
</div> </div>
{data?.length > 0 && <ComboboxOptions
<ComboboxOptions
anchor="bottom start" anchor="bottom start"
transition transition
className="bg-white shadow min-w-[220px] mt-2 rounded-xl p-2" className="bg-white shadow min-w-[220px] mt-2 rounded-xl p-2"
@@ -57,7 +50,7 @@ export const ComboBox: FC<Props> = ({ field, placeholder, className }) => {
<div className="text-sm/6 text-black">{value?.name}</div> <div className="text-sm/6 text-black">{value?.name}</div>
</ComboboxOption> </ComboboxOption>
))} ))}
</ComboboxOptions> </ComboboxOptions>}
</Combobox> </Combobox>
) )
} }
+5
View File
@@ -276,3 +276,8 @@ export interface ScoreResponse {
score: number, score: number,
total: number total: number
} }
export interface ComboBoxItems {
name: string,
id: string | number
}