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