1
1
import FormRender , { useForm } from 'form-render' ;
2
- import React , { useContext , useEffect , useState } from 'react' ;
2
+ import React , { useContext , useEffect } from 'react' ;
3
3
import type { ChangeSchemaItem , Dict , SchemaForUI , PropPanelWidgetProps , PropPanelSchema } from '@pdfme/common' ;
4
4
import type { SidebarProps } from '../../../../types' ;
5
5
import { MenuOutlined } from '@ant-design/icons' ;
@@ -14,31 +14,31 @@ import { InternalNamePath, ValidateErrorEntity } from "rc-field-form/es/interfac
14
14
const { Text } = Typography ;
15
15
16
16
type DetailViewProps = Pick < SidebarProps ,
17
- 'size' | 'schemas' | 'pageSize' | 'changeSchemas' | 'activeElements' | 'deselectSchema'
17
+ 'size' | 'schemas' | 'pageSize' | 'changeSchemas' | 'activeElements' | 'deselectSchema'
18
18
> & {
19
19
activeSchema : SchemaForUI ;
20
20
} ;
21
21
22
22
const DetailView = ( props : DetailViewProps ) => {
23
23
const { token } = theme . useToken ( ) ;
24
24
25
- const { size, changeSchemas, deselectSchema, activeSchema, activeElements } = props ;
25
+ const { size, changeSchemas, deselectSchema, activeSchema } = props ;
26
26
const form = useForm ( ) ;
27
27
28
28
const i18n = useContext ( I18nContext ) ;
29
29
const pluginsRegistry = useContext ( PluginsRegistry ) ;
30
30
const options = useContext ( OptionsContext ) ;
31
31
32
- const [ widgets , setWidgets ] = useState < {
33
- [ key : string ] : ( props : PropPanelWidgetProps ) => React . JSX . Element ;
34
- } > ( { } ) ;
32
+ useEffect ( ( ) => {
33
+ const values : any = { ...activeSchema } ;
34
+ // [position] Change the nested position object into a flat, as a three-column layout is difficult to implement
35
+ values . x = values . position . x ;
36
+ values . y = values . position . y ;
37
+ delete values . position ;
38
+ form . setValues ( values ) ;
39
+
40
+ } , [ activeSchema , form ] ) ;
35
41
36
- const values : any = { ...activeSchema } ;
37
- // [position] Change the nested position object into a flat, as a three-column layout is difficult to implement
38
- values . x = values . position . x ;
39
- values . y = values . position . y ;
40
- delete values . position ;
41
- form . setValues ( values ) ;
42
42
43
43
const handleWatch = ( formSchema : any ) => {
44
44
const formAndSchemaValuesDiffer = ( formValue : any , schemaValue : any ) : boolean => {
@@ -67,7 +67,7 @@ const DetailView = (props: DetailViewProps) => {
67
67
// FIXME memo: https://github.com/pdfme/pdfme/pull/367#issuecomment-1857468274
68
68
if ( value === null && [ 'rotate' , 'opacity' ] . includes ( key ) ) value = undefined ;
69
69
70
- changes . push ( { key, value, schemaId : activeSchema . id } ) ;
70
+ changes . push ( { key, value, schemaId : activeSchema . id } ) ;
71
71
}
72
72
}
73
73
@@ -76,11 +76,11 @@ const DetailView = (props: DetailViewProps) => {
76
76
form . validateFields ( )
77
77
. then ( ( ) => changeSchemas ( changes ) )
78
78
. catch ( ( reason : ValidateErrorEntity ) => {
79
- if ( reason . errorFields . length ) {
79
+ if ( reason . errorFields . length ) {
80
80
changes = changes . filter ( ( change : ChangeSchemaItem ) => ! reason . errorFields . find ( ( field : {
81
- name : InternalNamePath ;
82
- errors : string [ ] ;
83
- } ) => field . name . includes ( change . key )
81
+ name : InternalNamePath ;
82
+ errors : string [ ] ;
83
+ } ) => field . name . includes ( change . key )
84
84
) ) ;
85
85
}
86
86
if ( changes . length ) {
@@ -181,25 +181,27 @@ Check this document: https://pdfme.com/docs/custom-schemas`);
181
181
} ;
182
182
}
183
183
184
- const allWidgets : typeof widgets = {
184
+ const allWidgets : {
185
+ [ key : string ] : ( props : PropPanelWidgetProps ) => React . JSX . Element ;
186
+ } = {
185
187
AlignWidget : ( p ) => < AlignWidget { ...p } { ...props } options = { options } /> ,
186
188
Divider : ( ) => (
187
- < Divider style = { { marginTop : token . marginXS , marginBottom : token . marginXS } } />
189
+ < Divider style = { { marginTop : token . marginXS , marginBottom : token . marginXS } } />
188
190
) ,
189
191
ButtonGroup : ( p ) => < ButtonGroupWidget { ...p } { ...props } options = { options } /> ,
190
192
} ;
191
193
for ( const plugin of Object . values ( pluginsRegistry ) ) {
192
194
const widgets = plugin ?. propPanel . widgets || { } ;
193
195
Object . entries ( widgets ) . forEach ( ( [ widgetKey , widgetValue ] ) => {
194
196
allWidgets [ widgetKey ] = ( p ) => (
195
- < WidgetRenderer
196
- { ...p }
197
- { ...props }
198
- options = { options }
199
- theme = { token }
200
- i18n = { i18n as ( key : keyof Dict | string ) => string }
201
- widget = { widgetValue }
202
- />
197
+ < WidgetRenderer
198
+ { ...p }
199
+ { ...props }
200
+ options = { options }
201
+ theme = { token }
202
+ i18n = { i18n as ( key : keyof Dict | string ) => string }
203
+ widget = { widgetValue }
204
+ />
203
205
) ;
204
206
} ) ;
205
207
}
0 commit comments