fix: subtable search (#1124)

Reviewed-on: daoyoucloud/tachybase#1124
This commit is contained in:
sealday 2024-06-05 15:17:20 +08:00
parent 019a42b0ed
commit 80e65fe5f6
2 changed files with 28 additions and 33 deletions

View File

@ -1,4 +1,4 @@
import React, { useContext, useEffect, useMemo, useState } from 'react'; import React, { useContext, useMemo, useState } from 'react';
import { import {
action, action,
ArrayField, ArrayField,
@ -7,13 +7,11 @@ import {
observer, observer,
RecursionField, RecursionField,
useFieldSchema, useFieldSchema,
useForm,
} from '@tachybase/schema'; } from '@tachybase/schema';
import { useAsyncEffect } from 'ahooks'; import { Button } from 'antd';
import { Button, Tabs } from 'antd';
import { createStyles } from 'antd-style'; import { createStyles } from 'antd-style';
import { set, unionBy, uniqBy } from 'lodash'; import { unionBy, uniqBy } from 'lodash';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
@ -23,12 +21,11 @@ import {
SchemaComponentOptions, SchemaComponentOptions,
useActionContext, useActionContext,
} from '../..'; } from '../..';
import { useAPIClient, useRequest } from '../../../api-client';
import { useCreateActionProps } from '../../../block-provider/hooks'; import { useCreateActionProps } from '../../../block-provider/hooks';
import { FormActiveFieldsProvider } from '../../../block-provider/hooks/useFormActiveFields'; import { FormActiveFieldsProvider } from '../../../block-provider/hooks/useFormActiveFields';
import { TableSelectorParamsProvider } from '../../../block-provider/TableSelectorProvider'; import { TableSelectorParamsProvider } from '../../../block-provider/TableSelectorProvider';
import { CollectionProvider_deprecated } from '../../../collection-manager'; import { CollectionProvider_deprecated } from '../../../collection-manager';
import { CollectionRecordProvider, useCollectionManager, useCollectionRecord } from '../../../data-source'; import { CollectionRecordProvider, useCollectionRecord } from '../../../data-source';
import { markRecordAsNew } from '../../../data-source/collection-record/isNewRecord'; import { markRecordAsNew } from '../../../data-source/collection-record/isNewRecord';
import { FlagProvider } from '../../../flag-provider'; import { FlagProvider } from '../../../flag-provider';
import { useCompile } from '../../hooks'; import { useCompile } from '../../hooks';
@ -36,7 +33,7 @@ import { ActionContextProvider } from '../action';
import { Table } from '../table-v2/Table'; import { Table } from '../table-v2/Table';
import { useAssociationFieldContext, useFieldNames } from './hooks'; import { useAssociationFieldContext, useFieldNames } from './hooks';
import { useTableSelectorProps } from './InternalPicker'; import { useTableSelectorProps } from './InternalPicker';
import { InternalTabs } from './SubTabs/InternamTabs'; import { InternalTabs } from './SubTabs/InternalTabs';
import { getLabelFormatValue, useLabelUiSchema } from './util'; import { getLabelFormatValue, useLabelUiSchema } from './util';
const useStyles = createStyles(({ css }) => { const useStyles = createStyles(({ css }) => {

View File

@ -1,9 +1,10 @@
import React, { useEffect, useState } from 'react'; import React, { useState } from 'react';
import { ArrayField, observer, onFieldChange, Schema, useFieldSchema, useFormEffects } from '@tachybase/schema'; import { ArrayField, observer, onFieldChange, Schema, useFieldSchema, useFormEffects } from '@tachybase/schema';
import { fuzzysearch } from '@tachybase/utils/client';
import { CheckOutlined, CheckSquareOutlined, SearchOutlined } from '@ant-design/icons'; import { CheckOutlined, SearchOutlined } from '@ant-design/icons';
import { useAsyncEffect, useDeepCompareEffect } from 'ahooks'; import { useAsyncEffect, useDeepCompareEffect } from 'ahooks';
import { Badge, Button, Input, Space, Tabs } from 'antd'; import { Button, Input, Space, Tabs } from 'antd';
import flat from 'flat'; import flat from 'flat';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -23,27 +24,24 @@ export const InternalTabs = observer((props) => {
const quickAddParentField = fieldSchema['parent']['x-component-props']['quickAddParentCollection']; const quickAddParentField = fieldSchema['parent']['x-component-props']['quickAddParentCollection'];
const [options, setOptions] = useState([]); const [options, setOptions] = useState([]);
const [defOptions, setDefOptions] = useState([]); const [defOptions, setDefOptions] = useState([]);
const { field, options: collectionField } = useAssociationFieldContext<ArrayField>(); const { field } = useAssociationFieldContext<ArrayField>();
const { t } = useTranslation(); const { t } = useTranslation();
const [changeForm, setChangeForm] = useState<any>(); const [changeForm, setChangeForm] = useState<any>();
const tableSchema = fieldSchema.reduceProperties((buf, schema) => { const quickAddSchema = fieldSchema.reduceProperties((buf, schema) => {
const found = schema.reduceProperties( const found = schema.reduceProperties((buf, schema) => {
(buf, schema) => { if (schema.name === quickAddField) {
if (schema.name === quickAddField) { return schema;
return schema; }
} return buf;
return buf; });
}, if (found) {
new Schema({ name: 'not-found' }),
);
if (found.name === quickAddField) {
return found; return found;
} }
return buf; return buf;
}, new Schema({})); }, new Schema({}));
const tabparams = tableSchema['x-component-props']?.['service']?.['params']; const tabparams = quickAddSchema['x-component-props']?.['service']?.['params'];
const [fieldServiceFilter, setFieldServiceFilter] = useFieldServiceFilter(tabparams?.filter); const [fieldServiceFilter] = useFieldServiceFilter(tabparams?.filter);
const formFilter = flat(tabparams?.filter || {}); const formFilter = flat(tabparams?.filter || {});
const formFieldFilter = Object.values(formFilter) const formFieldFilter = Object.values(formFilter)
?.map((item) => { ?.map((item) => {
@ -69,9 +67,9 @@ export const InternalTabs = observer((props) => {
itemParams['parentOptions'] = parentItem?.data?.data; itemParams['parentOptions'] = parentItem?.data?.data;
} }
if (tableSchema && fieldInterface === 'm2o') { if (quickAddSchema && fieldInterface === 'm2o') {
itemParams['service'] = tableSchema['x-component-props'].service; itemParams['service'] = quickAddSchema['x-component-props'].service;
itemParams['collectionName'] = cm.getCollection(tableSchema['x-collection-field']).name; itemParams['collectionName'] = cm.getCollection(quickAddSchema['x-collection-field']).name;
const childrenItem = await api.request({ const childrenItem = await api.request({
url: itemParams['collectionName'] + ':list', url: itemParams['collectionName'] + ':list',
params, params,
@ -119,8 +117,8 @@ export const InternalTabs = observer((props) => {
} }
setOptions(optionsItem); setOptions(optionsItem);
setDefOptions(optionsItem); setDefOptions(optionsItem);
} else if (tableSchema) { } else if (quickAddSchema) {
const fieldItem = cm.getCollectionField(tableSchema['x-collection-field']); const fieldItem = cm.getCollectionField(quickAddSchema['x-collection-field']);
if (!fieldItem?.uiSchema || !fieldItem?.uiSchema.enum) return; if (!fieldItem?.uiSchema || !fieldItem?.uiSchema.enum) return;
fieldItem.uiSchema.enum.forEach((item) => { fieldItem.uiSchema.enum.forEach((item) => {
optionsItem.push(item); optionsItem.push(item);
@ -156,8 +154,8 @@ export const InternalTabs = observer((props) => {
if (quickAddParentField && quickAddParentField.value !== 'none') { if (quickAddParentField && quickAddParentField.value !== 'none') {
const filterOption = defOptions const filterOption = defOptions
.map((item) => { .map((item) => {
const filterItem = item?.childrenItems?.filter((childrenItem) => childrenItem?.label?.includes(value)); const filterItem = item?.childrenItems?.filter((childrenItem) => fuzzysearch(value, childrenItem?.label));
if (item?.label?.includes(value) || filterItem.length) { if (fuzzysearch(value, item?.label) || filterItem.length) {
return { return {
...item, ...item,
childrenItems: filterItem.length ? filterItem : item.childrenItems, childrenItems: filterItem.length ? filterItem : item.childrenItems,
@ -167,7 +165,7 @@ export const InternalTabs = observer((props) => {
.filter(Boolean); .filter(Boolean);
setOptions(filterOption); setOptions(filterOption);
} else { } else {
const filterOption = defOptions.filter((item) => item?.label?.includes(value)); const filterOption = defOptions.filter((item) => fuzzysearch(value, item?.label));
setOptions(filterOption); setOptions(filterOption);
} }
}; };