how to auto focus input otp shadcn element
Unanswered
Barbary Lion posted this in #help-forum
Barbary LionOP
I added
tabIndex={0} to it and now it auto focuses using a useEffect on first render but it doesn't display the cursor.. can someone tell me the right way you are supposed to do this?<InputOTPSlot autoFocus ref={firstInputRef} index={0} />35 Replies
@Ray try passing `autoFocus` to `<InputOTP />`
Barbary LionOP
that didn't work either
probabaly because of this: but I can't edit this state in the parent
probabaly because of this: but I can't edit this state in the parent
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-2/3 w-px animate-caret-blink bg-foreground duration-1000" />
</div>
)}@Barbary Lion that didn't work either
probabaly because of this: but I can't edit this state in the parent
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-2/3 w-px animate-caret-blink bg-foreground duration-1000" />
</div>
)}
what is this? could you show the full code
@Ray what is this? could you show the full code
Barbary LionOP
this is the input-otp.tsx
https://github.com/shadcn-ui/ui/blob/f47bb973bebaa38cb1bda8c56076ec962516a9bb/apps/www/registry/default/ui/input-otp.tsx#L4
The reason focus does not place a cursor is because it's not actually a cursor its just a div, but I can't manipulate the fake Caret state in the parent
https://github.com/shadcn-ui/ui/blob/f47bb973bebaa38cb1bda8c56076ec962516a9bb/apps/www/registry/default/ui/input-otp.tsx#L4
The reason focus does not place a cursor is because it's not actually a cursor its just a div, but I can't manipulate the fake Caret state in the parent
@Barbary Lion this is the input-otp.tsx
https://github.com/shadcn-ui/ui/blob/f47bb973bebaa38cb1bda8c56076ec962516a9bb/apps/www/registry/default/ui/input-otp.tsx#L4
The reason focus does not place a cursor is because it's not actually a cursor its just a div, but I can't manipulate the fake Caret state in the parent
I know this is shadcn
this code work out of the box
"use client";
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from "@/components/ui/input-otp";
export function InputOTPDemo() {
return (
<InputOTP maxLength={6} autoFocus>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
);
}this code work out of the box
Barbary LionOP
... But that doesn't auto focus
so you have to show the code on how are you rendering the input
@Barbary Lion that didn't work either
probabaly because of this: but I can't edit this state in the parent
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-2/3 w-px animate-caret-blink bg-foreground duration-1000" />
</div>
)}
I don't see you are rendering input-otp here
@Ray I don't see you are rendering input-otp here
Barbary LionOP
I think you misunderstand my problem
I am rendering it fine, the values update no problem, and it links to my db fine as well
the problem is I can't focus it on render, because its a fake cursor, its two divs which allow the caret to have an animation
Thats why when you focus it it only has an outline but the caret doesn't show up
and there is no way to update the state outside of the parent
Barbary LionOP
You're clicking on it right?
When you open the page, it doesn't do that
?
I hit refresh
Barbary LionOP
wtf
bro it doesnt work for me
Barbary LionOP
that is hella weird
ima keep messing with it
thanks now I know it works
you have to pass autoFocus to
<InputOTP /><InputOTPSlot /> is a div
Barbary LionOP
i did bro
didnt do anything
<InputOTP /> is the input
Barbary LionOP
Im using tauri so idk maybe its different
I literally copy and pasted the shadcn thing in
well consider showing your code, otherwise I can't help
@Ray well consider showing your code, otherwise I can't help
Barbary LionOP
its the same code you sent. Ive tried auto focus in every element
But I have another component I already made so its all good, thx for the help
oh ok