feat: the details tab can choose which fields to display

This commit is contained in:
chenos 2021-01-24 13:23:13 +08:00
parent a98f5af183
commit 1be960bebf
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,86 @@
import React, { useState } from 'react'
import { connect } from '@formily/react-schema-renderer'
import moment from 'moment'
import { Select, Table } from 'antd'
import get from 'lodash/get';
import {
mapStyledProps,
mapTextComponent,
compose,
isStr,
isArr
} from '../shared'
import { useRequest } from 'umi';
import api from '@/api-client';
import { Spin } from '@nocobase/client'
import { fields2columns, components } from '@/components/views/SortableTable'
function DraggableTableComponent(props) {
const { value, onChange, disabled, rowKey = 'id', fields = [], resourceName, associatedKey, filter, labelField, valueField = 'id', objectValue, placeholder } = props;
const { data = [], loading = true, mutate } = useRequest(() => {
return api.resource(resourceName).list({
associatedKey,
filter,
});
}, {
refreshDeps: [resourceName, associatedKey]
});
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
const onTableChange = (selectedRowKeys: React.ReactText[]) => {
onChange(selectedRowKeys);
}
const tableProps: any = {};
tableProps.rowSelection = {
selectedRowKeys: Array.isArray(value) ? value : data.map(item => item[rowKey]),
onChange: onTableChange,
}
const dataSource = data.filter(item => get(item, ['component', 'showInDetail']));
// const sortIds = get(value, 'sort')||[];
// let values = [];
// sortIds.forEach(id => {
// const item = dataSource.find(item => item[rowKey] === id);
// if (item) {
// values.push(item);
// }
// });
// if (values.length === 0) {
// values = dataSource;
// }
return (
<>
<Table
size={'small'}
dataSource={dataSource}
rowKey={rowKey}
loading={loading}
columns={fields2columns(fields||[])}
pagination={false}
{...tableProps}
// components={components({
// data: {
// list: data,
// },
// mutate: (values) => {
// onChange({
// sort: values.list.map(item => item[rowKey]),
// selectedRowKeys: get(value, 'selectedRowKeys')||data.map(item => item[rowKey]),
// });
// mutate(values.list);
// // setDataSource(values.list);
// console.log('mutate', values);
// },
// rowKey,
// onMoved: async ({resourceKey, target}) => {
// }
// })}
/>
</>
);
}
export const DraggableTable = connect({
getProps: mapStyledProps,
getComponent: mapTextComponent,
})(DraggableTableComponent)
export default DraggableTable

View File

@ -20,6 +20,7 @@ import { SubTable } from './sub-table'
import { Icon } from './icons' import { Icon } from './icons'
import { ColorSelect } from './color-select' import { ColorSelect } from './color-select'
import { Permissions } from './permissions' import { Permissions } from './permissions'
import { DraggableTable } from './draggable-table'
export const setup = () => { export const setup = () => {
registerFormFields({ registerFormFields({
@ -53,6 +54,7 @@ export const setup = () => {
drawerSelect: DrawerSelect, drawerSelect: DrawerSelect,
colorSelect: ColorSelect, colorSelect: ColorSelect,
subTable: SubTable, subTable: SubTable,
draggableTable: DraggableTable,
'permissions.actions': Permissions.Actions, 'permissions.actions': Permissions.Actions,
'permissions.fields': Permissions.Fields, 'permissions.fields': Permissions.Fields,
'permissions.tabs': Permissions.Tabs, 'permissions.tabs': Permissions.Tabs,

View File

@ -16,6 +16,7 @@ configResponsive({
export function Details(props: any) { export function Details(props: any) {
const dom = document.querySelector('body'); const dom = document.querySelector('body');
const responsive = useResponsive(); const responsive = useResponsive();
console.log('Details.props', props)
const { const {
activeTab = {}, activeTab = {},
@ -45,6 +46,7 @@ export function Details(props: any) {
} }
} }
console.log(props); console.log(props);
const { displayFields = [] } = activeTab;
return ( return (
<Card bordered={false}> <Card bordered={false}>
<Actions <Actions
@ -63,6 +65,9 @@ export function Details(props: any) {
{...descriptionsProps} {...descriptionsProps}
column={1}> column={1}>
{fields.map((field: any) => { {fields.map((field: any) => {
if (Array.isArray(displayFields) && displayFields.length && displayFields.indexOf(field.id) === -1) {
return null;
}
return ( return (
<Descriptions.Item label={field.title||field.name}> <Descriptions.Item label={field.title||field.name}>
<Field data={field} viewType={'descriptions'} schema={field} value={get(data, field.name)}/> <Field data={field} viewType={'descriptions'} schema={field} value={get(data, field.name)}/>

View File

@ -74,6 +74,11 @@ export default {
target: "associationField", target: "associationField",
condition: "{{ $self.value === 'association' }}" condition: "{{ $self.value === 'association' }}"
}, },
{
type: "value:visible",
target: "displayFields",
condition: "{{ $self.value === 'details' }}",
},
// { // {
// type: "value:schema", // type: "value:schema",
// target: "association", // target: "association",
@ -84,6 +89,16 @@ export default {
// }, // },
// }, // },
// }, // },
{
type: "value:schema",
target: "displayFields",
condition: "{{ $self.value === 'details' }}",
schema: {
"x-component-props": {
associatedKey: "{{ $form.values && $form.values.associatedKey }}"
},
},
},
{ {
type: "value:schema", type: "value:schema",
target: "associationField", target: "associationField",
@ -123,6 +138,7 @@ export default {
target: 'fields', target: 'fields',
title: '相关数据表', title: '相关数据表',
labelField: 'title', labelField: 'title',
required: true,
// valueField: 'name', // valueField: 'name',
component: { component: {
type: 'remoteSelect', type: 'remoteSelect',
@ -156,6 +172,43 @@ export default {
], ],
}, },
}, },
{
interface: 'json',
type: 'json',
name: 'displayFields',
title: '显示的字段',
labelField: 'title',
// valueField: 'name',
component: {
type: 'draggableTable',
showInDetail: true,
showInForm: true,
'x-component-props': {
resourceName: 'collections.fields',
labelField: 'title',
valueField: 'name',
showInDetail: true,
fields: [
// {
// interface: 'sort',
// name: 'sort',
// title: '排序',
// type: 'sort',
// dataIndex: ['sort'],
// className: 'drag-visible',
// },
{
interface: 'string',
name: 'title',
title: '字段名称',
type: 'string',
className: 'drag-visible',
dataIndex: ['title'],
}
],
},
},
},
{ {
interface: 'string', interface: 'string',
type: 'string', type: 'string',