You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have worked through some of the deficiencies in the prior react hooks example posted, so please find an updated version with the following changes which I think is worthy of posting for some of the reasons below:
It's now a "controlled" component, eg. rather than handling both the button, modal and preview, this component just handles the modal. This lets you customise the appearance easily, and I have provided an example of this at the bottom.
I've added a "signee name" input, which makes sense for most use cases, but is toggle-able via the displayNameInput prop.
I've added validation ensuring you can't hit "save" without filling in the required fields. This is tricky, and thus my main reasoning for posting this example. It's tricky because using hooks you don't get access to an easy lifecycle event to know what the internal state of the signaturepad is. You could call isEmpty() inside a poll, but this example uses onDrawEnd which I think is elegant.
You'll note I've left bootstrap classes in. If you're on Bootstrap it'll look nice out of the box, otherwise you'll want to drop in your own utility classes.
importReact,{useState,useRef,useCallback}from'react'importPropTypesfrom'prop-types'importSignatureCanvasfrom'react-signature-canvas'importModal,{Footer}from'react-modal'constModalSignatureCanvas=({onSave, onHide, widthRatio, canvasProps, displayNameInput =false})=>{const[signatureResult,setSignatureResult]=useState('')const[name,setName]=useState('')constsigCanvas=useRef({})constsigPad=useRef({})constsetNameOnChange=(event)=>{setName(event.target.value)}constsetSignatureOnChange=()=>{constdataURL=sigCanvas.current.toDataURL()setSignatureResult(dataURL)}constsaveInput=()=>{onSave({dataURL: signatureResult,name: name})}constclearInput=()=>{sigPad.current.clear()setSignatureResult('')}constmeasuredRef=useCallback(node=>{constresizeCanvas=(signaturePad,canvas)=>{canvas.width=canvas.parentElement.clientWidth// width of the .canvasWrappercanvas.height=canvas.parentElement.clientWidth/widthRatiosignaturePad.clear()}if(node!==null){sigCanvas.current=node.getCanvas()sigPad.current=node.getSignaturePad()resizeCanvas(node.getSignaturePad(),node.getCanvas())}},[widthRatio])constisNameValidIfRequired=(displayNameInput&&!!name)||!displayNameInputconstisSignatureValid=!!signatureResultconstisFormValid=isNameValidIfRequired&&isSignatureValidreturn(<Modaltitle="Enter your signature"onHide={onHide}><divclassName="canvasWrapper"><SignatureCanvascanvasProps={canvasProps}ref={measuredRef}onEnd={setSignatureOnChange}/></div>{displayNameInput&&<divclassName="nameInput"><label>Name of person entering signature:</label><inputtype="text"className="form-control"onChange={setNameOnChange}value={name}/></div>}<Footer><divclassName="btn-group btn-block"><buttontype="button"className="btn btn-secondary w-50"onClick={clearInput}>Clear</button><buttontype="button"className="btn btn-primary w-50"onClick={saveInput}disabled={!isFormValid}>Save</button></div></Footer></Modal>)}ModalSignatureCanvas.propTypes={canvasProps: PropTypes.object,widthRatio: PropTypes.number.isRequired,onSave: PropTypes.func,onHide: PropTypes.func,displayNameInput: PropTypes.bool,}exportdefaultModalSignatureCanvas
Apologies if this isn't in the right place to post this, but the docs just don't cover the intricacies of implementing this component. Hope this helps someone!
The text was updated successfully, but these errors were encountered:
agilgur5
changed the title
Updated react hooks example
Example with modal, hooks, validation, and name
Mar 6, 2020
While I certainly think this is a useful piece of code, as I've said in that issue, for examples to really be easy to grok and use, they must be minimal, and this adds a few features. This is similarly not a pure hooks example.
Perhaps this could be good for a Storybook with progressively advanced use cases in the future, right now these seem better as Gists, CodeSandbox examples, blog posts, tutorials, or separate components that wrap react-signature-canvas. Storybook will probably be the next thing I work on after some refactoring and TypeScript support (#42).
Also if you need to resize the canvas due to it being hidden initially, you can call ._resizeCanvas. It's currently a private API, but I've used it myself for this use case, so perhaps it might be good to make it public.
I have worked through some of the deficiencies in the prior react hooks example posted, so please find an updated version with the following changes which I think is worthy of posting for some of the reasons below:
displayNameInput
prop.You'll note I've left bootstrap classes in. If you're on Bootstrap it'll look nice out of the box, otherwise you'll want to drop in your own utility classes.
Example: Show signature and a change button
Which looks like:
Prompt for collection:
Modal open:
Preview on completion:
Apologies if this isn't in the right place to post this, but the docs just don't cover the intricacies of implementing this component. Hope this helps someone!
The text was updated successfully, but these errors were encountered: