@@ -5,20 +5,22 @@ import {
5
5
Section ,
6
6
withDefault ,
7
7
withExposingConfigs ,
8
+ withMethodExposing ,
8
9
eventHandlerControl ,
9
10
styleControl ,
10
11
toJSONObjectArray ,
11
12
jsonControl ,
12
13
AutoHeightControl ,
13
14
EditorContext ,
14
15
} from "lowcoder-sdk" ;
16
+ import { useResizeDetector } from "react-resize-detector" ;
15
17
16
18
import styles from "./styles.module.css" ;
17
19
18
20
import { i18nObjs , trans } from "./i18n/comps" ;
19
21
20
22
import { Chart } from './vendors'
21
- import { useContext , useEffect , useRef , useState } from "react" ;
23
+ import { useState } from "react" ;
22
24
23
25
24
26
export const CompStyles = [
@@ -59,7 +61,13 @@ export const CompStyles = [
59
61
}
60
62
] as const ;
61
63
62
-
64
+ interface Point {
65
+ id : number ,
66
+ color ?: string ,
67
+ description ?: string ,
68
+ x : number ,
69
+ size ?: number ,
70
+ }
63
71
64
72
// const HillchartsCompBase = new UICompBuilder(childrenMap, (props: any) => {
65
73
let HillchartsCompBase = ( function ( ) {
@@ -74,42 +82,57 @@ let HillchartsCompBase = (function () {
74
82
value : "change" ,
75
83
description : "Triggers when Chart data changes" ,
76
84
} ,
77
- ] ) ,
85
+ ] as const ) ,
78
86
} ;
79
87
80
- return new UICompBuilder ( childrenMap , ( props : { onEvent : ( arg0 : string ) => void ; styles : { backgroundColor : any ; border : any ; radius : any ; borderWidth : any ; margin : any ; padding : any ; textSize : any ; } ; data : any [ ] | null | undefined ; } ) => {
88
+ return new UICompBuilder ( childrenMap , ( props : {
89
+ onEvent : any ;
90
+ styles : { backgroundColor : any ; border : any ; radius : any ; borderWidth : any ; margin : any ; padding : any ; textSize : any ; } ;
91
+ data : any [ ] | null | undefined ;
92
+ autoHeight : boolean ;
93
+ } ) => {
81
94
const handleDataChange = ( ) => {
82
95
props . onEvent ( "change" ) ;
83
96
} ;
84
97
85
- const conRef = useRef < HTMLDivElement > ( null ) ;
86
- const [ dimensions , setDimensions ] = useState ( { width : 400 , height : 250 } ) ;
98
+ const [ dimensions , setDimensions ] = useState ( { width : 480 , height : 280 } ) ;
99
+ const { width, height, ref : conRef } = useResizeDetector ( { onResize : ( ) => {
100
+ const container = conRef . current ;
101
+ if ( ! container || ! width || ! height ) return ;
87
102
88
- useEffect ( ( ) => {
89
- if ( conRef . current ) {
103
+ if ( props . autoHeight ) {
90
104
setDimensions ( {
91
- width : conRef . current . clientWidth ,
92
- height : conRef . current . clientHeight
93
- } ) ;
105
+ width,
106
+ height : dimensions . height ,
107
+ } )
108
+ return ;
94
109
}
95
- } , [ ] ) ;
110
+
111
+ setDimensions ( {
112
+ width,
113
+ height,
114
+ } )
115
+ } } ) ;
96
116
97
117
return (
98
- < div ref = { conRef } className = { styles . wrapper } style = { {
99
- display : "flex" ,
100
- justifyContent : "center" ,
101
- height : `100%` ,
102
- width : `100%` ,
103
- backgroundColor : `${ props . styles . backgroundColor } ` ,
104
- borderColor : `${ props . styles . border } ` ,
105
- borderRadius : `${ props . styles . radius } ` ,
106
- borderWidth : `${ props . styles . borderWidth } ` ,
107
- margin : `${ props . styles . margin } ` ,
108
- padding : `${ props . styles . padding } ` ,
109
- fontSize : `${ props . styles . textSize } ` ,
110
- } } >
111
- < Chart data = { props . data } height = { dimensions . height } width = { dimensions . width } onDataChange = { handleDataChange } />
112
- </ div >
118
+ < div ref = { conRef } className = { styles . wrapper } style = { {
119
+ height : `100%` ,
120
+ width : `100%` ,
121
+ backgroundColor : `${ props . styles . backgroundColor } ` ,
122
+ borderColor : `${ props . styles . border } ` ,
123
+ borderRadius : `${ props . styles . radius } ` ,
124
+ borderWidth : `${ props . styles . borderWidth } ` ,
125
+ margin : `${ props . styles . margin } ` ,
126
+ padding : `${ props . styles . padding } ` ,
127
+ fontSize : `${ props . styles . textSize } ` ,
128
+ } } >
129
+ < Chart
130
+ data = { props . data }
131
+ height = { dimensions . height }
132
+ width = { dimensions . width }
133
+ onDataChange = { handleDataChange }
134
+ />
135
+ </ div >
113
136
) ;
114
137
} )
115
138
. setPropertyViewFn ( ( children : any ) => {
@@ -137,6 +160,38 @@ HillchartsCompBase = class extends HillchartsCompBase {
137
160
}
138
161
} ;
139
162
163
+ HillchartsCompBase = withMethodExposing ( HillchartsCompBase , [
164
+ {
165
+ method : {
166
+ name : "setPoint" ,
167
+ description : trans ( "methods.setPoint" ) ,
168
+ params : [ {
169
+ name : "data" ,
170
+ type : "JSON" ,
171
+ description : "JSON value"
172
+ } ] ,
173
+ } ,
174
+ execute : ( comp : any , values : any [ ] ) => {
175
+ const point = values [ 0 ] as Point ;
176
+ if ( typeof point !== 'object' ) {
177
+ return Promise . reject ( trans ( "methods.invalidInput" ) )
178
+ }
179
+ if ( ! point . id ) {
180
+ return Promise . reject ( trans ( "methods.requiredField" , { field : 'ID' } ) ) ;
181
+ }
182
+ if ( ! point . x ) {
183
+ return Promise . reject ( trans ( "methods.requiredField" , { field : 'X position' } ) ) ;
184
+ }
185
+ const data = comp . children . data . getView ( ) ;
186
+ const newData = [
187
+ ...data ,
188
+ point ,
189
+ ] ;
190
+ comp . children . data . dispatchChangeValueAction ( JSON . stringify ( newData , null , 2 ) ) ;
191
+ }
192
+ } ,
193
+ ] ) ;
194
+
140
195
export default withExposingConfigs ( HillchartsCompBase , [
141
196
new NameConfig ( "data" , trans ( "component.data" ) ) ,
142
197
] ) ;
0 commit comments