fix: support fuzzy search in cascader & fix undefined label (#718)

Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachycode#718
Co-authored-by: sealday <zhanglin@daoyoucloud.com>
Co-committed-by: sealday <zhanglin@daoyoucloud.com>
This commit is contained in:
sealday 2024-04-15 19:03:29 +08:00 committed by baijingfeng
parent 0e97825b13
commit bbf19d31c7
3 changed files with 47 additions and 10 deletions

View File

@ -0,0 +1,7 @@
---
"@hera/plugin-rental": patch
"@nocobase/client": patch
"@nocobase/utils": patch
---
support fuzzy search in cascader

View File

@ -1,17 +1,24 @@
import { ArrayItems, FormItem } from '@formily/antd-v5'; import { ArrayItems, FormItem } from '@formily/antd-v5';
import { createForm, onFormValuesChange } from '@nocobase/schema'; import {
import { FormProvider, connect, createSchemaField, observer, useField, useFieldSchema } from '@nocobase/schema'; FormProvider,
import { uid } from '@nocobase/schema'; connect,
import { Input, Space, Spin, Tag } from 'antd'; createForm,
import dayjs from 'dayjs'; createSchemaField,
observer,
onFormValuesChange,
uid,
useField,
useFieldSchema,
} from '@nocobase/schema';
import { fuzzysearch } from '@nocobase/utils/client';
import { Input, Space } from 'antd';
import React, { useEffect, useMemo, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { css, useAPIClient, useCollectionManager_deprecated } from '../../..'; import { CustomCascader, SchemaComponent } from '../..';
import { css, useAPIClient } from '../../..';
import { mergeFilter } from '../../../filter-provider/utils'; import { mergeFilter } from '../../../filter-provider/utils';
import { CustomCascader, SchemaComponent, useCompile, useDesignable } from '../..'; import { useAssociationFieldContext } from './hooks';
import useServiceOptions, { useAssociationFieldContext } from './hooks';
const EMPTY = 'N/A';
const SchemaField = createSchemaField({ const SchemaField = createSchemaField({
components: { components: {
Space, Space,
@ -111,7 +118,9 @@ const Cascade = connect((props) => {
setOptions(result); setOptions(result);
} }
}; };
const filter = (inputValue: string, path) => path.some((option) => (option.label as string).includes(inputValue));
const filter = (inputValue: string, path) =>
path.some((option) => fuzzysearch(inputValue, (option.label as string) ?? ''));
return ( return (
<Space <Space
wrap wrap

View File

@ -45,3 +45,24 @@ export const hasEmptyValue = (objOrArr: object | any[]) => {
export const nextTick = (fn: () => void) => { export const nextTick = (fn: () => void) => {
setTimeout(fn); setTimeout(fn);
}; };
export function fuzzysearch(needle: string, haystack: string): boolean {
const hlen = haystack.length;
const nlen = needle.length;
if (nlen > hlen) {
return false;
}
if (nlen === hlen) {
return needle === haystack;
}
outer: for (let i = 0, j = 0; i < nlen; i++) {
const nch = needle.charCodeAt(i);
while (j < hlen) {
if (haystack.charCodeAt(j++) === nch) {
continue outer;
}
}
return false;
}
return true;
}