11import { PureComponent , ReactNode } from "react" ;
2-
3- // @ts -expect-error signature_pad has no types
4- import SignaturePad , { IOptions } from "signature_pad" ;
2+ import SignaturePad , { Options } from "signature_pad" ;
53import classNames from "classnames" ;
6- import ReactResizeDetector from "react-resize-detector" ;
74
85import { Alert } from "./Alert" ;
96import { Grid } from "./Grid" ;
@@ -31,8 +28,7 @@ export type penOptions = "fountain" | "ballpoint" | "marker";
3128
3229export class Signature extends PureComponent < SignatureProps > {
3330 private canvasNode : HTMLCanvasElement | null = null ;
34- // @ts -expect-error signature_pad has no types
35- private signaturePad : SignaturePad ;
31+ private signaturePad : SignaturePad | undefined ;
3632
3733 render ( ) : ReactNode {
3834 const { className, alertMessage, wrapperStyle } = this . props ;
@@ -43,6 +39,7 @@ export class Signature extends PureComponent<SignatureProps> {
4339 className = { classNames ( "widget-signature" , className ) }
4440 classNameInner = "widget-signature-wrapper form-control mx-textarea-input mx-textarea"
4541 style = { wrapperStyle }
42+ onResize = { this . onResize }
4643 >
4744 < Alert bootstrapStyle = "danger" > { alertMessage } </ Alert >
4845 < Grid { ...this . props } />
@@ -52,7 +49,6 @@ export class Signature extends PureComponent<SignatureProps> {
5249 this . canvasNode = node ;
5350 } }
5451 />
55- < ReactResizeDetector handleWidth handleHeight onResize = { this . onResize } />
5652 </ SizeContainer >
5753 ) ;
5854 }
@@ -61,6 +57,7 @@ export class Signature extends PureComponent<SignatureProps> {
6157 if ( this . canvasNode ) {
6258 this . signaturePad = new SignaturePad ( this . canvasNode , {
6359 penColor : this . props . penColor ,
60+ // @ts -expect-error // looks like this never worked, there is no onEnd in SignaturePad code
6461 onEnd : this . handleSignEnd ,
6562 ...this . signaturePadOptions ( )
6663 } ) ;
@@ -87,7 +84,7 @@ export class Signature extends PureComponent<SignatureProps> {
8784 }
8885
8986 private onResize = ( ) : void => {
90- if ( this . canvasNode ) {
87+ if ( this . canvasNode && this . signaturePad ) {
9188 const data = this . signaturePad . toData ( ) ;
9289 this . canvasNode . width =
9390 this . canvasNode && this . canvasNode . parentElement ? this . canvasNode . parentElement . offsetWidth : 0 ;
@@ -98,8 +95,8 @@ export class Signature extends PureComponent<SignatureProps> {
9895 }
9996 } ;
10097
101- private signaturePadOptions ( ) : IOptions {
102- let options : IOptions = { } ;
98+ private signaturePadOptions ( ) : Options {
99+ let options : Options = { } ;
103100 if ( this . props . penType === "fountain" ) {
104101 options = { minWidth : 0.6 , maxWidth : 2.6 , velocityFilterWeight : 0.6 } ;
105102 } else if ( this . props . penType === "ballpoint" ) {
@@ -111,7 +108,7 @@ export class Signature extends PureComponent<SignatureProps> {
111108 }
112109
113110 private handleSignEnd = ( ) : void => {
114- if ( this . props . onSignEndAction ) {
111+ if ( this . props . onSignEndAction && this . signaturePad ) {
115112 this . props . onSignEndAction ( this . signaturePad . toDataURL ( ) ) ;
116113 }
117114 } ;
0 commit comments