@@ -112,18 +112,27 @@ const SignatureCanvas = React.lazy(() => import("react-signature-canvas"));
112112
113113let SignatureTmpComp = ( function ( ) {
114114 return new UICompBuilder ( childrenMap , ( props , dispatch ) => {
115- let canvas : SignatureCanvasType | null = null ;
115+ const canvasRef = useRef < SignatureCanvasType | null > ( null ) ;
116116 const [ isBegin , setIsBegin ] = useState ( false ) ;
117117 const [ canvasSize , setCanvasSize ] = useState ( [ 0 , 0 ] ) ;
118118 const conRef = useRef < HTMLDivElement > ( null ) ;
119119
120120 const updateValue = ( isClear : boolean = false ) => {
121- const clear = isClear || canvas ?. toData ( ) . length === 0 ;
122- if ( canvas ) {
123- clear && canvas ?. clear ( ) ;
121+ if ( ! canvasRef . current ) return ;
122+
123+ const clear = isClear || canvasRef . current . toData ( ) . length === 0 ;
124+ if ( clear ) {
125+ canvasRef . current . clear ( ) ;
126+ setIsBegin ( false ) ;
124127 dispatch (
125128 multiChangeAction ( {
126- value : changeValueAction ( clear ? "" : canvas . toDataURL ( ) , false ) ,
129+ value : changeValueAction ( "" , false ) ,
130+ } )
131+ ) ;
132+ } else {
133+ dispatch (
134+ multiChangeAction ( {
135+ value : changeValueAction ( canvasRef . current . toDataURL ( ) , false ) ,
127136 } )
128137 ) ;
129138 }
@@ -132,15 +141,27 @@ let SignatureTmpComp = (function () {
132141 useResizeDetector ( {
133142 targetRef : conRef ,
134143 onResize : ( { width, height} : ResizePayload ) => {
135- width && height && setCanvasSize ( [ width , height ] ) ;
136- updateValue ( true ) ;
144+ if ( width && height ) {
145+ setCanvasSize ( [ width , height ] ) ;
146+ // Don't clear on resize as it breaks the drawing functionality
147+ // updateValue(true);
148+ }
137149 } ,
138150 } ) ;
139151
152+ // Cleanup on unmount
153+ useEffect ( ( ) => {
154+ return ( ) => {
155+ if ( canvasRef . current ) {
156+ canvasRef . current . clear ( ) ;
157+ }
158+ } ;
159+ } , [ ] ) ;
160+
140161 return props . label ( {
141162 style : props . style ,
142163 labelStyle : props . labelStyle ,
143- inputFieldStyle :props . inputFieldStyle ,
164+ inputFieldStyle : props . inputFieldStyle ,
144165 children : (
145166 < Wrapper
146167 ref = { conRef }
@@ -153,9 +174,7 @@ let SignatureTmpComp = (function () {
153174 < div key = "signature" className = "signature" >
154175 < Suspense fallback = { < Skeleton /> } >
155176 < SignatureCanvas
156- ref = { ( ref ) => {
157- canvas = ref ;
158- } }
177+ ref = { canvasRef }
159178 penColor = { props . inputFieldStyle . pen }
160179 clearOnResize = { false }
161180 canvasProps = { {
@@ -168,7 +187,9 @@ let SignatureTmpComp = (function () {
168187 setIsBegin ( false ) ;
169188 props . onEvent ( "change" ) ;
170189 } }
171- onBegin = { ( ) => setIsBegin ( true ) }
190+ onBegin = { ( ) => {
191+ setIsBegin ( true ) ;
192+ } }
172193 />
173194 </ Suspense >
174195 </ div >
@@ -178,10 +199,11 @@ let SignatureTmpComp = (function () {
178199 < span className = "anticon" >
179200 < UndoIcon
180201 onClick = { ( ) => {
181- const data = canvas ?. toData ( ) ;
182- if ( data ) {
183- data ?. pop ( ) ;
184- canvas ?. fromData ( data ) ;
202+ if ( ! canvasRef . current ) return ;
203+ const data = canvasRef . current . toData ( ) ;
204+ if ( data && data . length > 0 ) {
205+ data . pop ( ) ;
206+ canvasRef . current . fromData ( data ) ;
185207 updateValue ( ) ;
186208 props . onEvent ( "change" ) ;
187209 }
0 commit comments