Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pin input #325

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/www/.vitepress/theme/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ export const docsConfig: DocsConfig = {
href: '/docs/components/pagination',
items: [],
},
{
title: 'Pin Input',
href: '/docs/components/pin-input',
label: 'New',
items: [],
},
{
title: 'Popover',
href: '/docs/components/popover',
Expand Down
28 changes: 28 additions & 0 deletions apps/www/__registry__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,20 @@ export const Index = {
component: () => import('../src/lib/registry/default/example/PaginationDemo.vue').then(m => m.default),
files: ['../src/lib/registry/default/example/PaginationDemo.vue'],
},
PinInputDemo: {
name: 'PinInputDemo',
type: 'components:example',
registryDependencies: ['pin-input'],
component: () => import('../src/lib/registry/default/example/PinInputDemo.vue').then(m => m.default),
files: ['../src/lib/registry/default/example/PinInputDemo.vue'],
},
PinInputFormDemo: {
name: 'PinInputFormDemo',
type: 'components:example',
registryDependencies: ['pin-input', 'button', 'form', 'toast'],
component: () => import('../src/lib/registry/default/example/PinInputFormDemo.vue').then(m => m.default),
files: ['../src/lib/registry/default/example/PinInputFormDemo.vue'],
},
PopoverDemo: {
name: 'PopoverDemo',
type: 'components:example',
Expand Down Expand Up @@ -1390,6 +1404,20 @@ export const Index = {
component: () => import('../src/lib/registry/new-york/example/PaginationDemo.vue').then(m => m.default),
files: ['../src/lib/registry/new-york/example/PaginationDemo.vue'],
},
PinInputDemo: {
name: 'PinInputDemo',
type: 'components:example',
registryDependencies: ['pin-input'],
component: () => import('../src/lib/registry/new-york/example/PinInputDemo.vue').then(m => m.default),
files: ['../src/lib/registry/new-york/example/PinInputDemo.vue'],
},
PinInputFormDemo: {
name: 'PinInputFormDemo',
type: 'components:example',
registryDependencies: ['pin-input', 'button', 'form', 'toast'],
component: () => import('../src/lib/registry/new-york/example/PinInputFormDemo.vue').then(m => m.default),
files: ['../src/lib/registry/new-york/example/PinInputFormDemo.vue'],
},
PopoverDemo: {
name: 'PopoverDemo',
type: 'components:example',
Expand Down
21 changes: 21 additions & 0 deletions apps/www/src/content/docs/components/pin-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: PIN Input
description: Allows users to input a sequence of one-character alphanumeric inputs.
source: apps/www/src/lib/registry/default/ui/pin-input
primitive: https://www.radix-vue.com/components/pin-input.html
---

<ComponentPreview name="PinInputDemo" />


## Installation

```bash
npx shadcn-vue@latest add pin-input
```

## Usage

### Form

<ComponentPreview name="PinInputFormDemo" />
28 changes: 28 additions & 0 deletions apps/www/src/lib/registry/default/example/PinInputDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script setup lang="ts">
import { ref } from 'vue'
import {
PinInput,
PinInputInput,
} from '@/lib/registry/default/ui/pin-input'

const value = ref<string[]>([])
const handleComplete = (e: string[]) => alert(e.join(''))
</script>

<template>
<div>
<PinInput
id="pin-input"
v-model="value"
placeholder="○"
class="flex gap-2 items-center mt-1"
@complete="handleComplete"
>
<PinInputInput
v-for="(id, index) in 5"
:key="id"
:index="index"
/>
</PinInput>
</div>
</template>
78 changes: 78 additions & 0 deletions apps/www/src/lib/registry/default/example/PinInputFormDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<script setup lang="ts">
import { h } from 'vue'
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import * as z from 'zod'
import {
PinInput,
PinInputInput,
} from '@/lib/registry/new-york/ui/pin-input'
import { Button } from '@/lib/registry/default/ui/button'
import {
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/lib/registry/default/ui/form'
import { toast } from '@/lib/registry/default/ui/toast'

const formSchema = toTypedSchema(z.object({
pin: z.array(z.string().min(1)).length(5),
}))

const { handleSubmit, setValues } = useForm({
validationSchema: formSchema,
initialValues: {
pin: [],
},
})

const onSubmit = handleSubmit(({ pin }) => {
toast({
title: 'You submitted the following values:',
description: h('pre', { class: 'mt-2 w-[340px] rounded-md bg-slate-950 p-4' }, h('code', { class: 'text-white' }, JSON.stringify(pin.join(''), null, 2))),
})
})

const handleComplete = (e: string[]) => console.log(e.join(''))
</script>

<template>
<form class="w-2/3 space-y-6 mx-auto" @submit="onSubmit">
<FormField v-slot="{ componentField }" name="pin">
<FormItem>
<FormLabel>Otp</FormLabel>
<FormControl>
<PinInput
id="pin-input"
placeholder="○"
class="flex gap-2 items-center mt-1"
otp
type="number"
:name="componentField.name"
@complete="handleComplete"
@update:model-value="(arrStr) => {
setValues({
pin: arrStr,
})
}"
>
<PinInputInput
v-for="(id, index) in 5"
:key="id"
:index="index"
/>
</PinInput>
</FormControl>
<FormDescription>
Allows users to input a sequence of one-character alphanumeric inputs.
</FormDescription>
<FormMessage />
</FormItem>
</FormField>

<Button>Submit</Button>
</form>
</template>
21 changes: 21 additions & 0 deletions apps/www/src/lib/registry/default/ui/pin-input/PinInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup lang="ts">
import { type HTMLAttributes, computed } from 'vue'
import { PinInputRoot, type PinInputRootEmits, type PinInputRootProps, useForwardPropsEmits } from 'radix-vue'
import { cn } from '@/lib/utils'

const props = defineProps<PinInputRootProps & { class?: HTMLAttributes['class'] }>()
const emits = defineEmits<PinInputRootEmits>()

const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})

const forwarded = useForwardPropsEmits(delegatedProps, emits)
</script>

<template>
<PinInputRoot v-bind="forwarded" :class="cn('flex gap-2 items-center', props.class)">
<slot />
</PinInputRoot>
</template>
18 changes: 18 additions & 0 deletions apps/www/src/lib/registry/default/ui/pin-input/PinInputInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script setup lang="ts">
import { type HTMLAttributes, computed } from 'vue'
import { PinInputInput, type PinInputInputProps, useForwardProps } from 'radix-vue'
import { cn } from '@/lib/utils'

const props = defineProps<PinInputInputProps & { class?: HTMLAttributes['class'] }>()

const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})

