normalize title
This commit is contained in:
@@ -48,6 +48,13 @@ interface CoursePickerProps extends Omit<
|
||||
error?: any
|
||||
}
|
||||
|
||||
const normalize = (value: string) =>
|
||||
value
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-z0-9]/g, '')
|
||||
|
||||
export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
|
||||
({ value, onChange, options, error, ...props }, ref) => {
|
||||
const { hits } = use(options)
|
||||
@@ -55,8 +62,13 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
|
||||
const [open, { set }] = useToggle()
|
||||
const [sort, { toggle }] = useToggle('a-z', 'z-a')
|
||||
const fuse = useMemo(() => {
|
||||
return new Fuse(hits, {
|
||||
keys: ['name'],
|
||||
const hits_ = hits.map((item) => ({
|
||||
...item,
|
||||
_name: normalize(item.name)
|
||||
}))
|
||||
|
||||
return new Fuse(hits_, {
|
||||
keys: ['_name'],
|
||||
threshold: 0.3,
|
||||
includeMatches: true
|
||||
})
|
||||
@@ -65,7 +77,7 @@ export const CoursePicker = forwardRef<HTMLInputElement, CoursePickerProps>(
|
||||
const filtered = useMemo(() => {
|
||||
const results = !search
|
||||
? hits
|
||||
: fuse.search(search).map(({ item }) => item)
|
||||
: fuse.search(normalize(search)).map(({ item }) => item)
|
||||
|
||||
return results.sort((a, b) => {
|
||||
const comparison = a.name.localeCompare(b.name)
|
||||
|
||||
Reference in New Issue
Block a user