-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[react-signature-canvas] Add definitions #41396
Merged
elibarzilay
merged 3 commits into
DefinitelyTyped:master
from
ksocha:add-react-signature-canvas
Jan 15, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Type definitions for react-signature-canvas 1.0 | ||
// Project: https://github.com/agilgur5/react-signature-canvas | ||
// Definitions by: Kamil Socha <https://github.com/ksocha> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
|
||
import * as React from 'react'; | ||
import SignaturePad from 'signature_pad'; | ||
|
||
export interface ReactSignatureCanvasProps extends SignaturePad.SignaturePadOptions { | ||
canvasProps?: React.CanvasHTMLAttributes<HTMLCanvasElement>; | ||
clearOnResize?: boolean; | ||
} | ||
|
||
declare class ReactSignatureCanvas extends React.Component<ReactSignatureCanvasProps> { | ||
on: SignaturePad['on']; | ||
|
||
off: SignaturePad['off']; | ||
|
||
clear: SignaturePad['clear']; | ||
|
||
isEmpty: SignaturePad['isEmpty']; | ||
|
||
fromDataURL: SignaturePad['fromDataURL']; | ||
|
||
toDataURL: SignaturePad['toDataURL']; | ||
|
||
fromData: SignaturePad['fromData']; | ||
|
||
toData: SignaturePad['toData']; | ||
|
||
getCanvas(): HTMLCanvasElement; | ||
|
||
getTrimmedCanvas(): HTMLCanvasElement; | ||
|
||
getSignaturePad(): SignaturePad; | ||
} | ||
|
||
export default ReactSignatureCanvas; |
52 changes: 52 additions & 0 deletions
52
types/react-signature-canvas/react-signature-canvas-tests.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import ReactSignatureCanvas from 'react-signature-canvas'; | ||
import SignaturePad from 'signature_pad'; | ||
import * as React from 'react'; | ||
|
||
const ReactSignatureCanvasNoOptions: JSX.Element = <ReactSignatureCanvas />; | ||
|
||
const ReactSignatureCanvasDotSizeFunctionOptions: JSX.Element = <ReactSignatureCanvas dotSize={() => 4} />; | ||
|
||
const ReactSignatureCanvasAllOptions: JSX.Element = ( | ||
<ReactSignatureCanvas | ||
dotSize={2} | ||
minWidth={2} | ||
maxWidth={2} | ||
throttle={2} | ||
minDistance={2} | ||
backgroundColor="blue" | ||
penColor="red" | ||
velocityFilterWeight={2} | ||
onBegin={(event: MouseEvent) => {}} | ||
onEnd={(event: MouseEvent) => {}} | ||
canvasProps={{ | ||
title: 'canvas', | ||
width: '50%', | ||
height: 300, | ||
className: 'canvas', | ||
style: { border: '2px solid #000' }, | ||
}} | ||
clearOnResize | ||
/> | ||
); | ||
|
||
class Example extends React.Component { | ||
canvasRef = React.createRef<ReactSignatureCanvas>(); | ||
|
||
componentDidMount() { | ||
if (this.canvasRef.current) { | ||
this.canvasRef.current.clear(); | ||
this.canvasRef.current.fromData([[new SignaturePad.Point(1, 2, 3)]]); | ||
this.canvasRef.current.fromDataURL('url'); | ||
this.canvasRef.current.fromDataURL('url', { height: 1, width: 4 }); | ||
const isEmptyResult: boolean = this.canvasRef.current.isEmpty(); | ||
this.canvasRef.current.off(); | ||
this.canvasRef.current.on(); | ||
const toDataResult: SignaturePad.Point[][] = this.canvasRef.current.toData(); | ||
const toDataURLResult: string = this.canvasRef.current.toDataURL(); | ||
} | ||
} | ||
|
||
render() { | ||
return <ReactSignatureCanvas ref={this.canvasRef} />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
], | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strictFunctionTypes": true, | ||
"strictNullChecks": true, | ||
"baseUrl": "../", | ||
"typeRoots": [ | ||
"../" | ||
], | ||
"types": [], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"jsx": "react" | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"react-signature-canvas-tests.tsx" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "extends": "dtslint/dt.json" } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
HTMLCanvasElement
the right generic type? I thought it should becanvasProps<T>?: React.CanvasHTMLAttributes<T>
, but I'm not 100% sure. If you could provide an explanation, would be helpful to understand better 🙏 .See same discussion in agilgur5/react-signature-canvas#25 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is the only proper way to type the attributes of a canvas element. This is how it's used in
React
itself:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, confusing why it would be a generic then 🤔 I guess it passes type-checking in the tests, so that does mean it works o.o.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's generic because of consistency. In the React's type definitions,
CanvasHTMLAttributes
is defined as:IntrinsicElements
interface which defines all html elements looks like:Generic type passed to
HTMLAttributes
is used mostly (if not only) in event handlers definitions, e.g.:onFocus?: FocusEventHandler<T>;