fix(data-templates): fix filter is empty (#2193)
This commit is contained in:
parent
817646d68d
commit
f44c5f3b4a
@ -2,7 +2,7 @@ import { LoadingOutlined } from '@ant-design/icons';
|
|||||||
import { connect, mapProps, mapReadPretty, useField, useFieldSchema, useForm } from '@formily/react';
|
import { connect, mapProps, mapReadPretty, useField, useFieldSchema, useForm } from '@formily/react';
|
||||||
import { Divider, SelectProps, Tag } from 'antd';
|
import { Divider, SelectProps, Tag } from 'antd';
|
||||||
import flat from 'flat';
|
import flat from 'flat';
|
||||||
import { uniqBy } from 'lodash';
|
import _, { uniqBy } from 'lodash';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { ResourceActionOptions, useRequest } from '../../../api-client';
|
import { ResourceActionOptions, useRequest } from '../../../api-client';
|
||||||
@ -11,7 +11,7 @@ import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
|||||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||||
import { getInnermostKeyAndValue } from '../../common/utils/uitls';
|
import { getInnermostKeyAndValue } from '../../common/utils/uitls';
|
||||||
import { useCompile } from '../../hooks';
|
import { useCompile } from '../../hooks';
|
||||||
import { defaultFieldNames, Select } from '../select';
|
import { Select, defaultFieldNames } from '../select';
|
||||||
import { ReadPretty } from './ReadPretty';
|
import { ReadPretty } from './ReadPretty';
|
||||||
import { extractFilterfield, extractValuesByPattern, generatePattern, parseVariables } from './utils';
|
import { extractFilterfield, extractValuesByPattern, generatePattern, parseVariables } from './utils';
|
||||||
const EMPTY = 'N/A';
|
const EMPTY = 'N/A';
|
||||||
@ -122,58 +122,69 @@ const InternalRemoteSelect = connect(
|
|||||||
},
|
},
|
||||||
[targetField?.uiSchema, fieldNames],
|
[targetField?.uiSchema, fieldNames],
|
||||||
);
|
);
|
||||||
const parseFilter = (rules) => {
|
const parseFilter = useCallback(
|
||||||
if (!rules) {
|
(rules) => {
|
||||||
return undefined;
|
if (!rules) {
|
||||||
}
|
return undefined;
|
||||||
if (typeof rules === 'string') {
|
|
||||||
return rules;
|
|
||||||
}
|
|
||||||
const type = Object.keys(rules)[0] || '$and';
|
|
||||||
const conditions = rules[type];
|
|
||||||
const results = [];
|
|
||||||
conditions?.forEach((c) => {
|
|
||||||
const jsonlogic = getInnermostKeyAndValue(c);
|
|
||||||
const regex = /{{(.*?)}}/;
|
|
||||||
const matches = jsonlogic.value?.match?.(regex);
|
|
||||||
if (!matches || (!matches[1].includes('$form') && !matches[1].includes('$iteration'))) {
|
|
||||||
results.push(c);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
const associationfield = extractFilterfield(matches[1]);
|
if (typeof rules === 'string') {
|
||||||
const filterCollectionField = getCollectionJoinField(`${ctx.props.collection}.${associationfield}`);
|
return rules;
|
||||||
if (['o2m', 'm2m'].includes(filterCollectionField?.interface)) {
|
}
|
||||||
// 对多子表单
|
const type = Object.keys(rules)[0] || '$and';
|
||||||
const pattern = generatePattern(matches?.[1], associationfield);
|
const conditions = rules[type];
|
||||||
const parseValue: any = extractValuesByPattern(flat(form.values), pattern);
|
const results = [];
|
||||||
const filters = parseValue.map((v) => {
|
conditions?.forEach((c) => {
|
||||||
return JSON.parse(JSON.stringify(c).replace(jsonlogic.value, v));
|
const jsonlogic = getInnermostKeyAndValue(c);
|
||||||
});
|
const regex = /{{(.*?)}}/;
|
||||||
results.push({ $or: filters });
|
const matches = jsonlogic.value?.match?.(regex);
|
||||||
} else {
|
if (!matches || (!matches[1].includes('$form') && !matches[1].includes('$iteration'))) {
|
||||||
const variablesCtx = { $form: form.values, $iteration: form.values };
|
results.push(c);
|
||||||
let str = matches?.[1];
|
return;
|
||||||
if (str.includes('$iteration')) {
|
|
||||||
const path = field.path.segments.concat([]);
|
|
||||||
path.pop();
|
|
||||||
str = str.replace('$iteration.', `$iteration.${path.join('.')}.`);
|
|
||||||
}
|
}
|
||||||
const parseValue = parseVariables(str, variablesCtx);
|
const associationfield = extractFilterfield(matches[1]);
|
||||||
if (Array.isArray(parseValue)) {
|
const filterCollectionField = getCollectionJoinField(`${ctx.props.collection}.${associationfield}`);
|
||||||
|
if (['o2m', 'm2m'].includes(filterCollectionField?.interface)) {
|
||||||
|
// 对多子表单
|
||||||
|
const pattern = generatePattern(matches?.[1], associationfield);
|
||||||
|
const parseValue: any = extractValuesByPattern(flat(form.values), pattern);
|
||||||
const filters = parseValue.map((v) => {
|
const filters = parseValue.map((v) => {
|
||||||
return JSON.parse(JSON.stringify(c).replace(jsonlogic.value, v));
|
return JSON.parse(JSON.stringify(c).replace(jsonlogic.value, v));
|
||||||
});
|
});
|
||||||
results.push({ $or: filters });
|
results.push({ $or: filters });
|
||||||
} else {
|
} else {
|
||||||
const filterObj = JSON.parse(
|
const variablesCtx = { $form: form.values, $iteration: form.values };
|
||||||
JSON.stringify(c).replace(jsonlogic.value, str.endsWith('id') ? parseValue ?? 0 : parseValue),
|
let str = matches?.[1];
|
||||||
);
|
if (str.includes('$iteration')) {
|
||||||
results.push(filterObj);
|
const path = field.path.segments.concat([]);
|
||||||
|
path.pop();
|
||||||
|
str = str.replace('$iteration.', `$iteration.${path.join('.')}.`);
|
||||||
|
}
|
||||||
|
const parseValue = parseVariables(str, variablesCtx);
|
||||||
|
if (Array.isArray(parseValue)) {
|
||||||
|
const filters = parseValue.map((v) => {
|
||||||
|
return JSON.parse(JSON.stringify(c).replace(jsonlogic.value, v));
|
||||||
|
});
|
||||||
|
results.push({ $or: filters });
|
||||||
|
} else {
|
||||||
|
const filterObj = JSON.parse(
|
||||||
|
JSON.stringify(c).replace(jsonlogic.value, str.endsWith('id') ? parseValue ?? 0 : parseValue),
|
||||||
|
);
|
||||||
|
results.push(filterObj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
return { [type]: results };
|
||||||
return { [type]: results };
|
},
|
||||||
};
|
[ctx.props?.collection, field.path.segments, form.values, getCollectionJoinField],
|
||||||
|
);
|
||||||
|
|
||||||
|
const filter = useMemo(() => {
|
||||||
|
const filterFromSchema = _.isString(fieldSchema?.['x-component-props']?.service?.params?.filter)
|
||||||
|
? field.componentProps?.service?.params?.filter
|
||||||
|
: fieldSchema?.['x-component-props']?.service?.params?.filter;
|
||||||
|
|
||||||
|
return mergeFilter([parseFilter(filterFromSchema) || service?.params?.filter]);
|
||||||
|
}, [field.componentProps?.service?.params?.filter, fieldSchema, parseFilter, service?.params?.filter]);
|
||||||
|
|
||||||
const { data, run, loading } = useRequest(
|
const { data, run, loading } = useRequest(
|
||||||
{
|
{
|
||||||
@ -183,9 +194,7 @@ const InternalRemoteSelect = connect(
|
|||||||
pageSize: 200,
|
pageSize: 200,
|
||||||
...service?.params,
|
...service?.params,
|
||||||
// search needs
|
// search needs
|
||||||
filter: mergeFilter([
|
filter,
|
||||||
parseFilter(fieldSchema?.['x-component-props']?.service?.params?.filter) || service?.params?.filter,
|
|
||||||
]),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -233,7 +242,7 @@ const InternalRemoteSelect = connect(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
: {},
|
: {},
|
||||||
fieldSchema?.['x-component-props']?.service?.params?.filter || service?.params?.filter,
|
filter,
|
||||||
]),
|
]),
|
||||||
});
|
});
|
||||||
searchData.current = search;
|
searchData.current = search;
|
||||||
|
Loading…
Reference in New Issue
Block a user