fix: set default sorting rules

This commit is contained in:
chenos 2022-03-10 13:43:33 +08:00
parent 345080f92e
commit fa16fe6fec
2 changed files with 127 additions and 1 deletions

View File

@ -28,6 +28,28 @@ export const useCancelFilterAction = () => {
}; };
}; };
export const useSortFields = (collectionName: string) => {
const { getCollectionFields, getInterface } = useCollectionManager();
const fields = getCollectionFields(collectionName);
return fields
.filter((field: any) => {
if (!field.interface) {
return false;
}
const fieldInterface = getInterface(field.interface);
if (fieldInterface.sortable) {
return true;
}
return false;
})
.map((field: any) => {
return {
value: field.name,
label: field?.uiSchema?.title || field.name,
};
});
};
export const useCollectionFilterOptions = (collectionName: string) => { export const useCollectionFilterOptions = (collectionName: string) => {
const { getCollectionFields, getInterface } = useCollectionManager(); const { getCollectionFields, getInterface } = useCollectionManager();
const fields = getCollectionFields(collectionName); const fields = getCollectionFields(collectionName);

View File

@ -1,8 +1,9 @@
import { ArrayItems } from '@formily/antd';
import { ISchema, useField, useFieldSchema } from '@formily/react'; import { ISchema, useField, useFieldSchema } from '@formily/react';
import React from 'react'; import React from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useCollection, useResourceActionContext } from '../../../collection-manager'; import { useCollection, useResourceActionContext } from '../../../collection-manager';
import { useCollectionFilterOptions } from '../../../collection-manager/action-hooks'; import { useCollectionFilterOptions, useSortFields } from '../../../collection-manager/action-hooks';
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
import { useDesignable } from '../../hooks'; import { useDesignable } from '../../hooks';
@ -11,11 +12,23 @@ export const TableVoidDesigner = () => {
const field = useField(); const field = useField();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const dataSource = useCollectionFilterOptions(name); const dataSource = useCollectionFilterOptions(name);
const sortFields = useSortFields(name);
const ctx = useResourceActionContext(); const ctx = useResourceActionContext();
const { t } = useTranslation(); const { t } = useTranslation();
const { dn } = useDesignable(); const { dn } = useDesignable();
const defaultFilter = fieldSchema?.['x-decorator-props']?.request?.params?.filter || {}; const defaultFilter = fieldSchema?.['x-decorator-props']?.request?.params?.filter || {};
const defaultSort = fieldSchema?.['x-decorator-props']?.request?.params?.sort || []; const defaultSort = fieldSchema?.['x-decorator-props']?.request?.params?.sort || [];
const sort = defaultSort?.map((item: string) => {
return item.startsWith('-')
? {
field: item.substring(1),
direction: 'desc',
}
: {
field: item,
direction: 'asc',
};
});
return ( return (
<GeneralSchemaDesigner title={title || name}> <GeneralSchemaDesigner title={title || name}>
<SchemaSettings.SwitchItem <SchemaSettings.SwitchItem
@ -64,6 +77,97 @@ export const TableVoidDesigner = () => {
}); });
}} }}
/> />
<SchemaSettings.ModalItem
title={t('Set default sorting rules')}
components={{ ArrayItems }}
schema={
{
type: 'object',
title: t('Set default sorting rules'),
properties: {
sort: {
type: 'array',
default: sort,
'x-component': 'ArrayItems',
'x-decorator': 'FormItem',
items: {
type: 'object',
properties: {
space: {
type: 'void',
'x-component': 'Space',
properties: {
sort: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.SortHandle',
},
field: {
type: 'string',
enum: sortFields,
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: {
width: 260,
},
},
},
direction: {
type: 'string',
'x-decorator': 'FormItem',
'x-component': 'Radio.Group',
'x-component-props': {
optionType: 'button',
},
enum: [
{
label: t('ASC'),
value: 'asc',
},
{
label: t('DESC'),
value: 'desc',
},
],
},
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.Remove',
},
},
},
},
},
properties: {
add: {
type: 'void',
title: t('Add sort field'),
'x-component': 'ArrayItems.Addition',
},
},
},
},
} as ISchema
}
onSubmit={({ sort }) => {
const sortArr = sort.map((item) => {
return item.direction === 'desc' ? `-${item.field}` : item.field;
});
const params = field.decoratorProps.request.params || {};
params.sort = sortArr;
field.decoratorProps.request.params = params;
fieldSchema['x-decorator-props']['request']['params'] = params;
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],
'x-decorator-props': fieldSchema['x-decorator-props'],
},
});
ctx.run({ ...ctx.params?.[0], sort: sortArr });
}}
/>
<SchemaSettings.SelectItem <SchemaSettings.SelectItem
title={'每页显示'} title={'每页显示'}
value={field.decoratorProps.request.params?.pageSize || 20} value={field.decoratorProps.request.params?.pageSize || 20}