fix(client/table-selector-provider): make data range config effective (#960)

* fix(client/table-selector-provider): make data range config effective

* fix(client/table-selector): fix when save data range config filter lose
This commit is contained in:
lyf-coder 2022-10-31 11:28:40 +08:00 committed by chenos
parent 9a81b1b8ee
commit b9910cdc1f
2 changed files with 70 additions and 39 deletions

View File

@ -9,7 +9,7 @@ import { useFormBlockContext } from './FormBlockProvider';
export const TableSelectorContext = createContext<any>({}); export const TableSelectorContext = createContext<any>({});
const InternalTableSelectorProvider = (props) => { const InternalTableSelectorProvider = (props) => {
const { params, rowKey } = props; const { params, rowKey, extraFilter } = props;
const field = useField(); const field = useField();
const { resource, service } = useBlockRequestContext(); const { resource, service } = useBlockRequestContext();
// if (service.loading) { // if (service.loading) {
@ -22,6 +22,7 @@ const InternalTableSelectorProvider = (props) => {
service, service,
resource, resource,
params, params,
extraFilter,
rowKey, rowKey,
}} }}
> >
@ -41,12 +42,14 @@ const useAssociationNames = (collection) => {
const recursiveParent = (schema: Schema, component) => { const recursiveParent = (schema: Schema, component) => {
return schema['x-component'] === component return schema['x-component'] === component
? schema ? schema
: (schema.parent ? recursiveParent(schema.parent, component) : null); : schema.parent
} ? recursiveParent(schema.parent, component)
: null;
};
export const TableSelectorProvider = (props) => { export const TableSelectorProvider = (props) => {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const ctx = useFormBlockContext() const ctx = useFormBlockContext();
const { getCollectionJoinField, getCollectionFields } = useCollectionManager(); const { getCollectionJoinField, getCollectionFields } = useCollectionManager();
const record = useRecord(); const record = useRecord();
@ -63,57 +66,74 @@ export const TableSelectorProvider = (props) => {
if (!Object.keys(params).includes('appends')) { if (!Object.keys(params).includes('appends')) {
params['appends'] = appends; params['appends'] = appends;
} }
let extraFilter;
if (collectionField) { if (collectionField) {
if (['oho', 'o2m'].includes(collectionField.interface)) { if (['oho', 'o2m'].includes(collectionField.interface)) {
if (record?.[collectionField.sourceKey]) { if (record?.[collectionField.sourceKey]) {
params['filter'] = { extraFilter = {
$or: [{ $or: [
{
[collectionField.foreignKey]: { [collectionField.foreignKey]: {
$is: null, $is: null,
} },
}, { },
{
[collectionField.foreignKey]: { [collectionField.foreignKey]: {
$eq: record?.[collectionField.sourceKey], $eq: record?.[collectionField.sourceKey],
} },
}] },
} ],
};
} else { } else {
params['filter'] = { extraFilter = {
[collectionField.foreignKey]: { [collectionField.foreignKey]: {
$is: null, $is: null,
} },
} };
} }
} }
if (['obo'].includes(collectionField.interface)) { if (['obo'].includes(collectionField.interface)) {
const fields = getCollectionFields(collectionField.target); const fields = getCollectionFields(collectionField.target);
const targetField = fields.find(f => f.foreignKey && f.foreignKey === collectionField.foreignKey); const targetField = fields.find((f) => f.foreignKey && f.foreignKey === collectionField.foreignKey);
if (targetField) { if (targetField) {
if (record?.[collectionField.foreignKey]) { if (record?.[collectionField.foreignKey]) {
params['filter'] = { extraFilter = {
$or: [{ $or: [
{
[`${targetField.name}.${targetField.foreignKey}`]: { [`${targetField.name}.${targetField.foreignKey}`]: {
$is: null, $is: null,
} },
}, { },
{
[`${targetField.name}.${targetField.foreignKey}`]: { [`${targetField.name}.${targetField.foreignKey}`]: {
$eq: record?.[collectionField.foreignKey], $eq: record?.[collectionField.foreignKey],
} },
}] },
} ],
};
} else { } else {
params['filter'] = { extraFilter = {
[`${targetField.name}.${targetField.foreignKey}`]: { [`${targetField.name}.${targetField.foreignKey}`]: {
$is: null, $is: null,
},
};
} }
} }
} }
} }
if (extraFilter) {
if (params?.filter) {
params['filter'] = {
$and: [extraFilter, params['filter']],
};
} else {
params['filter'] = extraFilter;
} }
} }
return ( return (
<BlockProvider {...props} params={params}> <BlockProvider {...props} params={params}>
<InternalTableSelectorProvider {...props} params={params} /> <InternalTableSelectorProvider {...props} params={params} extraFilter={extraFilter} />
</BlockProvider> </BlockProvider>
); );
}; };

View File

@ -8,6 +8,7 @@ import { useCollectionFilterOptions, useSortFields } from '../../../collection-m
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
import { useSchemaTemplate } from '../../../schema-templates'; import { useSchemaTemplate } from '../../../schema-templates';
import { useDesignable } from '../../hooks'; import { useDesignable } from '../../hooks';
import { cloneDeep } from 'lodash';
export const TableSelectorDesigner = () => { export const TableSelectorDesigner = () => {
const { name, title } = useCollection(); const { name, title } = useCollection();
@ -15,7 +16,7 @@ export const TableSelectorDesigner = () => {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const dataSource = useCollectionFilterOptions(name); const dataSource = useCollectionFilterOptions(name);
const sortFields = useSortFields(name); const sortFields = useSortFields(name);
const { service } = useTableSelectorContext(); const { service, extraFilter } = useTableSelectorContext();
const { t } = useTranslation(); const { t } = useTranslation();
const { dn } = useDesignable(); const { dn } = useDesignable();
const defaultFilter = fieldSchema?.['x-decorator-props']?.params?.filter || {}; const defaultFilter = fieldSchema?.['x-decorator-props']?.params?.filter || {};
@ -57,7 +58,17 @@ export const TableSelectorDesigner = () => {
params.filter = filter; params.filter = filter;
field.decoratorProps.params = params; field.decoratorProps.params = params;
fieldSchema['x-decorator-props']['params'] = params; fieldSchema['x-decorator-props']['params'] = params;
service.run({ ...service.params?.[0], filter, page: 1 }); let serviceFilter = cloneDeep(filter);
if (extraFilter) {
if (serviceFilter) {
serviceFilter = {
$and: [extraFilter, serviceFilter],
};
} else {
serviceFilter = extraFilter;
}
}
service.run({ ...service.params?.[0], filter: serviceFilter, page: 1 });
dn.emit('patch', { dn.emit('patch', {
schema: { schema: {
['x-uid']: fieldSchema['x-uid'], ['x-uid']: fieldSchema['x-uid'],