Skip to content
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
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions types/react-signature-canvas/index.d.ts
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>;
Copy link
Contributor

@agilgur5 agilgur5 Jan 4, 2020

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 be canvasProps<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)

Copy link
Contributor Author

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:

canvas: React.DetailedHTMLProps<React.CanvasHTMLAttributes<HTMLCanvasElement>, HTMLCanvasElement>;

Copy link
Contributor

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.

Copy link
Contributor Author

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:

interface CanvasHTMLAttributes<T> extends HTMLAttributes<T> {
    height?: number | string;
    width?: number | string;
}

IntrinsicElements interface which defines all html elements looks like:

[...]
button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>;
canvas: React.DetailedHTMLProps<React.CanvasHTMLAttributes<HTMLCanvasElement>, HTMLCanvasElement>;
caption: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
[...]

Generic type passed to HTMLAttributes is used mostly (if not only) in event handlers definitions, e.g.: onFocus?: FocusEventHandler<T>;

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 types/react-signature-canvas/react-signature-canvas-tests.tsx
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} />;
}
}
25 changes: 25 additions & 0 deletions types/react-signature-canvas/tsconfig.json
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"
]
}
1 change: 1 addition & 0 deletions types/react-signature-canvas/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }