1
- import React , { useEffect , useRef } from 'react' ;
1
+ import React , { useEffect , useRef , forwardRef , useImperativeHandle } from 'react' ;
2
2
import { getStroke , type StrokeOptions } from 'perfect-freehand' ;
3
3
import { getSvgPathFromStroke , getBoundingClientRect , getClinetXY , defaultOptions , defaultStyle } from './utils' ;
4
4
@@ -9,12 +9,14 @@ export interface SignatureProps extends React.SVGProps<SVGSVGElement> {
9
9
10
10
let points : number [ ] [ ] = [ ] ;
11
11
12
- export default function Signature ( props : SignatureProps = { } ) {
12
+ const Signature = forwardRef < SVGSVGElement , SignatureProps > ( ( props , ref ) => {
13
13
const { className, prefixCls = 'w-signature' , style, options, ...others } = props ;
14
14
const cls = [ className , prefixCls ] . filter ( Boolean ) . join ( ' ' ) ;
15
15
const $svg = useRef < SVGSVGElement > ( null ) ;
16
16
const $path = useRef < SVGPathElement > ( ) ;
17
17
18
+ useImperativeHandle < SVGSVGElement | null , SVGSVGElement | null > ( ref , ( ) => $svg . current , [ $svg . current ] ) ;
19
+
18
20
const handlePointerDown = ( e : React . PointerEvent < SVGSVGElement > ) => {
19
21
const { offsetY, offsetX } = getBoundingClientRect ( $svg . current ) ;
20
22
const clientX = e . clientX || e . nativeEvent . clientX ;
@@ -23,6 +25,7 @@ export default function Signature(props: SignatureProps = {}) {
23
25
const pathElm = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'path' ) ;
24
26
$path . current = pathElm ;
25
27
$svg . current ! . appendChild ( pathElm ) ;
28
+ // $svg.current?.remove()
26
29
} ;
27
30
28
31
const handlePointerMove = ( e : PointerEvent ) => {
@@ -59,4 +62,6 @@ export default function Signature(props: SignatureProps = {}) {
59
62
style = { { ...defaultStyle , ...style } }
60
63
/>
61
64
) ;
62
- }
65
+ } ) ;
66
+
67
+ export default Signature ;
0 commit comments