feat: column sortable and form item pattern (#518)

This commit is contained in:
金昶 2022-06-20 16:48:52 +08:00 committed by GitHub
parent 2b0c7384f2
commit 553088e839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 114 additions and 8 deletions

View File

@ -81,7 +81,9 @@ export const useTableBlockContext = () => {
export const useTableBlockProps = () => { export const useTableBlockProps = () => {
const field = useField<ArrayField>(); const field = useField<ArrayField>();
const fieldSchema = useFieldSchema();
const ctx = useTableBlockContext(); const ctx = useTableBlockContext();
const globalSort = fieldSchema.parent?.['x-decorator-props']?.['params']?.['sort'];
useEffect(() => { useEffect(() => {
if (!ctx?.service?.loading) { if (!ctx?.service?.loading) {
field.value = ctx?.service?.data?.data; field.value = ctx?.service?.data?.data;
@ -116,8 +118,10 @@ export const useTableBlockProps = () => {
}); });
ctx.service.refresh(); ctx.service.refresh();
}, },
onChange({ current, pageSize }) { onChange({ current, pageSize }, filters, sorter) {
ctx.service.run({ ...ctx.service.params?.[0], page: current, pageSize }); let sort = sorter.order ? (sorter.order === `ascend` ? [sorter.field] : [`-${sorter.field}`]) : globalSort || null;
ctx.service.run({ ...ctx.service.params?.[0], page: current, pageSize, sort });
}, },
}; };
}; };

View File

@ -311,11 +311,11 @@ export const useUpdateActionProps = () => {
...assignedValues, ...assignedValues,
}, },
}); });
actionField.data.loading = false; actionField.data.loading = false;
__parent?.service?.refresh?.(); if (!(resource instanceof TableFieldResource)) {
if (!(resource instanceof TableFieldResource)) {
__parent?.__parent?.service?.refresh?.(); __parent?.__parent?.service?.refresh?.();
} }
__parent?.service?.refresh?.();
setVisible?.(false); setVisible?.(false);
if (!onSuccess?.successMessage) { if (!onSuccess?.successMessage) {
return; return;

View File

@ -183,6 +183,11 @@ export default {
"Subform mode": "子表单模式", "Subform mode": "子表单模式",
"Edit block title": "编辑区块标题", "Edit block title": "编辑区块标题",
"Block title": "区块标题", "Block title": "区块标题",
"Pattern": "模式",
"Editable": "可编辑",
"Readonly": "只读(禁止编辑)",
"Easy-reading": "只读(阅读模式)",
"Sortable": "可排序",
"Add filter": "添加筛选条件", "Add filter": "添加筛选条件",
"Add filter group": "添加筛选分组", "Add filter group": "添加筛选分组",
"is": "等于", "is": "等于",

View File

@ -56,6 +56,13 @@ FormItem.Designer = () => {
value: field?.name, value: field?.name,
label: compile(field?.uiSchema?.title) || field?.name, label: compile(field?.uiSchema?.title) || field?.name,
})); }));
let readOnlyMode = 'editable';
if (fieldSchema['x-read-pretty'] === true) {
readOnlyMode = 'read-pretty';
}
if (fieldSchema['x-component-props']?.['readOnly'] === true) {
readOnlyMode = 'readonly';
}
return ( return (
<GeneralSchemaDesigner> <GeneralSchemaDesigner>
{collectionField && ( {collectionField && (
@ -172,6 +179,64 @@ FormItem.Designer = () => {
}} }}
/> />
)} )}
{!field.readPretty && (
<SchemaSettings.SelectItem
title={t('Pattern')}
options={
[{ label: t('Editable'), value: 'editable' }, { label: t('Readonly'), value: 'readonly' }, { label: t('Easy-reading'), value: 'read-pretty' }]
}
value={readOnlyMode}
onChange={(v) => {
const schema = {
['x-uid']: fieldSchema['x-uid'],
};
switch(v) {
case 'readonly': {
fieldSchema['x-read-pretty'] = false;
schema['x-read-pretty'] = false;
fieldSchema['x-component-props'] = {
...fieldSchema['x-component-props'],
readOnly: true,
}
schema['x-component-props'] = fieldSchema['x-component-props'];
field.readPretty = false;
field.componentProps.readOnly = true;
break;
}
case 'read-pretty': {
fieldSchema['x-read-pretty'] = true;
schema['x-read-pretty'] = true;
fieldSchema['x-component-props'] = {
...fieldSchema['x-component-props'],
readOnly: false,
}
schema['x-component-props'] = fieldSchema['x-component-props'];
field.readPretty = true;
field.componentProps.readOnly = false;
break;
}
default: {
fieldSchema['x-read-pretty'] = false;
schema['x-read-pretty'] = false;
fieldSchema['x-component-props'] = {
...fieldSchema['x-component-props'],
readOnly: false,
}
schema['x-component-props'] = fieldSchema['x-component-props'];
field.readPretty = false;
field.componentProps.readOnly = false;
break;
}
}
dn.emit('patch', {
schema
});
dn.refresh();
}}
/>
)}
{collectionField?.target && ( {collectionField?.target && (
<SchemaSettings.SelectItem <SchemaSettings.SelectItem
title={t('Title field')} title={t('Title field')}

View File

@ -65,7 +65,26 @@ export const TableColumnDesigner = (props) => {
dn.refresh(); dn.refresh();
}} }}
/> />
{['linkTo', 'm2m', 'm2o', 'obo', 'oho'].includes(collectionField?.interface) && ( <SchemaSettings.SwitchItem
title={t('Sortable')}
checked={field.componentProps.sorter}
onChange={(v) => {
const schema: ISchema = {
['x-uid']: columnSchema['x-uid'],
};
columnSchema['x-component-props'] = {
...columnSchema['x-component-props'],
sorter: v
}
schema['x-component-props'] = columnSchema['x-component-props'];
field.componentProps.sorter = v;
dn.emit('patch', {
schema
});
dn.refresh();
}}
/>
{['linkTo', 'm2m', 'm2o', 'o2m', 'obo', 'oho'].includes(collectionField?.interface) && (
<SchemaSettings.SelectItem <SchemaSettings.SelectItem
title={t('Title field')} title={t('Title field')}
options={options} options={options}

View File

@ -1,7 +1,7 @@
import { MenuOutlined } from '@ant-design/icons'; import { MenuOutlined } from '@ant-design/icons';
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { ArrayField } from '@formily/core'; import { ArrayField } from '@formily/core';
import { observer, RecursionField, Schema, useField, useFieldSchema } from '@formily/react'; import { ISchema, observer, RecursionField, Schema, useField, useFieldSchema } from '@formily/react';
import { Table as AntdTable, TableColumnProps } from 'antd'; import { Table as AntdTable, TableColumnProps } from 'antd';
import { default as classNames, default as cls } from 'classnames'; import { default as classNames, default as cls } from 'classnames';
import React from 'react'; import React from 'react';
@ -14,11 +14,16 @@ const isColumnComponent = (schema: Schema) => {
return schema['x-component']?.endsWith('.Column') > -1; return schema['x-component']?.endsWith('.Column') > -1;
}; };
const isCollectionFieldComponent = (schema: ISchema) => {
return schema['x-component'] === 'CollectionField';
}
const useTableColumns = () => { const useTableColumns = () => {
const start = Date.now(); const start = Date.now();
const field = useField<ArrayField>(); const field = useField<ArrayField>();
const schema = useFieldSchema(); const schema = useFieldSchema();
const { exists, render } = useSchemaInitializer(schema['x-initializer']); const { exists, render } = useSchemaInitializer(schema['x-initializer']);
// console.log('useTableColumns', exists);
const columns = schema const columns = schema
.reduceProperties((buf, s) => { .reduceProperties((buf, s) => {
if (isColumnComponent(s)) { if (isColumnComponent(s)) {
@ -26,10 +31,18 @@ const useTableColumns = () => {
} }
}, []) }, [])
.map((s: Schema) => { .map((s: Schema) => {
const collectionFields = s
.reduceProperties((buf, s) => {
if (isCollectionFieldComponent(s)) {
return buf.concat([s]);
}
}, []);
const dataIndex = collectionFields?.length > 0 ? collectionFields[0].name : s.name;
return { return {
title: <RecursionField name={s.name} schema={s} onlyRenderSelf />, title: <RecursionField name={s.name} schema={s} onlyRenderSelf />,
dataIndex: s.name, dataIndex,
key: s.name, key: s.name,
sorter: s['x-component-props']?.['sorter'],
// width: 300, // width: 300,
render: (v, record) => { render: (v, record) => {
const index = field.value?.indexOf(record); const index = field.value?.indexOf(record);