feat: improve Table.Operation

This commit is contained in:
chenos 2021-08-18 11:30:24 +08:00
parent 90f8f721b7
commit 3e5e9c194c
3 changed files with 424 additions and 455 deletions

View File

@ -92,14 +92,13 @@ const useActionPermissionResource = ({ onSuccess }) => {
console.log('actionPermissions', data); console.log('actionPermissions', data);
onSuccess({ onSuccess({
actionPermissions: data.map((permission) => { actionPermissions: data.map((permission) => {
const item: any = {} const item: any = {};
Object.keys(permission).forEach(key => { Object.keys(permission).forEach((key) => {
if (isValid(permission[key])) { if (isValid(permission[key])) {
item[key] = permission[key]; item[key] = permission[key];
} }
}); });
item.fields = item.fields = permission?.fields.map((field) => field.key) || [];
permission?.fields.map((field) => field.key) || [];
return item; return item;
}), }),
}); });
@ -162,34 +161,51 @@ const collectionSchema: ISchema = {
collectionName: 'collections', collectionName: 'collections',
}, },
properties: { properties: {
[uid()]: { column1: {
type: 'void',
title: '数据表名称',
'x-component': 'Table.Column',
properties: {
title: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column2: {
type: 'void',
title: '数据表标识',
'x-component': 'Table.Column',
properties: {
name: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column3: {
type: 'void', type: 'void',
title: '操作', title: '操作',
'x-component': 'Table.Operation', 'x-component': 'Table.Column',
'x-component-props': { 'x-component-props': {
className: 'nb-table-operation', width: 60,
}, },
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
'x-component': 'Action', 'x-component': 'Action.Group',
'x-component-props': { 'x-component-props': {
icon: 'EllipsisOutlined', type: 'link',
}, },
properties: {
[uid()]: {
type: 'void',
'x-component': 'Action.Dropdown',
'x-component-props': {},
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '配置', title: '配置',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': { 'x-component-props': {
style: { type: 'link',
minWidth: 150,
},
}, },
'x-action-type': 'view', 'x-action-type': 'view',
properties: { properties: {
@ -218,32 +234,6 @@ const collectionSchema: ISchema = {
}, },
}, },
}, },
},
column1: {
type: 'void',
title: '数据表名称',
'x-component': 'Table.Column',
properties: {
title: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column2: {
type: 'void',
title: '数据表标识',
'x-component': 'Table.Column',
properties: {
name: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
},
}; };
const menuSchema: ISchema = { const menuSchema: ISchema = {
@ -342,34 +332,51 @@ const schema: ISchema = {
}, },
}, },
}, },
column1: {
type: 'void',
title: '权限名称',
'x-component': 'Table.Column',
properties: {
title: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column2: {
type: 'void',
title: '权限标识',
'x-component': 'Table.Column',
properties: {
name: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '操作', title: '操作',
'x-component': 'Table.Operation', 'x-component': 'Table.Column',
'x-component-props': { 'x-component-props': {
className: 'nb-table-operation', width: 160,
}, },
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
'x-component': 'Action', 'x-component': 'Action.Group',
'x-component-props': { 'x-component-props': {
icon: 'EllipsisOutlined', type: 'link',
}, },
properties: {
[uid()]: {
type: 'void',
'x-component': 'Action.Dropdown',
'x-component-props': {},
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '配置', title: '配置',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': { 'x-component-props': {
style: { type: 'link',
minWidth: 150,
},
}, },
'x-action-type': 'view', 'x-action-type': 'view',
properties: { properties: {
@ -416,7 +423,10 @@ const schema: ISchema = {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '编辑', title: '编辑',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': {
type: 'link',
},
'x-action-type': 'update', 'x-action-type': 'update',
properties: { properties: {
[uid()]: { [uid()]: {
@ -444,10 +454,11 @@ const schema: ISchema = {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '删除', title: '删除',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'destroy', 'x-action-type': 'destroy',
'x-component-props': { 'x-component-props': {
type: 'link',
useAction: '{{ Table.useTableDestroyAction }}', useAction: '{{ Table.useTableDestroyAction }}',
}, },
}, },
@ -457,32 +468,6 @@ const schema: ISchema = {
}, },
}, },
}, },
column1: {
type: 'void',
title: '权限名称',
'x-component': 'Table.Column',
properties: {
title: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column2: {
type: 'void',
title: '权限标识',
'x-component': 'Table.Column',
properties: {
name: {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
},
},
}, },
}, },
}, },

View File

@ -196,7 +196,7 @@ function generateCardItemSchema(component) {
type: 'void', type: 'void',
title: '新增数据', title: '新增数据',
'x-decorator': 'Form', 'x-decorator': 'Form',
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
useOkAction: '{{ Table.useTableCreateAction }}', useOkAction: '{{ Table.useTableCreateAction }}',
}, },
@ -217,52 +217,32 @@ function generateCardItemSchema(component) {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '操作', title: '操作',
'x-component': 'Table.Operation', 'x-component': 'Table.Column',
'x-component-props': { 'x-component-props': {},
className: 'nb-table-operation',
},
'x-designable-bar': 'Table.Operation.DesignableBar', 'x-designable-bar': 'Table.Operation.DesignableBar',
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
'x-component': 'Action', 'x-component': 'Action.Group',
'x-component-props': { 'x-component-props': {
icon: 'EllipsisOutlined', type: 'link',
}, },
properties: { properties: {
[uid()]: {
type: 'void',
'x-component': 'Action.Dropdown',
'x-component-props': {},
properties: {
// [uid()]: {
// type: 'void',
// title: '操作 1',
// 'x-component': 'Menu.Action',
// 'x-component-props': {
// style: {
// minWidth: 150,
// },
// disabled: true,
// },
// },
[uid()]: { [uid()]: {
type: 'void', type: 'void',
name: 'action1', name: 'action1',
title: '查看', title: '查看',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': { 'x-component-props': {
style: { type: 'link',
minWidth: 150,
},
}, },
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'view', 'x-action-type': 'view',
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '查看', title: '查看数据',
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
bodyStyle: { bodyStyle: {
background: '#f0f2f5', background: '#f0f2f5',
@ -278,8 +258,7 @@ function generateCardItemSchema(component) {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '详情', title: '详情',
'x-designable-bar': 'x-designable-bar': 'Tabs.TabPane.DesignableBar',
'Tabs.TabPane.DesignableBar',
'x-component': 'Tabs.TabPane', 'x-component': 'Tabs.TabPane',
'x-component-props': {}, 'x-component-props': {},
properties: { properties: {
@ -301,7 +280,10 @@ function generateCardItemSchema(component) {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '编辑', title: '编辑',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': {
type: 'link',
},
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'update', 'x-action-type': 'update',
properties: { properties: {
@ -313,7 +295,7 @@ function generateCardItemSchema(component) {
useResource: '{{ Table.useResource }}', useResource: '{{ Table.useResource }}',
useValues: '{{ Table.useTableRowRecord }}', useValues: '{{ Table.useTableRowRecord }}',
}, },
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
useOkAction: '{{ Table.useTableUpdateAction }}', useOkAction: '{{ Table.useTableUpdateAction }}',
}, },
@ -332,10 +314,11 @@ function generateCardItemSchema(component) {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '删除', title: '删除',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'destroy', 'x-action-type': 'destroy',
'x-component-props': { 'x-component-props': {
type: 'link',
useAction: '{{ Table.useTableDestroyAction }}', useAction: '{{ Table.useTableDestroyAction }}',
}, },
}, },
@ -345,8 +328,6 @@ function generateCardItemSchema(component) {
}, },
}, },
}, },
},
},
Form: { Form: {
type: 'void', type: 'void',
name: uid(), name: uid(),
@ -471,34 +452,81 @@ function generateCardItemSchema(component) {
}, },
}, },
}, },
column1: {
type: 'void',
title: '操作时间',
'x-component': 'Table.Column',
properties: {
created_at: {
type: 'string',
'x-component': 'DatePicker',
'x-read-pretty': true,
'x-component-props': {
format: 'YYYY-MM-DD HH:mm:ss',
},
},
},
},
column2: {
type: 'void',
title: '操作用户',
'x-component': 'Table.Column',
properties: {
'user.nickname': {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column3: {
type: 'void',
title: '所属数据表',
'x-component': 'Table.Column',
properties: {
'collection.title': {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column4: {
type: 'void',
title: '操作类型',
'x-component': 'Table.Column',
properties: {
type: {
type: 'string',
'x-component': 'Select',
'x-read-pretty': true,
enum: [
{ label: '新增', value: 'create', color: 'green' },
{ label: '更新', value: 'update', color: 'blue' },
{ label: '删除', value: 'destroy', color: 'red' },
],
},
},
},
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '操作', title: '操作',
'x-component': 'Table.Operation', 'x-component': 'Table.Column',
'x-component-props': { 'x-component-props': {
className: 'nb-table-operation', width: 60,
align: 'center',
}, },
properties: {
[uid()]: {
type: 'void',
'x-component': 'Action',
'x-component-props': {
icon: 'EllipsisOutlined',
},
properties: {
[uid()]: {
type: 'void',
'x-component': 'Action.Dropdown',
'x-component-props': {},
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
name: 'action1', name: 'action1',
title: '查看', title: '查看',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': { 'x-component-props': {
type: 'link',
style: { style: {
minWidth: 150, padding: '0',
height: 'auto',
}, },
}, },
'x-action-type': 'view', 'x-action-type': 'view',
@ -509,8 +537,7 @@ function generateCardItemSchema(component) {
'x-read-pretty': true, 'x-read-pretty': true,
'x-decorator': 'Form', 'x-decorator': 'Form',
'x-decorator-props': { 'x-decorator-props': {
useResource: useResource: '{{ Table.useActionLogDetailsResource }}',
'{{ Table.useActionLogDetailsResource }}',
}, },
'x-component': 'Action.Drawer', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
@ -642,66 +669,6 @@ function generateCardItemSchema(component) {
}, },
}, },
}, },
},
},
column1: {
type: 'void',
title: '操作时间',
'x-component': 'Table.Column',
properties: {
created_at: {
type: 'string',
'x-component': 'DatePicker',
'x-read-pretty': true,
'x-component-props': {
format: 'YYYY-MM-DD HH:mm:ss',
},
},
},
},
column2: {
type: 'void',
title: '操作用户',
'x-component': 'Table.Column',
properties: {
'user.nickname': {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column3: {
type: 'void',
title: '所属数据表',
'x-component': 'Table.Column',
properties: {
'collection.title': {
type: 'string',
'x-component': 'Input',
'x-read-pretty': true,
},
},
},
column4: {
type: 'void',
title: '操作类型',
'x-component': 'Table.Column',
properties: {
type: {
type: 'string',
'x-component': 'Select',
'x-read-pretty': true,
enum: [
{ label: '新增', value: 'create', color: 'green' },
{ label: '更新', value: 'update', color: 'blue' },
{ label: '删除', value: 'destroy', color: 'red' },
],
},
},
},
},
},
}; };
return defaults[component]; return defaults[component];
} }
@ -935,7 +902,11 @@ AddNew.CardItem = observer((props: any) => {
{ghost ? ( {ghost ? (
<PlusOutlined /> <PlusOutlined />
) : ( ) : (
<Button type={'dashed'} className={'designable-btn designable-btn-dash'} icon={<PlusOutlined />}> <Button
type={'dashed'}
className={'designable-btn designable-btn-dash'}
icon={<PlusOutlined />}
>
</Button> </Button>
)} )}
@ -1122,7 +1093,11 @@ AddNew.FormItem = observer((props: any) => {
{ghost ? ( {ghost ? (
<PlusOutlined /> <PlusOutlined />
) : ( ) : (
<Button type={'dashed'} className={'designable-btn designable-btn-dash'} icon={<SettingOutlined />}> <Button
type={'dashed'}
className={'designable-btn designable-btn-dash'}
icon={<SettingOutlined />}
>
</Button> </Button>
)} )}
@ -1326,7 +1301,12 @@ AddNew.PaneItem = observer((props: any) => {
{ghost ? ( {ghost ? (
<PlusOutlined /> <PlusOutlined />
) : ( ) : (
<Button block className={'designable-btn designable-btn-dash'} type={'dashed'} icon={<PlusOutlined />}> <Button
block
className={'designable-btn designable-btn-dash'}
type={'dashed'}
icon={<PlusOutlined />}
>
</Button> </Button>
)} )}

View File

@ -370,7 +370,7 @@ const useTableColumns = () => {
const { getField } = useCollectionContext(); const { getField } = useCollectionContext();
const operation = useTableOperation(); // const operation = useTableOperation();
const columnSchemas = schema.reduceProperties((columns, current) => { const columnSchemas = schema.reduceProperties((columns, current) => {
if (isColumn(current)) { if (isColumn(current)) {
@ -379,7 +379,7 @@ const useTableColumns = () => {
return [...columns]; return [...columns];
}, []); }, []);
const columns: any[] = [operation].concat( const columns: any[] = [].concat(
columnSchemas.map((column: Schema) => { columnSchemas.map((column: Schema) => {
const columnProps = column['x-component-props'] || {}; const columnProps = column['x-component-props'] || {};
const collectionField = getField(columnProps.fieldName); const collectionField = getField(columnProps.fieldName);
@ -905,7 +905,7 @@ function generateActionSchema(type) {
type: 'void', type: 'void',
title: '新增数据', title: '新增数据',
'x-decorator': 'Form', 'x-decorator': 'Form',
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
useOkAction: '{{ Table.useTableCreateAction }}', useOkAction: '{{ Table.useTableCreateAction }}',
}, },
@ -951,14 +951,17 @@ function generateMenuActionSchema(type) {
name: uid(), name: uid(),
type: 'void', type: 'void',
title: '查看', title: '查看',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': {
type: 'link',
},
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'view', 'x-action-type': 'view',
properties: { properties: {
[uid()]: { [uid()]: {
type: 'void', type: 'void',
title: '查看数据', title: '查看数据',
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
bodyStyle: { bodyStyle: {
background: '#f0f2f5', background: '#f0f2f5',
@ -998,7 +1001,10 @@ function generateMenuActionSchema(type) {
name: uid(), name: uid(),
type: 'void', type: 'void',
title: '编辑', title: '编辑',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-component-props': {
type: 'link',
},
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'update', 'x-action-type': 'update',
properties: { properties: {
@ -1010,7 +1016,7 @@ function generateMenuActionSchema(type) {
useResource: '{{ Table.useResource }}', useResource: '{{ Table.useResource }}',
useValues: '{{ Table.useTableRowRecord }}', useValues: '{{ Table.useTableRowRecord }}',
}, },
'x-component': 'Action.Modal', 'x-component': 'Action.Drawer',
'x-component-props': { 'x-component-props': {
useOkAction: '{{ Table.useTableUpdateAction }}', useOkAction: '{{ Table.useTableUpdateAction }}',
}, },
@ -1031,11 +1037,12 @@ function generateMenuActionSchema(type) {
name: uid(), name: uid(),
type: 'void', type: 'void',
title: '删除', title: '删除',
'x-component': 'Menu.Action', 'x-component': 'Action',
'x-designable-bar': 'Table.Action.DesignableBar', 'x-designable-bar': 'Table.Action.DesignableBar',
'x-action-type': 'destroy', 'x-action-type': 'destroy',
'x-component-props': { 'x-component-props': {
useAction: '{{ Table.useTableDestroyAction }}', useAction: '{{ Table.useTableDestroyAction }}',
type: 'link',
}, },
}, },
}; };
@ -1611,15 +1618,12 @@ Table.Operation.Cell = observer((props: any) => {
); );
}); });
Table.Operation.DesignableBar = (props) => { Table.Operation.DesignableBar = () => {
const { designable, schema, remove, refresh, appendChild } = useDesignable( const { schema: columnSchema } = useDesignable();
props.path, const groupSchema = Object.values(columnSchema.properties || {}).shift();
); const groupPath = getSchemaPath(groupSchema);
const { schema, remove, refresh, appendChild } = useDesignable(groupPath);
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
if (!designable || !schema?.parent?.parent['x-designable-bar']) {
return null;
}
console.log('Table.Operation.DesignableBar', schema?.parent?.parent);
const map = new Map(); const map = new Map();
schema.mapProperties((s) => { schema.mapProperties((s) => {
if (!s['x-action-type']) { if (!s['x-action-type']) {
@ -1627,6 +1631,7 @@ Table.Operation.DesignableBar = (props) => {
} }
map.set(s['x-action-type'], s.name); map.set(s['x-action-type'], s.name);
}); });
const path = getSchemaPath(schema);
return ( return (
<div className={cls('designable-bar', { active: visible })}> <div className={cls('designable-bar', { active: visible })}>
<span <span
@ -1661,10 +1666,7 @@ Table.Operation.DesignableBar = (props) => {
const data = appendChild(s); const data = appendChild(s);
await createSchema(data); await createSchema(data);
} else if (map.get(item.name)) { } else if (map.get(item.name)) {
const removed = remove([ const removed = remove([...path, map.get(item.name)]);
...props.path,
map.get(item.name),
]);
await removeSchema(removed); await removeSchema(removed);
} }
}} }}
@ -1672,7 +1674,7 @@ Table.Operation.DesignableBar = (props) => {
))} ))}
</Menu.ItemGroup> </Menu.ItemGroup>
<Menu.Divider /> <Menu.Divider />
<Menu.SubMenu title={'自定义'}> <Menu.SubMenu disabled title={'自定义'}>
<Menu.Item style={{ minWidth: 120 }}></Menu.Item> <Menu.Item style={{ minWidth: 120 }}></Menu.Item>
<Menu.Item></Menu.Item> <Menu.Item></Menu.Item>
<Menu.Item></Menu.Item> <Menu.Item></Menu.Item>
@ -1694,9 +1696,11 @@ Table.Action.DesignableBar = () => {
const { schema, remove, refresh, insertAfter } = useDesignable(); const { schema, remove, refresh, insertAfter } = useDesignable();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const isPopup = Object.keys(schema.properties || {}).length > 0; const isPopup = Object.keys(schema.properties || {}).length > 0;
const popupSchema = Object.values(schema.properties || {}).shift();
const inActionBar = schema.parent['x-component'] === 'Table.ActionBar'; const inActionBar = schema.parent['x-component'] === 'Table.ActionBar';
const displayed = useDisplayedMapContext(); const displayed = useDisplayedMapContext();
const field = useField(); const field = useField();
const popupComponent = popupSchema?.['x-component'] || 'Action.Drawer';
return ( return (
<div className={cls('designable-bar', { active: visible })}> <div className={cls('designable-bar', { active: visible })}>
<span <span
@ -1764,7 +1768,7 @@ Table.Action.DesignableBar = () => {
<Select <Select
bordered={false} bordered={false}
size={'small'} size={'small'}
defaultValue={'Action.Modal'} defaultValue={popupComponent}
onChange={(value) => { onChange={(value) => {
const s = Object.values(schema.properties).shift(); const s = Object.values(schema.properties).shift();
s['x-component'] = value; s['x-component'] = value;