feat: improve code

This commit is contained in:
chenos 2021-08-26 22:17:49 +08:00
parent 0ef730d7e4
commit 5aad2bae76
7 changed files with 128 additions and 97 deletions

View File

@ -93,7 +93,10 @@ export const useAsyncDataSource = (service: any) => (field: any) => {
); );
}; };
export const loadChinaRegionDataSource = async (field) => { export const loadChinaRegionDataSource = async (field: ArrayField) => {
if (field.readPretty) {
return [];
}
const maxLevel = field.componentProps.maxLevel || 3; const maxLevel = field.componentProps.maxLevel || 3;
const resource = Resource.make('china_regions'); const resource = Resource.make('china_regions');
const { data } = await resource.list({ const { data } = await resource.list({
@ -141,6 +144,11 @@ export const loadChinaRegionData = (
}); });
}; };
const useChinaRegionFieldValue = (field: ArrayField) => {
field.value = field?.value?.sort((a, b) => a.level - b.level);
console.log('useChinaRegionFieldValue', field.value);
}
export const SchemaField = createSchemaField({ export const SchemaField = createSchemaField({
scope: { scope: {
Table, Table,
@ -148,6 +156,7 @@ export const SchemaField = createSchemaField({
Kanban, Kanban,
useAsyncDataSource, useAsyncDataSource,
ChinaRegion: { ChinaRegion: {
useFieldValue: useChinaRegionFieldValue,
loadData: loadChinaRegionData, loadData: loadChinaRegionData,
loadDataSource: loadChinaRegionDataSource, loadDataSource: loadChinaRegionDataSource,
}, },

View File

@ -27,7 +27,10 @@ export const chinaRegion: FieldOptions = {
children: 'children', children: 'children',
}, },
}, },
'x-reactions': ['{{useAsyncDataSource(ChinaRegion.loadDataSource)}}'], 'x-reactions': [
'{{ChinaRegion.useFieldValue}}',
'{{useAsyncDataSource(ChinaRegion.loadDataSource)}}',
],
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-designable-bar': 'Cascader.DesignableBar', 'x-designable-bar': 'Cascader.DesignableBar',
}, },

View File

@ -17,7 +17,12 @@ export const createdBy: FieldOptions = {
type: 'object', type: 'object',
title: '创建人', title: '创建人',
'x-component': 'Select.Drawer', 'x-component': 'Select.Drawer',
'x-component-props': {}, 'x-component-props': {
fieldNames: {
value: 'id',
label: 'nickname',
},
},
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-read-pretty': true, 'x-read-pretty': true,
'x-designable-bar': 'Select.Drawer.DesignableBar', 'x-designable-bar': 'Select.Drawer.DesignableBar',

View File

@ -16,7 +16,12 @@ export const updatedBy: FieldOptions = {
type: 'object', type: 'object',
title: '最后修改人', title: '最后修改人',
'x-component': 'Select.Drawer', 'x-component': 'Select.Drawer',
'x-component-props': {}, 'x-component-props': {
fieldNames: {
value: 'id',
label: 'nickname',
},
},
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-read-pretty': true, 'x-read-pretty': true,
'x-designable-bar': 'Select.Drawer.DesignableBar', 'x-designable-bar': 'Select.Drawer.DesignableBar',

View File

@ -198,7 +198,11 @@ Select.Object = connect(
), ),
mapReadPretty( mapReadPretty(
observer((props: any) => { observer((props: any) => {
const { value, fieldNames = { label: 'label', color: 'color' }, ...others } = props; const {
value,
fieldNames = { label: 'label', color: 'color' },
...others
} = props;
if (!value) { if (!value) {
return null; return null;
} }
@ -286,9 +290,9 @@ Select.Drawer = connect(
} }
}; };
const selectedKeys = toArr(optionValue).map(item => item.value); const selectedKeys = toArr(optionValue).map((item) => item.value);
console.log({ selectedKeys }) console.log({ selectedKeys });
return ( return (
<> <>
@ -365,7 +369,7 @@ Select.Drawer = connect(
const { fieldNames = { label: 'label' }, ...others } = props; const { fieldNames = { label: 'label' }, ...others } = props;
const value = field.value || field.initialValue; const value = field.value || field.initialValue;
const schema = useFieldSchema(); const schema = useFieldSchema();
console.log({ field, value }); console.log({ fieldNames, field, value });
if (!value) { if (!value) {
return null; return null;
} }
@ -375,13 +379,14 @@ Select.Drawer = connect(
{values.map((data, index) => { {values.map((data, index) => {
return ( return (
<OptionTagContext.Provider value={{ index, data, fieldNames }}> <OptionTagContext.Provider value={{ index, data, fieldNames }}>
<RecursionField {data[fieldNames.label]}
{/* <RecursionField
schema={schema} schema={schema}
onlyRenderProperties onlyRenderProperties
filterProperties={(s) => { filterProperties={(s) => {
return s['x-component'] === 'Select.OptionTag'; return s['x-component'] === 'Select.OptionTag';
}} }}
/> /> */}
</OptionTagContext.Provider> </OptionTagContext.Provider>
); );
})} })}

View File

@ -7,7 +7,7 @@ import {
useForm, useForm,
} from '@formily/react'; } from '@formily/react';
import { Modal, Pagination, Popover, Table as AntdTable } from 'antd'; import { Modal, Pagination, Popover, Table as AntdTable } from 'antd';
import { cloneDeep, findIndex, forIn, range, set } from 'lodash'; import { cloneDeep, cloneDeepWith, findIndex, forIn, range, set } from 'lodash';
import React, { Fragment, useEffect, useState } from 'react'; import React, { Fragment, useEffect, useState } from 'react';
import { useContext } from 'react'; import { useContext } from 'react';
import { createContext } from 'react'; import { createContext } from 'react';
@ -310,54 +310,26 @@ export function isColumnComponent(component: string) {
return ['Table.Operation', 'Table.Column'].includes(component); return ['Table.Operation', 'Table.Column'].includes(component);
} }
const useTableOperation = () => { const useCollectionFields = (schema: Schema) => {
const { const columns = schema.reduceProperties((columns, current) => {
field, if (isColumn(current)) {
schema, if (current['x-hidden']) {
props: { rowKey }, return columns;
} = useTable();
const findActionDropdown = (schema: Schema) => {
return schema.reduceProperties((s, current) => {
if (current['x-component'] === 'Action.Dropdown') {
return current;
} }
const action = findActionDropdown(current); if (current['x-display'] && current['x-display'] !== 'visible') {
if (action) { return columns;
return action;
} }
return s; return [...columns, current];
}, new Schema({}));
};
const operationSchema = schema.reduceProperties((s, current) => {
if (isOperationColumn(current)) {
return current;
} }
return s; return [...columns];
}, new Schema({})); }, []);
const dropdownSchema = findActionDropdown(operationSchema);
const columnProps = operationSchema?.['x-component-props'] || {}; return columns
return { .map((column) => {
title: <Table.Operation path={getSchemaPath(dropdownSchema)} />, const columnProps = column['x-component-props'] || {};
dataIndex: 'operation', return columnProps.fieldName;
...columnProps, })
render: (_: any, record: any) => { .filter(Boolean);
const index = findIndex(
field.value,
(item) => item[rowKey] === record[rowKey],
);
return (
<div className={'nb-table-column'}>
<TableRowContext.Provider value={{ index, record }}>
<RecursionField
schema={operationSchema}
name={index}
onlyRenderProperties
/>
</TableRowContext.Provider>
</div>
);
},
};
}; };
const useTableColumns = () => { const useTableColumns = () => {
@ -370,8 +342,6 @@ const useTableColumns = () => {
const { getField } = useCollectionContext(); const { getField } = useCollectionContext();
// const operation = useTableOperation();
const columnSchemas = schema.reduceProperties((columns, current) => { const columnSchemas = schema.reduceProperties((columns, current) => {
if (isColumn(current)) { if (isColumn(current)) {
if (current['x-hidden']) { if (current['x-hidden']) {
@ -723,6 +693,8 @@ const TableProvider = (props: any) => {
const { resource } = useResource(); const { resource } = useResource();
const { sortableField } = useCollectionContext(); const { sortableField } = useCollectionContext();
const dragSort = props.dragSort; const dragSort = props.dragSort;
const collectionFields = useCollectionFields(schema);
console.log({ collectionFields });
const getDefaultParams = () => { const getDefaultParams = () => {
const defaultParams = { ...pagination }; const defaultParams = { ...pagination };
if (dragSort) { if (dragSort) {
@ -730,9 +702,10 @@ const TableProvider = (props: any) => {
} else { } else {
defaultParams['sort'] = (props.defaultSort || []).join(','); defaultParams['sort'] = (props.defaultSort || []).join(',');
} }
if (props.defaultAppends) { defaultParams['defaultAppends'] = [
defaultParams['defaultAppends'] = props.defaultAppends; ...(props.defaultAppends || []),
} ...collectionFields,
];
if (props.defaultFilter) { if (props.defaultFilter) {
defaultParams['defaultFilter'] = props.defaultFilter; defaultParams['defaultFilter'] = props.defaultFilter;
} }
@ -1850,7 +1823,13 @@ Table.Cell = observer((props: any) => {
if (schema['x-component'] === 'Table.Operation') { if (schema['x-component'] === 'Table.Operation') {
return <Table.Operation.Cell {...props} />; return <Table.Operation.Cell {...props} />;
} }
let uiSchema = collectionField?.uiSchema as Schema;
if (uiSchema?.['x-component'] === 'Upload.Attachment') {
uiSchema = cloneDeepWith(uiSchema);
set(uiSchema, 'x-component-props.size', 'small');
}
return ( return (
<div className={`field-interface-${collectionField?.interface}`}>
<RecursionField <RecursionField
schema={ schema={
!collectionField !collectionField
@ -1859,7 +1838,7 @@ Table.Cell = observer((props: any) => {
type: 'void', type: 'void',
properties: { properties: {
[collectionField.name]: { [collectionField.name]: {
...collectionField.uiSchema, ...uiSchema,
title: undefined, title: undefined,
'x-read-pretty': true, 'x-read-pretty': true,
'x-decorator-props': { 'x-decorator-props': {
@ -1873,6 +1852,7 @@ Table.Cell = observer((props: any) => {
name={ctx.index} name={ctx.index}
onlyRenderProperties onlyRenderProperties
/> />
</div>
); );
}); });
@ -2279,14 +2259,32 @@ Table.useResource = ({ onSuccess, manual = true }) => {
resourceName: collection?.name || props.collectionName, resourceName: collection?.name || props.collectionName,
resourceKey: ctx.record[props.rowKey], resourceKey: ctx.record[props.rowKey],
}); });
const { schema } = useDesignable();
const fieldFields = (schema: Schema) => {
const names = [];
schema.reduceProperties((buf, current) => {
if (current['x-component'] === 'Form.Field') {
const fieldName = current['x-component-props']?.['fieldName'];
if (fieldName) {
buf.push(fieldName);
}
} else {
const fieldNames = fieldFields(current);
buf.push(...fieldNames);
}
return buf;
}, names);
return names;
};
console.log( console.log(
'collection?.name || props.collectionName', 'collection?.name || props.collectionName',
collection?.name || props.collectionName, collection?.name || props.collectionName,
// fieldFields(schema),
); );
const service = useRequest( const service = useRequest(
(params?: any) => { (params?: any) => {
console.log('Table.useResource', params); console.log('Table.useResource', params);
return resource.get(params); return resource.get({ ...params, appends: fieldFields(schema) });
}, },
{ {
formatResult: (result) => result?.data, formatResult: (result) => result?.data,

View File

@ -231,3 +231,9 @@ td.nb-table-operation {
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
} }
.field-interface-attachment {
margin-top: -13px;
margin-bottom: -13px;
}