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} +