Skip to content

Commit

Permalink
fix: optimize onComplete/onIncomplete invocation
Browse files Browse the repository at this point in the history
Ensure onComplete is invoked only once when the
pin input is fully completed. Invoke onIncomplete
only once when the pin input changes from complete
to incomplete. Adjust default font-size for
pin-input-raw snippet.
  • Loading branch information
satnaing committed Sep 14, 2024
1 parent 6a4a3d4 commit ffac428
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
17 changes: 10 additions & 7 deletions src/assets/snippets/pin-input-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,18 @@ const PinInput = React.forwardRef<HTMLDivElement, PinInputProps>(
onChange(pinValue)
}, [onChange, pinValue])
/* call onComplete func if pinValue is valid and completed */
/* call onComplete/onIncomplete func if pinValue is valid and completed/incompleted */
const completeRef = React.useRef(pinValue.length === length);
React.useEffect(() => {
if (onComplete && pinValue.length === length) {
onComplete(pinValue)
if (pinValue.length === length && completeRef.current === false) {
completeRef.current = true;
if (onComplete) onComplete(pinValue);
}
if (onIncomplete && pinValue.length !== length) {
onIncomplete(pinValue)
if (pinValue.length !== length && completeRef.current === true) {
completeRef.current = false;
if (onIncomplete) onIncomplete(pinValue);
}
}, [length, onComplete, onIncomplete, pinValue])
}, [length, onComplete, onIncomplete, pinValue, pins, value]);
/* focus on first input field if autoFocus is set */
React.useEffect(() => {
Expand Down Expand Up @@ -207,7 +210,7 @@ const PinInputFieldNoRef = <T extends React.ElementType = 'input'>(
{
className,
component,
fontSize = '2.5rem',
fontSize = '1rem',
...props
}: PinInputFieldProps<T> &
(React.ComponentType<T> extends undefined
Expand Down
15 changes: 9 additions & 6 deletions src/assets/snippets/pin-input-shadcn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ const PinInput = React.forwardRef<HTMLDivElement, PinInputProps>(
onChange(pinValue)
}, [onChange, pinValue]);
/* call onComplete func if pinValue is valid and completed */
/* call onComplete/onIncomplete func if pinValue is valid and completed/incompleted */
const completeRef = React.useRef(pinValue.length === length);
React.useEffect(() => {
if (onComplete && pinValue.length === length) {
onComplete(pinValue);
if (pinValue.length === length && completeRef.current === false) {
completeRef.current = true;
if (onComplete) onComplete(pinValue);
}
if (onIncomplete && pinValue.length !== length) {
onIncomplete(pinValue);
if (pinValue.length !== length && completeRef.current === true) {
completeRef.current = false;
if (onIncomplete) onIncomplete(pinValue);
}
}, [length, onComplete, onIncomplete, pinValue]);
}, [length, onComplete, onIncomplete, pinValue, pins, value]);
/* focus on first input field if autoFocus is set */
React.useEffect(() => {
Expand Down
15 changes: 9 additions & 6 deletions src/components/ui/pin-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ const PinInput = React.forwardRef<HTMLDivElement, PinInputProps>(
onChange(pinValue);
}, [onChange, pinValue]);

/* call onComplete func if pinValue is valid and completed */
/* call onComplete/onIncomplete func if pinValue is valid and completed/incompleted */
const completeRef = React.useRef(pinValue.length === length);
React.useEffect(() => {
if (onComplete && pinValue.length === length) {
onComplete(pinValue);
if (pinValue.length === length && completeRef.current === false) {
completeRef.current = true;
if (onComplete) onComplete(pinValue);
}
if (onIncomplete && pinValue.length !== length) {
onIncomplete(pinValue);
if (pinValue.length !== length && completeRef.current === true) {
completeRef.current = false;
if (onIncomplete) onIncomplete(pinValue);
}
}, [length, onComplete, onIncomplete, pinValue]);
}, [length, onComplete, onIncomplete, pinValue, pins, value]);

/* focus on first input field if autoFocus is set */
React.useEffect(() => {
Expand Down

0 comments on commit ffac428

Please sign in to comment.