const forwardedProps = useForwardProps(delegatedProps)
</script>

<template>
<PinInputInput v-bind="forwardedProps" :class="cn('flex w-10 h-10 text-center rounded-md border border-input bg-background text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50', props.class)" />
</template>
2 changes: 2 additions & 0 deletions apps/www/src/lib/registry/default/ui/pin-input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as PinInput } from './PinInput.vue'
export { default as PinInputInput } from './PinInputInput.vue'
28 changes: 28 additions & 0 deletions apps/www/src/lib/registry/new-york/example/PinInputDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script setup lang="ts">
import { ref } from 'vue'
import {
PinInput,
PinInputInput,
} from '@/lib/registry/new-york/ui/pin-input'

const value = ref<string[]>([])
function handleComplete() {
console.log('212121')
}
</script>

<template>
<PinInput
id="pin-input"
v-model="value"
placeholder="○"
class="flex gap-2 items-center mt-1"
@complete="handleComplete"
>
<PinInputInput
v-for="(id, index) in 5"
:key="id"
:index="index"
/>
</PinInput>
</template>
78 changes: 78 additions & 0 deletions apps/www/src/lib/registry/new-york/example/PinInputFormDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<script setup lang="ts">
import { h } from 'vue'
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import * as z from 'zod'
import {
PinInput,
PinInputInput,
} from '@/lib/registry/new-york/ui/pin-input'
import { Button } from '@/lib/registry/new-york/ui/button'
import {
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/lib/registry/new-york/ui/form'
import { toast } from '@/lib/registry/new-york/ui/toast'

const formSchema = toTypedSchema(z.object({
pin: z.array(z.string().min(1)).length(5),
zernonia marked this conversation as resolved.
Show resolved Hide resolved
}))

const { handleSubmit, setValues } = useForm({
validationSchema: formSchema,
initialValues: {
pin: [],
},
})

const onSubmit = handleSubmit(({ pin }) => {
toast({
title: 'You submitted the following values:',
description: h('pre', { class: 'mt-2 w-[340px] rounded-md bg-slate-950 p-4' }, h('code', { class: 'text-white' }, JSON.stringify(pin.join(''), null, 2))),
})
})

const handleComplete = (e: string[]) => console.log(e.join(''))
</script>

<template>
<form class="w-2/3 space-y-6 mx-auto" @submit="onSubmit">
<FormField v-slot="{ componentField }" name="pin">
<FormItem>
<FormLabel>Otp</FormLabel>
<FormControl>
<PinInput
id="pin-input"
placeholder="○"
class="flex gap-2 items-center mt-1"
otp
type="number"
:name="componentField.name"
@complete="handleComplete"
@update:model-value="(arrStr) => {
setValues({
pin: arrStr,
})
}"
>
<PinInputInput
v-for="(id, index) in 5"
:key="id"
:index="index"
/>
</PinInput>
</FormControl>
<FormDescription>
Allows users to input a sequence of one-character alphanumeric inputs.
</FormDescription>
<FormMessage />
</FormItem>
</FormField>

<Button>Submit</Button>
</form>
</template>
21 changes: 21 additions & 0 deletions apps/www/src/lib/registry/new-york/ui/pin-input/PinInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup lang="ts">
import { type HTMLAttributes, computed } from 'vue'
import { PinInputRoot, type PinInputRootEmits, type PinInputRootProps, useForwardPropsEmits } from 'radix-vue'
import { cn } from '@/lib/utils'

const props = defineProps<PinInputRootProps & { class?: HTMLAttributes['class'] }>()
const emits = defineEmits<PinInputRootEmits>()

const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})

const forwarded = useForwardPropsEmits(delegatedProps, emits)
</script>

<template>
<PinInputRoot v-bind="forwarded" :class="cn('flex gap-2 items-center', props.class)">
<slot />
</PinInputRoot>
</template>
18 changes: 18 additions & 0 deletions apps/www/src/lib/registry/new-york/ui/pin-input/PinInputInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script setup lang="ts">
import { type HTMLAttributes, computed } from 'vue'
import { PinInputInput, type PinInputInputProps, useForwardProps } from 'radix-vue'
import { cn } from '@/lib/utils'

const props = defineProps<PinInputInputProps & { class?: HTMLAttributes['class'] }>()

const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})

const forwardedProps = useForwardProps(delegatedProps)
</script>

<template>
<PinInputInput v-bind="forwardedProps" :class="cn('flex w-10 h-10 text-center rounded-md border border-input bg-background text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50', props.class)" />
</template>
2 changes: 2 additions & 0 deletions apps/www/src/lib/registry/new-york/ui/pin-input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as PinInput } from './PinInput.vue'
export { default as PinInputInput } from './PinInputInput.vue'
Loading