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

fix: add default penColor prop #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lav39
Copy link

@Lav39 Lav39 commented Jul 18, 2024

Summary

This PR addresses an issue where the penColor prop might be passed as undefined during parent component re-renders. This causes the underlying signature_pad library to set the fillColor as undefined, preventing any further drawing on the canvas. To resolve this, we are adding penColor as a default prop with a value of 'black'.

Changes

Added penColor to defaultProps with a default value of 'black'.

How to reproduce the issue

import React, { useRef, useCallback, useState } from "react";
import SignatureCanvas from "react-signature-canvas";

const App = (props) => {
  const { penColor } = props;
  const signaturePadRef = useRef();
  const [counter, setCounter] = useState(0);

  function clearPad() {
    signaturePadRef.current?.clear();
  }

  const onBegin = useCallback(() => {
    setCounter(counter + 1);
  }, []);

  const onEnd = useCallback(() => {
    setCounter(counter + 1);
  }, []);

  return (
    <div style={{ position: "relative", border: "1px solid" }}>
      <div onClick={clearPad}>Clear</div>
      <SignatureCanvas
        ref={signaturePadRef}
        onBegin={onBegin}
        onEnd={onEnd}
        canvasProps={{ style: { width: "100%", height: "100%" } }}
        penColor={undefined}
      />
    </div>
  );
};

export default App;
  1. Run above code in a react sandbox.
  2. Draw something and then click on Clear text and try to draw some again and it won't work.
  3. If you comment out the penColor prop, you will not encounter the issue.

@agilgur5 agilgur5 changed the title fix: add default penColor prop to SignatureCanvas fix: add default penColor prop Jul 18, 2024
Copy link
Owner

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        penColor={undefined}

3. If you comment out the penColor prop, you will not encounter the issue.

This seems contrived to me

might be passed as undefined during parent component re-renders.

I'm not sure this should be resolved here, but rather in the parent component. You could technically apply a similar fix for every other inherited prop, so that does not seem like good precedent. Special casing is a rabbit hole.

There were also no tests added here despite the special case.

* In such cases, the underlying signature_pad assigns fillColor as undefined, preventing drawing.
* Ideally, this issue should be fixed in the signature_pad library, but since we are using an older version, we need to handle it here.
*/
penColor: 'black',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could be a breaking change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a breaking change.
This is inspired from underlying library: https://github.com/szimek/signature_pad/blob/master/src/signature_pad.ts#L96

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or rather, the 2.x version: https://github.com/szimek/signature_pad/blob/2171e510b74362a8dd7c8e7fc77a142f16a4aed1/src/signature_pad.js#L24

Although yes, in both cases, upstream does not prevent you from assigning undefined. As a wrapper, I think following that behavior and not deviating from it is appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants