From 5eac3e0e04d319c5319c6032922220199f8a0c1c Mon Sep 17 00:00:00 2001 From: Alessandro Lion <39250826+tastydev@users.noreply.github.com> Date: Fri, 3 May 2024 11:35:03 +0200 Subject: [PATCH] fix(onFulfill): make sure to fire once per code fulfill onFulfill always fired twice before which caused bugs if implementations rely on code fulfill event detection i.e to post requests automatically. Ths fix will make sure to only fire 1 event per code completion --- src/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 0a00f22..eb93439 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,8 @@ class SmoothPinCodeInput extends Component { }; ref = React.createRef(); inputRef = React.createRef(); - + didFireOnfulfillForCodeRef = React.createRef(); + animate = ({ animation = "shake", duration = 650 }) => { if (!this.props.animated) { return new Promise((resolve, reject) => reject(new Error("Animations are disabled"))); @@ -66,10 +67,16 @@ class SmoothPinCodeInput extends Component { code = (code.match(/[0-9]/g) || []).join(""); } + if (code.length < codeLength) { + this.didFireOnfulfillForCodeRef.current = false + } + if (onTextChange) { onTextChange(code); } - if (code.length === codeLength && onFulfill) { + + if (code.length === codeLength && !this.didFireOnfulfillForCodeRef.current && onFulfill) { + this.didFireOnfulfillForCodeRef.current = true onFulfill(code); }