Skip to content

Commit 380cfb2

Browse files
committed
chore(examples): add auto submit example
1 parent cb8c4b8 commit 380cfb2

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client'
2+
3+
import * as React from 'react'
4+
5+
import { Button } from '@/components/ui/button'
6+
import { formAction } from './server/form-action'
7+
import { ExampleComponent } from '../example-playground/component'
8+
9+
export default function ExampleAutoSubmit() {
10+
const formRef = React.useRef<HTMLFormElement>(null)
11+
12+
const [value, setValue] = React.useState('12')
13+
14+
return (
15+
<form ref={formRef} action={formAction}>
16+
<ExampleComponent
17+
name="otp"
18+
value={value}
19+
onChange={setValue}
20+
onComplete={() => formRef.current?.submit()}
21+
/>
22+
<Button type="submit">Submit</Button>
23+
</form>
24+
)
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use server'
2+
3+
export async function formAction(formData: FormData) {
4+
const rawFormData = {
5+
otp: formData.get('otp'),
6+
}
7+
8+
console.log({ rawFormData })
9+
}

apps/website/src/app/(local-pages)/example-playground/component.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
'use client'
22

3-
import { cn } from '@/lib/utils'
4-
3+
import * as React from 'react'
54
import { OTPInput, SlotProps } from 'input-otp'
65

7-
export function ExampleComponent() {
6+
import { cn } from '@/lib/utils'
7+
8+
export function ExampleComponent(
9+
props: Partial<React.ComponentProps<typeof OTPInput>>,
10+
) {
811
return (
912
<OTPInput
13+
{...props}
1014
maxLength={6}
1115
containerClassName="group flex items-center has-[:disabled]:opacity-30"
1216
render={({ slots }) => (

apps/website/src/app/(pages)/(home)/_components/showcase.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export function Showcase({ className, ...props }: { className?: string }) {
4848
}, [value.length])
4949

5050
async function onSubmit(e?: React.FormEvent<HTMLFormElement>) {
51+
e?.preventDefault?.()
52+
5153
inputRef.current?.select()
5254
await new Promise(r => setTimeout(r, 1_00))
5355

54-
e?.preventDefault?.()
55-
5656
if (value === '123456') {
5757
setHasGuessed(true)
5858

bun.lockb

-48 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)