This commit is contained in:
2026-01-05 17:25:29 -03:00
parent b371f33a0e
commit d7f66d929d

View File

@@ -62,27 +62,29 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
const [open, { set }] = useToggle()
const [sort, { toggle }] = useToggle('a-z', 'z-a')
const fuse = useMemo(() => {
const hits_ = hits.map((item) => ({
...item,
_name: normalize(item.name)
}))
return new Fuse(hits_, {
keys: ['_name'],
return new Fuse(hits, {
keys: ['name'],
threshold: 0.3,
includeMatches: true
includeMatches: true,
getFn: (obj, path) => {
const value = obj[path as keyof typeof obj]
return typeof value === 'string' ? normalize(value) : value
}
})
}, [hits])
const filtered = useMemo(() => {
const results = !search
? hits
: fuse.search(normalize(search)).map(({ item }) => item)
return results.sort((a, b) => {
if (!search) {
return [...hits].sort((a, b) => {
const comparison = a.name.localeCompare(b.name)
return sort === 'a-z' ? comparison : -comparison
})
}
return fuse.search(search).map(({ item, matches }) => ({
...item,
matches
}))
}, [search, fuse, hits, sort])
return (
@@ -124,6 +126,7 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
tabIndex={-1}
className="cursor-pointer text-muted-foreground hover:text-accent-foreground"
onClick={toggle}
disabled={search.length > 0}
>
{sort == 'a-z' ? <ArrowDownAZIcon /> : <ArrowUpAZIcon />}
</Button>
@@ -143,8 +146,10 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
id,
name,
access_period,
metadata__unit_price: unit_price
}) => (
metadata__unit_price: unit_price,
matches
}) => {
return (
<CommandItem
key={id}
value={id}
@@ -168,6 +173,7 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
/>
</CommandItem>
)
}
)}
</CommandGroup>
</CommandList>