Skip to content

Commit

Permalink
fix refocus in b/w address
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Feb 23, 2024
1 parent c8fbd2b commit c2f1cf3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/nextjs/components/scaffold-eth/Input/InputBase.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, ReactNode, useCallback, useEffect, useRef } from "react";
import { ChangeEvent, FocusEvent, ReactNode, useCallback, useEffect, useRef } from "react";
import { CommonInputProps } from "~~/components/scaffold-eth";

type InputBaseProps<T> = CommonInputProps<T> & {
Expand Down Expand Up @@ -35,8 +35,16 @@ export const InputBase = <T extends { toString: () => string } | undefined = str
[onChange],
);

// Runs only when reFocus prop is passed, usefull for setting the cursor
// at the end of the input. Example AddressInput
const onFocus = (e: FocusEvent<HTMLInputElement, Element>) => {
if (reFocus !== undefined) {
console.log("Running OnFocus", reFocus);
e.currentTarget.setSelectionRange(e.currentTarget.value.length, e.currentTarget.value.length);
}
};
useEffect(() => {
if (reFocus) inputReft.current?.focus();
if (reFocus !== undefined && reFocus === true) inputReft.current?.focus();
}, [reFocus]);

return (
Expand All @@ -51,6 +59,7 @@ export const InputBase = <T extends { toString: () => string } | undefined = str
disabled={disabled}
autoComplete="off"
ref={inputReft}
onFocus={onFocus}
/>
{suffix}
</div>
Expand Down

0 comments on commit c2f1cf3

Please sign in to comment.