From 0c83c16391397f9b578b0b79723594f29e139029 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 9 Feb 2022 23:37:12 +0800 Subject: [PATCH] feat(client): add settings form --- packages/client/src/index.tsx | 1 + .../client/src/settings-form/SettingsForm.tsx | 269 ++++++++++++++++++ .../client/src/settings-form/demos/demo1.tsx | 133 +++++++++ packages/client/src/settings-form/index.md | 4 +- packages/client/src/settings-form/index.ts | 1 + 5 files changed, 407 insertions(+), 1 deletion(-) create mode 100644 packages/client/src/settings-form/SettingsForm.tsx create mode 100644 packages/client/src/settings-form/demos/demo1.tsx create mode 100644 packages/client/src/settings-form/index.ts diff --git a/packages/client/src/index.tsx b/packages/client/src/index.tsx index 8a9fa425ab..15d18496c9 100644 --- a/packages/client/src/index.tsx +++ b/packages/client/src/index.tsx @@ -16,6 +16,7 @@ export * from './record-provider'; export * from './route-switch'; export * from './schema-component'; export * from './schema-initializer'; +export * from './settings-form'; export * from './system-settings'; export * from './user'; diff --git a/packages/client/src/settings-form/SettingsForm.tsx b/packages/client/src/settings-form/SettingsForm.tsx new file mode 100644 index 0000000000..65bc3bf4c0 --- /dev/null +++ b/packages/client/src/settings-form/SettingsForm.tsx @@ -0,0 +1,269 @@ +import { FormButtonGroup, FormDialog, FormDrawer, FormItem, FormLayout, Reset, Submit } from '@formily/antd'; +import { createForm, Field, ObjectField, onFormValuesChange } from '@formily/core'; +import { + FieldContext, + FormContext, + observer, + RecursionField, + Schema, + SchemaOptionsContext, + useField, + useFieldSchema, + useForm +} from '@formily/react'; +import { Dropdown, Menu, Modal, Select, Switch } from 'antd'; +import React, { createContext, useContext, useMemo, useState } from 'react'; +import { SchemaComponentOptions, useAttach, useDesignable } from '..'; + +export interface SettingsFormContextProps { + field?: Field; + fieldSchema?: Schema; + dropdownVisible?: boolean; + setDropdownVisible?: (v: boolean) => void; + dn?: any; +} + +export const SettingsFormContext = createContext(null); + +export const useSettingsFormContext = () => { + return useContext(SettingsFormContext); +}; + +export const SettingsForm: any = observer((props: any) => { + const dn = useDesignable(); + const field = useField(); + const fieldSchema = useFieldSchema(); + const [dropdownVisible, setDropdownVisible] = useState(false); + const settingsFormSchema = useMemo(() => new Schema(props.schema), []); + const form = useMemo( + () => + createForm({ + initialValues: fieldSchema.toJSON(), + effects(form) { + onFormValuesChange((form) => { + dn.patch(form.values); + console.log('form.values', form.values); + }); + }, + }), + [], + ); + const f = useAttach(form.createVoidField({ ...field.props, basePath: '' })); + return ( + + + + + + setDropdownVisible(visible)} + overlayStyle={{ width: 200 }} + overlay={ + + {settingsFormSchema.mapProperties((s, key) => { + return ; + })} + + } + > + 配置 + + + + + + + ); +}); + +SettingsForm.Divider = () => { + return ; +}; + +SettingsForm.Remove = (props) => { + const field = useField(); + const { dn, setDropdownVisible } = useSettingsFormContext(); + return ( + { + setDropdownVisible(false); + Modal.confirm({ + title: 'Are you sure delete this task?', + content: 'Some descriptions', + okText: 'Yes', + okType: 'danger', + cancelText: 'No', + ...props.confirm, + onOk() { + dn.remove(); + console.log('OK'); + }, + onCancel() { + console.log('Cancel'); + }, + }); + }} + > + {field.title} + + ); +}; + +SettingsForm.Switch = observer(() => { + const field = useField(); + return ( + { + field.value = !field.value; + }} + > +
+ {field.title} +
+
+ ); +}); + +SettingsForm.Select = observer((props) => { + const field = useField(); + const [open, setOpen] = useState(false); + return ( + !open && setOpen(true)}> +
+ {field.title} +