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