|
1 | 1 | import classNames from "classnames"; |
2 | | -import { ReactElement, useEffect, useRef } from "react"; |
3 | | -// import ReactResizeDetector from "react-resize-detector"; |
4 | | -import SignaturePad, { Options } from "signature_pad"; |
| 2 | +import { ReactElement } from "react"; |
5 | 3 |
|
| 4 | +import { useSignaturePad } from "src/utils/useSignaturePad"; |
| 5 | +import Utils from "../utils/Utils"; |
| 6 | +import { SignatureProps } from "../utils/customTypes"; |
6 | 7 | import { Alert } from "./Alert"; |
7 | 8 | import { Grid } from "./Grid"; |
8 | | -import { Dimensions, SizeContainer } from "./SizeContainer"; |
9 | | -import { SignatureContainerProps } from "typings/SignatureProps"; |
10 | | -import Utils from "../utils/Utils"; |
11 | | - |
12 | | -export type penOptions = "fountain" | "ballpoint" | "marker"; |
13 | | - |
14 | | -export interface SignatureProps extends Dimensions, SignatureContainerProps { |
15 | | - className: string; |
16 | | - alertMessage?: string; |
17 | | - clearSignature: boolean; |
18 | | - showGrid: boolean; |
19 | | - gridCellWidth: number; |
20 | | - gridCellHeight: number; |
21 | | - gridBorderColor: string; |
22 | | - gridBorderWidth: number; |
23 | | - penType: penOptions; |
24 | | - penColor: string; |
25 | | - onSignEndAction?: (imageUrl?: string) => void; |
26 | | - wrapperStyle?: object; |
27 | | - readOnly: boolean; |
28 | | -} |
| 9 | +import { SizeContainer } from "./SizeContainer"; |
29 | 10 |
|
30 | 11 | export function SignatureComponent(props: SignatureProps): ReactElement { |
31 | | - const { className, alertMessage, wrapperStyle } = props; |
32 | | - const canvasRef = useRef<HTMLCanvasElement | null>(null); |
33 | | - const signaturePadRef = useRef<SignaturePad | null>(null); |
34 | | - |
35 | | - const handleSignEnd = (): void => { |
36 | | - const imageDataUrl = signaturePadRef.current?.toDataURL(); |
| 12 | + const { className, alertMessage, wrapperStyle, imageSource, hasSignatureAttribute, onSignEndAction } = props; |
37 | 13 |
|
| 14 | + const handleSignEnd = (imageDataUrl?: string): void => { |
38 | 15 | if (imageDataUrl) { |
39 | | - props.imageSource.setValue(Utils.convertUrlToBlob(imageDataUrl)); |
| 16 | + imageSource.setValue(Utils.convertUrlToBlob(imageDataUrl)); |
40 | 17 | } |
41 | | - // Trigger microflow to update signature attribute |
42 | | - if (props.onSignEndAction) { |
43 | | - props.onSignEndAction(signaturePadRef.current?.toDataURL()); |
| 18 | + |
| 19 | + if (hasSignatureAttribute) { |
| 20 | + hasSignatureAttribute.setValue(true); |
44 | 21 | } |
45 | | - }; |
46 | 22 |
|
47 | | - const signaturePadOptions = (): Options => { |
48 | | - let options: Options = {}; |
49 | | - if (props.penType === "fountain") { |
50 | | - options = { minWidth: 0.6, maxWidth: 2.6, velocityFilterWeight: 0.6 }; |
51 | | - } else if (props.penType === "ballpoint") { |
52 | | - options = { minWidth: 1.4, maxWidth: 1.5, velocityFilterWeight: 1.5 }; |
53 | | - } else if (props.penType === "marker") { |
54 | | - options = { minWidth: 2, maxWidth: 4, velocityFilterWeight: 0.9 }; |
| 23 | + // Trigger microflow to update signature attribute |
| 24 | + if (onSignEndAction) { |
| 25 | + onSignEndAction(imageDataUrl); |
55 | 26 | } |
56 | | - return options; |
57 | 27 | }; |
58 | 28 |
|
59 | | - useEffect(() => { |
60 | | - if (canvasRef.current) { |
61 | | - signaturePadRef.current = new SignaturePad(canvasRef.current, { |
62 | | - penColor: props.penColor, |
63 | | - ...signaturePadOptions() |
64 | | - }); |
65 | | - signaturePadRef.current.addEventListener("endStroke", handleSignEnd); |
66 | | - if (props.readOnly) { |
67 | | - signaturePadRef.current?.off(); |
68 | | - } |
69 | | - } |
70 | | - }, []); |
| 29 | + const { canvasRef, signaturePadRef } = useSignaturePad(props, handleSignEnd); |
71 | 30 |
|
72 | 31 | const onResize = (): void => { |
73 | 32 | if (canvasRef.current) { |
74 | | - const data = signaturePadRef.current?.toData(); |
75 | 33 | canvasRef.current.width = |
76 | 34 | canvasRef.current && canvasRef.current.parentElement ? canvasRef.current.parentElement.offsetWidth : 0; |
77 | 35 | canvasRef.current.height = |
78 | 36 | canvasRef.current && canvasRef.current.parentElement ? canvasRef.current.parentElement.offsetHeight : 0; |
79 | | - signaturePadRef.current?.clear(); |
80 | | - if (data) { |
81 | | - signaturePadRef.current?.fromData(data); |
82 | | - } |
| 37 | + signaturePadRef.current?.redraw(); |
83 | 38 | } |
84 | 39 | }; |
85 | 40 |
|
|
0 commit comments