Skip to content

Commit

Permalink
(lint/types): fix or ignore remaining lint type errors
Browse files Browse the repository at this point in the history
- use TS 3.0 defaultProps support so that clearOnResize doesn't have to
  be optional

- ignore consistent-type-assertions and no-non-null-assertion in places
  where those hacks were intentionally used for simplicity's sake

- fix some strict-boolean-expressions by checking for types explicitly
  or using nullish coalescing
  - nullish coalescing on canvasProps isn't breaking because it already
    had PropTypes.object defined
  - nullish coalescing on DPR isn't breaking because it should be a
    number (or undefined), and shouldn't be 0
    - and even if it were 0, it would've been set to 1 before anyway
      bc of the Math.max

- ignore several strict-boolean-expressions in _resizeCanvas because
  checking for undefined specifically is a breaking change
  - 0, '', null, etc falsey width/height was still resized previously,
    so if we check for undefined instead, it will stop resizing falseys
    - 0, '', null, etc don't make a lot of sense to set a width/height,
      if these were used it may have been accidental or for some
      temporary hack
      - accidental would be a bug, so will consider a fix, but fixing
        that bug would still be breaking :/ :/
  • Loading branch information
agilgur5 committed Apr 20, 2022
1 parent fb1a42b commit e19871f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import trimCanvas from 'trim-canvas'

export interface SignatureCanvasProps extends SignaturePad.SignaturePadOptions {
canvasProps?: React.CanvasHTMLAttributes<HTMLCanvasElement>
clearOnResize?: boolean
clearOnResize: boolean
}

export class SignatureCanvas extends Component<SignatureCanvasProps> {
Expand All @@ -25,16 +25,18 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
clearOnResize: PropTypes.bool
}

static defaultProps = {
static defaultProps: Pick<SignatureCanvasProps, 'clearOnResize'> = {
clearOnResize: true
}

// this is some hack-ish init and casting to avoid `| null` everywhere :/
/* eslint-disable @typescript-eslint/consistent-type-assertions */
_sigPad: SignaturePad = {} as SignaturePad
_canvas: HTMLCanvasElement = {} as HTMLCanvasElement
/* eslint-enable @typescript-eslint/consistent-type-assertions */

private readonly setRef = (ref: HTMLCanvasElement | null): void => {
if (ref) {
if (ref !== null) {
this._canvas = ref
}
}
Expand Down Expand Up @@ -70,6 +72,7 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
const copy = document.createElement('canvas')
copy.width = this._canvas.width
copy.height = this._canvas.height
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
copy.getContext('2d')!.drawImage(this._canvas, 0, 0)
// then trim it
return trimCanvas(copy)
Expand All @@ -87,10 +90,13 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
this._resizeCanvas()
}

// a few eslint disables of strict-boolean-expressions here because changing
// these lines would be breaking -- 0s, '', null etc are falsey
_resizeCanvas = (): void => {
const canvasProps = this.props.canvasProps || {}
const canvasProps = this.props.canvasProps ?? {}
const { width, height } = canvasProps
// don't resize if the canvas has fixed width and height
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (width && height) {
return
}
Expand All @@ -99,14 +105,17 @@ export class SignatureCanvas extends Component<SignatureCanvasProps> {
/* When zoomed out to less than 100%, for some very strange reason,
some browsers report devicePixelRatio as less than 1
and only part of the canvas is cleared then. */
const ratio = Math.max(window.devicePixelRatio || 1, 1)
const ratio = Math.max(window.devicePixelRatio ?? 1, 1)

// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!width) {
canvas.width = canvas.offsetWidth * ratio
}
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!height) {
canvas.height = canvas.offsetHeight * ratio
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
canvas.getContext('2d')!.scale(ratio, ratio)
this.clear()
}
Expand Down

0 comments on commit e19871f

Please sign in to comment.