diff --git a/packages/client/src/components/admin-layout/datatable.tsx b/packages/client/src/components/admin-layout/datatable.tsx index c74b9a9f7..2902a304e 100644 --- a/packages/client/src/components/admin-layout/datatable.tsx +++ b/packages/client/src/components/admin-layout/datatable.tsx @@ -3,8 +3,10 @@ import React from 'react'; import { FormItem } from '@formily/antd'; import { action } from '@formily/reactive'; import { useCollectionsContext } from '../../constate/Collections'; +import { Field } from '@formily/core'; export const useAsyncDataSource = (service: any) => (field: any) => { + console.log('loadCollectionFields'); field.loading = true; service(field).then( action.bound((data: any) => { @@ -24,6 +26,22 @@ export default () => { })); }; + const loadCollectionFields = async (field: Field) => { + const target = field.query('....target').get('value'); + const f = field.query('....target').take(); + console.log('loadCollectionFields', f, field); + const collection = collections?.find((item) => item.name === target); + if (!collection) { + return []; + } + return collection?.generalFields + ?.filter((item) => item?.uiSchema?.title) + ?.map((item) => ({ + label: item?.uiSchema?.title || item.name, + value: item.name, + })); + }; + const schema = { type: 'array', name: 'collections', @@ -56,7 +74,7 @@ export default () => { }; return ( diff --git a/packages/client/src/constate/Collections.ts b/packages/client/src/constate/Collections.ts index bb952e049..512e2c468 100644 --- a/packages/client/src/constate/Collections.ts +++ b/packages/client/src/constate/Collections.ts @@ -6,9 +6,14 @@ const [CollectionsProvider, useCollectionsContext] = constate(() => { formatResult: (result) => result?.data, }); return { - ...result, collections: result.data || [], findCollection(name) { + ...result, collections: result.data || [], + findCollection(name) { return result?.data?.find((item) => item.name === name); - } + }, + getFieldsByCollection(collectionName) { + const collection = result?.data?.find((item) => item.name === collectionName); + return collection?.generalFields; + }, }; }); diff --git a/packages/client/src/schemas/add-new/index.tsx b/packages/client/src/schemas/add-new/index.tsx index 654def2cc..6135815a0 100644 --- a/packages/client/src/schemas/add-new/index.tsx +++ b/packages/client/src/schemas/add-new/index.tsx @@ -1437,12 +1437,13 @@ AddNew.FormItem = observer((props: any) => { default: [], 'x-component-props': { rowKey: 'id', - defaultSelectedRowKeys: - '{{ Select.useSelectedRowKeys() }}', + useSelectedRowKeys: + '{{ Select.useSelectedRowKeys }}', onSelect: '{{ Select.useSelect() }}', + useRowSelection: '{{ Select.useRowSelection }}', collectionName: field.target, // dragSort: true, - showIndex: true, + // showIndex: true, refreshRequestOnChange: true, pagination: { pageSize: 10, diff --git a/packages/client/src/schemas/form/Field.DesignableBar.tsx b/packages/client/src/schemas/form/Field.DesignableBar.tsx index 933b92917..04f21463c 100644 --- a/packages/client/src/schemas/form/Field.DesignableBar.tsx +++ b/packages/client/src/schemas/form/Field.DesignableBar.tsx @@ -20,7 +20,7 @@ import { updateSchema, } from '../'; import get from 'lodash/get'; -import { Button, Dropdown, Menu, Modal, Space, Switch } from 'antd'; +import { Button, Dropdown, Menu, Modal, Select, Space, Switch } from 'antd'; import { MenuOutlined, DragOutlined } from '@ant-design/icons'; import cls from 'classnames'; import { FormDialog, FormLayout } from '@formily/antd'; @@ -33,19 +33,25 @@ import { useEffect } from 'react'; import { uid } from '@formily/shared'; import { getSchemaPath } from '../../components/schema-renderer'; import { RandomNameContext } from '.'; -import { useCollectionContext, useDisplayedMapContext } from '../../constate'; +import { + useCollectionContext, + useCollectionsContext, + useDisplayedMapContext, +} from '../../constate'; import SwitchMenuItem from '../../components/SwitchMenuItem'; import { DragHandle } from '../../components/Sortable'; +import { set } from 'lodash'; export const FieldDesignableBar = observer((props) => { const field = useField(); - const { schema, deepRemove } = useDesignable(); + const { schema, deepRemove, refresh } = useDesignable(); const [visible, setVisible] = useState(false); const { dragRef } = useContext(DraggableBlockContext); const randomName = useContext(RandomNameContext); const displayed = useDisplayedMapContext(); const fieldName = schema['x-component-props']?.['fieldName']; const { getField } = useCollectionContext(); + const { getFieldsByCollection } = useCollectionsContext(); const collectionField = getField(fieldName); @@ -124,6 +130,59 @@ export const FieldDesignableBar = observer((props) => { > 自定义字段名称 + {collectionField.interface === 'linkTo' && ( + +
+ 标签字段{' '} + { + set( + schema['x-component-props'], + 'fieldNames.label', + value, + ); + await updateSchema({ + key: schema['key'], + 'x-component-props': { + fieldNames: { + label: value, + }, + }, + }); + refreshTable(); + // await service.refresh(); + }} + bordered={false} + size={'small'} + style={{ marginLeft: 16, minWidth: 120 }} + options={getFieldsByCollection(collectionField.target) + .filter((f) => f?.uiSchema?.title) + .map((field) => ({ + label: field?.uiSchema?.title || field.name, + value: field.name, + }))} + /> +
+
+ )} {