下拉框支持拼音搜索

61 阅读1分钟
import { pinyin } from 'pinyin-pro'
export const filterOption = (input, option, config = null) => {
  if (!input) return option

  const label = option.label.toLowerCase()
  const fullPinyin = pinyin(option.label, { toneType: 'none' }).replace(/\s+/g, '').toLowerCase()
  const firstPinyin = pinyin(option.label, { toneType: 'none' })
    .split(/\s+/)
    .map((word) => word.charAt(0))
    .join('')
  return label.indexOf(input.toLowerCase()) >= 0 || firstPinyin.indexOf(input.toLowerCase()) >= 0 || fullPinyin.indexOf(input.toLowerCase()) >= 0
}