-
-
Notifications
You must be signed in to change notification settings - Fork 633
/
SettingsForm.tsx
105 lines (100 loc) 路 2.89 KB
/
SettingsForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useMemo } from 'react'
import { createForm } from '@formily/core'
import { Form } from '@formily/antd'
import { observer } from '@formily/react'
import { requestIdle, cancelIdle } from '@designable/shared'
import {
usePrefix,
useSelected,
useOperation,
useCurrentNode,
useWorkbench,
IconWidget,
NodePathWidget,
} from '@designable/react'
import { SchemaField } from './SchemaField'
import { ISettingFormProps } from './types'
import { SettingsFormContext } from './shared/context'
import { useLocales, useSnapshot } from './effects'
import { Empty } from 'antd'
import cls from 'classnames'
import './styles.less'
const GlobalState = {
idleRequest: null,
}
export const SettingsForm: React.FC<ISettingFormProps> = observer(
(props) => {
const workbench = useWorkbench()
const currentWorkspace =
workbench?.activeWorkspace || workbench?.currentWorkspace
const currentWorkspaceId = currentWorkspace?.id
const operation = useOperation(currentWorkspaceId)
const node = useCurrentNode(currentWorkspaceId)
const selected = useSelected(currentWorkspaceId)
const prefix = usePrefix('settings-form')
const schema = node?.designerProps?.propsSchema
const form = useMemo(() => {
return createForm({
values: node?.props,
effects(form) {
useLocales(schema?.['$namespace'])
useSnapshot(operation)
props.effects?.(form)
},
})
}, [node, node?.props, schema, operation])
const isEmpty = !(
node &&
node.designerProps?.propsSchema &&
selected.length === 1
)
const render = () => {
if (!isEmpty) {
return (
<div
className={cls(prefix, props.className)}
style={props.style}
key={node.id}
>
<SettingsFormContext.Provider value={props}>
<Form
form={form}
colon={false}
labelWidth={120}
labelAlign="left"
wrapperAlign="right"
feedbackLayout="none"
tooltipLayout="text"
>
<SchemaField
schema={schema}
components={props.components}
scope={props.scope}
/>
</Form>
</SettingsFormContext.Provider>
</div>
)
}
return (
<div className={prefix + '-empty'}>
<Empty />
</div>
)
}
return (
<IconWidget.Provider tooltip>
<div className={prefix + '-wrapper'}>
{!isEmpty && <NodePathWidget workspaceId={currentWorkspaceId} />}
<div className={prefix + '-content'}>{render()}</div>
</div>
</IconWidget.Provider>
)
},
{
scheduler: (update) => {
cancelIdle(GlobalState.idleRequest)
GlobalState.idleRequest = requestIdle(update)
},
}
)