From 6fdf8873491181da9f78bb9a8b50cf1154f83156 Mon Sep 17 00:00:00 2001 From: Sun HaoWei Date: Fri, 5 Nov 2021 13:43:31 +0800 Subject: [PATCH] feat(formily-setters): add config to DataSourceSetter (#121) --- .../DataSourceSetter/DataSettingPanel.tsx | 32 ++++++++++------- .../components/DataSourceSetter/TreePanel.tsx | 34 +++++++++++++------ .../src/components/DataSourceSetter/index.tsx | 28 +++++++++++++-- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/formily/setters/src/components/DataSourceSetter/DataSettingPanel.tsx b/formily/setters/src/components/DataSourceSetter/DataSettingPanel.tsx index 3989b9f5d..b2f5bf26e 100644 --- a/formily/setters/src/components/DataSourceSetter/DataSettingPanel.tsx +++ b/formily/setters/src/components/DataSourceSetter/DataSettingPanel.tsx @@ -2,7 +2,7 @@ import React, { useMemo, Fragment } from 'react' import { Button } from 'antd' import { PlusOutlined } from '@ant-design/icons' import { ArrayItems, Form, Input, FormItem } from '@formily/antd' -import { createForm } from '@formily/core' +import { createForm, Form as FormCore } from '@formily/core' import { observer } from '@formily/reactive-react' import { createSchemaField } from '@formily/react' import { ValueInput } from '@designable/react-settings-form' @@ -23,10 +23,13 @@ const SchemaField = createSchemaField({ export interface IDataSettingPanelProps { treeDataSource: ITreeDataSource + allowExtendOption?: boolean + effects?: (form: FormCore) => void } export const DataSettingPanel: React.FC = observer( (props) => { + const { allowExtendOption, effects } = props const prefix = usePrefix('data-source-setter') const form = useMemo(() => { let values: any @@ -37,6 +40,7 @@ export const DataSettingPanel: React.FC = observer( }) return createForm({ values, + effects: effects, }) }, [ props.treeDataSource.selectedKey, @@ -63,17 +67,19 @@ export const DataSettingPanel: React.FC = observer( } extra={ - + allowExtendOption ? ( + + ) : null } />
@@ -89,6 +95,7 @@ export const DataSettingPanel: React.FC = observer( } x-decorator="FormItem" + x-disabled={!allowExtendOption} name="label" x-component="Input" /> @@ -102,6 +109,7 @@ export const DataSettingPanel: React.FC = observer( /> { + if (dropPosition === 0) { + return false + } + return true +} + export interface ITreePanelProps { treeDataSource: ITreeDataSource + allowTree: boolean + defaultOptionValue: { + label: string + value: any + }[] } export const TreePanel: React.FC = observer((props) => { @@ -77,18 +89,19 @@ export const TreePanel: React.FC = observer((props) => { onClick={() => { const uuid = uid() const dataSource = props.treeDataSource.dataSource + const initialKeyValuePairs = props.defaultOptionValue || [ + { + label: 'label', + value: `${GlobalRegistry.getDesignerMessage( + `SettingComponents.DataSourceSetter.item` + )} ${dataSource.length + 1}`, + }, + { label: 'value', value: uuid }, + ] props.treeDataSource.dataSource = dataSource.concat({ key: uuid, duplicateKey: uuid, - map: [ - { - label: 'label', - value: `${GlobalRegistry.getDesignerMessage( - `SettingComponents.DataSourceSetter.item` - )} ${dataSource.length + 1}`, - }, - { label: 'value', value: uuid }, - ], + map: initialKeyValuePairs, children: [], }) }} @@ -101,7 +114,8 @@ export const TreePanel: React.FC = observer((props) => {
void value: IDataSourceItem[] + allowTree?: boolean + allowExtendOption?: boolean + defaultOptionValue?: { + label: string + value: any + }[] + effects?: (form: Form) => void } + export const DataSourceSetter: React.FC = observer( (props) => { - const { className, value = [], onChange } = props + const { + className, + value = [], + onChange, + allowTree = true, + allowExtendOption = true, + defaultOptionValue, + effects = () => {}, + } = props const theme = useTheme() const prefix = usePrefix('data-source-setter') const [modalVisible, setModalVisible] = useState(false) @@ -58,11 +74,17 @@ export const DataSourceSetter: React.FC = observer( }`} >
- +