|
|
|
@ -4,13 +4,13 @@ import type { Ref, ShallowRef } from 'vue' |
|
|
|
/** |
|
|
|
* @param {*} watchName |
|
|
|
* @param {Ref<UnwrapRef<number>>} [searchType=0] 0:键盘搜索 1:手写搜索 |
|
|
|
* @return {*} [searchShopListRef] |
|
|
|
* @return {*} [searchShopList] |
|
|
|
*/ |
|
|
|
export const useSearchShop = (watchName: Ref<string>, searchType: Ref<0 | 1>): { searchShopListRef: ShallowRef<Shop[]> } => { |
|
|
|
export const useSearchShop = (watchName: Ref<string>, searchType: Ref<0 | 1>): { searchShopList: ShallowRef<Shop[]> } => { |
|
|
|
const store = useRootStore() |
|
|
|
const searchShopListRef = shallowRef<Shop[]>([]) |
|
|
|
const cacheFirstSearchListRef = shallowRef<Shop[]>([]) //缓存第一次检索首字母后的列表结果
|
|
|
|
const shopListRef = computed(() => store.shopList) |
|
|
|
const searchShopList = shallowRef<Shop[]>([]) |
|
|
|
const cacheFirstSearchList = shallowRef<Shop[]>([]) //缓存第一次检索首字母后的列表结果
|
|
|
|
const shopList = computed(() => store.shopList) |
|
|
|
|
|
|
|
watch(watchName, val => { |
|
|
|
if (val.length) { |
|
|
|
@ -18,29 +18,31 @@ export const useSearchShop = (watchName: Ref<string>, searchType: Ref<0 | 1>): { |
|
|
|
let _shopList: Shop[] = [] |
|
|
|
if (val.length === 1) { |
|
|
|
//缓存第一次搜索结果
|
|
|
|
cacheFirstSearchListRef.value = shopListRef.value.filter( |
|
|
|
cacheFirstSearchList.value = shopList.value.filter( |
|
|
|
item => |
|
|
|
item.initials?.toUpperCase()?.startsWith(val) || |
|
|
|
item.shopNameEn?.toUpperCase()?.startsWith(val) || |
|
|
|
item.shopName?.toUpperCase()?.startsWith(val) |
|
|
|
item.shopName?.toUpperCase()?.startsWith(val) || |
|
|
|
item.spelling?.toUpperCase()?.startsWith(val) |
|
|
|
) |
|
|
|
} else { |
|
|
|
const searchName = val.slice(1) |
|
|
|
_shopList = cacheFirstSearchListRef.value.filter( |
|
|
|
_shopList = cacheFirstSearchList.value.filter( |
|
|
|
item => |
|
|
|
item.initials?.toUpperCase()?.includes(searchName) || |
|
|
|
item.shopNameEn?.toUpperCase()?.includes(searchName) || |
|
|
|
item.shopName?.toUpperCase()?.includes(searchName) |
|
|
|
item.shopName?.toUpperCase()?.includes(searchName) || |
|
|
|
item.spelling?.toUpperCase()?.includes(searchName) |
|
|
|
) |
|
|
|
} |
|
|
|
searchShopListRef.value = val.length === 1 ? cacheFirstSearchListRef.value : _shopList |
|
|
|
searchShopList.value = val.length === 1 ? cacheFirstSearchList.value : _shopList |
|
|
|
} else { |
|
|
|
searchShopListRef.value = val.length ? shopListRef.value.filter(item => item.shopName.includes(val)) : [] |
|
|
|
searchShopList.value = val.length ? shopList.value.filter(item => item.shopName.includes(val)) : [] |
|
|
|
} |
|
|
|
} else { |
|
|
|
searchShopListRef.value = [] |
|
|
|
cacheFirstSearchListRef.value = [] |
|
|
|
searchShopList.value = [] |
|
|
|
cacheFirstSearchList.value = [] |
|
|
|
} |
|
|
|
}) |
|
|
|
return { searchShopListRef } |
|
|
|
return { searchShopList } |
|
|
|
} |
|
|
|
|