@@ -112,18 +112,27 @@ const SignatureCanvas = React.lazy(() => import("react-signature-canvas"));
112
112
113
113
let SignatureTmpComp = ( function ( ) {
114
114
return new UICompBuilder ( childrenMap , ( props , dispatch ) => {
115
- let canvas : SignatureCanvasType | null = null ;
115
+ const canvasRef = useRef < SignatureCanvasType | null > ( null ) ;
116
116
const [ isBegin , setIsBegin ] = useState ( false ) ;
117
117
const [ canvasSize , setCanvasSize ] = useState ( [ 0 , 0 ] ) ;
118
118
const conRef = useRef < HTMLDivElement > ( null ) ;
119
119
120
120
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 ) ;
124
127
dispatch (
125
128
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 ) ,
127
136
} )
128
137
) ;
129
138
}
@@ -132,15 +141,27 @@ let SignatureTmpComp = (function () {
132
141
useResizeDetector ( {
133
142
targetRef : conRef ,
134
143
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
+ }
137
149
} ,
138
150
} ) ;
139
151
152
+ // Cleanup on unmount
153
+ useEffect ( ( ) => {
154
+ return ( ) => {
155
+ if ( canvasRef . current ) {
156
+ canvasRef . current . clear ( ) ;
157
+ }
158
+ } ;
159
+ } , [ ] ) ;
160
+
140
161
return props . label ( {
141
162
style : props . style ,
142
163
labelStyle : props . labelStyle ,
143
- inputFieldStyle :props . inputFieldStyle ,
164
+ inputFieldStyle : props . inputFieldStyle ,
144
165
children : (
145
166
< Wrapper
146
167
ref = { conRef }
@@ -153,9 +174,7 @@ let SignatureTmpComp = (function () {
153
174
< div key = "signature" className = "signature" >
154
175
< Suspense fallback = { < Skeleton /> } >
155
176
< SignatureCanvas
156
- ref = { ( ref ) => {
157
- canvas = ref ;
158
- } }
177
+ ref = { canvasRef }
159
178
penColor = { props . inputFieldStyle . pen }
160
179
clearOnResize = { false }
161
180
canvasProps = { {
@@ -168,7 +187,9 @@ let SignatureTmpComp = (function () {
168
187
setIsBegin ( false ) ;
169
188
props . onEvent ( "change" ) ;
170
189
} }
171
- onBegin = { ( ) => setIsBegin ( true ) }
190
+ onBegin = { ( ) => {
191
+ setIsBegin ( true ) ;
192
+ } }
172
193
/>
173
194
</ Suspense >
174
195
</ div >
@@ -178,10 +199,11 @@ let SignatureTmpComp = (function () {
178
199
< span className = "anticon" >
179
200
< UndoIcon
180
201
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 ) ;
185
207
updateValue ( ) ;
186
208
props . onEvent ( "change" ) ;
187
209
}
0 commit comments