Skip to content

Instantly share code, notes, and snippets.

@thekarel
Created July 15, 2021 16:25
Show Gist options
  • Save thekarel/a85af52782545f0848526121e50cfef6 to your computer and use it in GitHub Desktop.
Save thekarel/a85af52782545f0848526121e50cfef6 to your computer and use it in GitHub Desktop.
antd Cascader case-insensitive search
import {CascaderOptionType, FilledFieldNamesType} from 'antd/lib/cascader'
export const caseInsensitiveFilter = (inputValue: string, path: CascaderOptionType[], names: FilledFieldNamesType) =>
path.some((option) => option[names.label].toLowerCase().includes(inputValue.toLowerCase()))
import {CascaderOptionType, FilledFieldNamesType} from 'antd/lib/cascader'
import * as React from 'react'
export const caseInsensitiveHighlight = (
inputValue: string,
path: CascaderOptionType[],
prefixCls: string | undefined,
names: FilledFieldNamesType,
) =>
path.map((option, index) => {
const label = option[names.label]
const node = label.toLowerCase().includes(inputValue.toLowerCase())
? highlightKeyword(label, inputValue, prefixCls)
: label
return index === 0 ? node : [' / ', node]
})
const highlightKeyword = (string: string, search: string, prefixCls?: string) => {
const startIndex = string.toLowerCase().indexOf(search.toLowerCase())
if (startIndex === -1) {
return string
}
const head = string.substr(0, startIndex)
const matchingPart = string.substr(startIndex, search.length)
const tail = string.substr(startIndex + search.length)
const highlightedSpan = (
<span className={`${prefixCls}-menu-item-keyword`} key="seperator">
{matchingPart}
</span>
)
return [head, highlightedSpan, tail]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment