fix(FilterDynamicComponent): avoid crashing (#2566)

This commit is contained in:
被雨水过滤的空气-Rain 2023-08-30 23:26:51 +08:00 committed by GitHub
parent 3bb38b16f3
commit d56cc66ef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { useCompile } from '../../schema-component'; import { useCompile } from '../../schema-component';
import { useFilterOptions } from '../../schema-component/antd/filter'; import { useGetFilterOptions } from '../../schema-component/antd/filter';
import { useValues } from '../../schema-component/antd/filter/useValues'; import { useValues } from '../../schema-component/antd/filter/useValues';
import { Variable } from '../../schema-component/antd/variable'; import { Variable } from '../../schema-component/antd/variable';
@ -8,31 +8,36 @@ interface GetOptionsParams {
schema: any; schema: any;
operator: string; operator: string;
maxDepth: number; maxDepth: number;
getFilterOptions: (collectionName: string) => any[];
count?: number; count?: number;
} }
const useOptions = (collectionName: string, { schema, operator, maxDepth, count = 1 }: GetOptionsParams) => { const getOptions = (
collectionName: string,
{ schema, operator, maxDepth, getFilterOptions, count = 1 }: GetOptionsParams,
) => {
if (count > maxDepth) { if (count > maxDepth) {
return []; return [];
} }
const result = useFilterOptions(collectionName).map((option) => { const result = getFilterOptions(collectionName).map((option) => {
if ((option.type !== 'belongsTo' && option.type !== 'hasOne') || !option.target) { if ((option.type !== 'belongsTo' && option.type !== 'hasOne') || !option.target) {
return { return {
key: option.name, key: option.name,
value: option.name, value: option.name,
label: option.title, label: option.title,
// TODO: 现在是通过组件的名称来过滤能够被选择的选项,这样的坏处是不够精确,后续可以优化 // TODO: 现在是通过组件的名称来过滤能够被选择的选项,这样的坏处是不够精确,后续可以优化
disabled: schema?.['x-component'] !== option.schema['x-component'], disabled: schema?.['x-component'] !== option.schema?.['x-component'],
}; };
} }
const children = const children =
useOptions(option.target, { getOptions(option.target, {
schema, schema,
operator, operator,
maxDepth, maxDepth,
count: count + 1, count: count + 1,
getFilterOptions,
}) || []; }) || [];
return { return {
@ -48,7 +53,8 @@ const useOptions = (collectionName: string, { schema, operator, maxDepth, count
}; };
const useUserVariable = ({ schema, operator }) => { const useUserVariable = ({ schema, operator }) => {
const options = useOptions('users', { schema, operator, maxDepth: 1 }) || []; const getFilterOptions = useGetFilterOptions();
const options = getOptions('users', { schema, operator, maxDepth: 1, getFilterOptions }) || [];
return { return {
label: `{{t("Current user")}}`, label: `{{t("Current user")}}`,