@@ -5,20 +5,22 @@ import {
55 Section ,
66 withDefault ,
77 withExposingConfigs ,
8+ withMethodExposing ,
89 eventHandlerControl ,
910 styleControl ,
1011 toJSONObjectArray ,
1112 jsonControl ,
1213 AutoHeightControl ,
1314 EditorContext ,
1415} from "lowcoder-sdk" ;
16+ import { useResizeDetector } from "react-resize-detector" ;
1517
1618import styles from "./styles.module.css" ;
1719
1820import { i18nObjs , trans } from "./i18n/comps" ;
1921
2022import { Chart } from './vendors'
21- import { useContext , useEffect , useRef , useState } from "react" ;
23+ import { useState } from "react" ;
2224
2325
2426export const CompStyles = [
@@ -59,7 +61,13 @@ export const CompStyles = [
5961 }
6062] as const ;
6163
62-
64+ interface Point {
65+ id : number ,
66+ color ?: string ,
67+ description ?: string ,
68+ x : number ,
69+ size ?: number ,
70+ }
6371
6472// const HillchartsCompBase = new UICompBuilder(childrenMap, (props: any) => {
6573let HillchartsCompBase = ( function ( ) {
@@ -74,42 +82,57 @@ let HillchartsCompBase = (function () {
7482 value : "change" ,
7583 description : "Triggers when Chart data changes" ,
7684 } ,
77- ] ) ,
85+ ] as const ) ,
7886 } ;
7987
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+ } ) => {
8194 const handleDataChange = ( ) => {
8295 props . onEvent ( "change" ) ;
8396 } ;
8497
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 ;
87102
88- useEffect ( ( ) => {
89- if ( conRef . current ) {
103+ if ( props . autoHeight ) {
90104 setDimensions ( {
91- width : conRef . current . clientWidth ,
92- height : conRef . current . clientHeight
93- } ) ;
105+ width,
106+ height : dimensions . height ,
107+ } )
108+ return ;
94109 }
95- } , [ ] ) ;
110+
111+ setDimensions ( {
112+ width,
113+ height,
114+ } )
115+ } } ) ;
96116
97117 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 >
113136 ) ;
114137} )
115138. setPropertyViewFn ( ( children : any ) => {
@@ -137,6 +160,38 @@ HillchartsCompBase = class extends HillchartsCompBase {
137160 }
138161} ;
139162
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+
140195export default withExposingConfigs ( HillchartsCompBase , [
141196 new NameConfig ( "data" , trans ( "component.data" ) ) ,
142197] ) ;
0 commit comments