-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from aXenDeveloper/tag_input
feat(frontend): Add tag input
- Loading branch information
Showing
6 changed files
with
201 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: Tag Input | ||
description: Tag Input is a component that allows users to input tags. | ||
--- | ||
|
||
## Preview | ||
|
||
Image | ||
|
||
## Props | ||
|
||
| Name | Type | Default | Description | | ||
| ----------- | --------- | ------- | -------------------- | | ||
| placeholder | `string` | | Placeholder text. | | ||
| multiple | `boolean` | false | Allow multiple tags. | | ||
| disabled | `boolean` | false | Disable input. | | ||
|
||
## Usage | ||
|
||
```tsx | ||
const formSchema = z.object({ | ||
tag_input_test: z.object({ id: z.number(), value: z.string() }), | ||
}); | ||
``` | ||
|
||
```tsx | ||
import { TagInput } from 'vitnode-frontend/components/ui/tag-input'; | ||
``` | ||
|
||
```tsx | ||
<FormField | ||
control={form.control} | ||
name="tag_input_test" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Tag Input Test</FormLabel> | ||
<FormControl> | ||
<TagInput {...field} /> // [!code highlight] | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
``` | ||
|
||
### Multiple | ||
|
||
```tsx | ||
const formSchema = z.object({ | ||
tag_input_test: z.array(z.object({ id: z.number(), value: z.string() })), | ||
}); | ||
``` | ||
|
||
```tsx | ||
import { TagInput } from 'vitnode-frontend/components/ui/tag-input'; | ||
``` | ||
|
||
```tsx | ||
<FormField | ||
control={form.control} | ||
name="tag_input_test" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Tag Input Test</FormLabel> | ||
<FormControl> | ||
<TagInput {...field} multiple /> // [!code highlight] | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
``` | ||
|
||
import { placeholder } from 'drizzle-orm'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import { X } from 'lucide-react'; | ||
import { AnimatePresence, motion } from 'framer-motion'; | ||
|
||
import { badgeVariants } from './badge'; | ||
import { Input } from './input'; | ||
|
||
interface TagInputItemProps { | ||
id: number | string; | ||
value: string; | ||
} | ||
|
||
interface Props | ||
extends Omit<React.HTMLAttributes<HTMLInputElement>, 'onChange'> { | ||
onChange: (value?: TagInputItemProps | TagInputItemProps[]) => void; | ||
className?: string; | ||
disabled?: boolean; | ||
} | ||
|
||
interface MultiProps extends Props { | ||
multiple?: true; | ||
value?: TagInputItemProps[]; | ||
} | ||
|
||
interface SingleProps extends Props { | ||
multiple?: never; | ||
value?: TagInputItemProps; | ||
} | ||
|
||
export const TagInput = ({ | ||
multiple, | ||
onChange, | ||
value: valueFromProps, | ||
disabled, | ||
...rest | ||
}: MultiProps | SingleProps) => { | ||
const values: TagInputItemProps[] = Array.isArray(valueFromProps) | ||
? valueFromProps | ||
: valueFromProps | ||
? [valueFromProps] | ||
: []; | ||
const [textInput, setTextInput] = React.useState(''); | ||
|
||
return ( | ||
<div className="space-y-3"> | ||
{values.length > 0 && ( | ||
<div className="flex flex-wrap items-center gap-2"> | ||
<AnimatePresence> | ||
{values.map(item => { | ||
const onRemove = () => { | ||
if (multiple) { | ||
onChange(values.filter(value => value.id !== item.id)); | ||
|
||
return; | ||
} | ||
|
||
onChange(); | ||
}; | ||
|
||
return ( | ||
<motion.div | ||
initial={{ opacity: 0, scale: 0.5 }} | ||
animate={{ opacity: 1, scale: 1 }} | ||
exit={{ opacity: 0, scale: 0.5 }} | ||
layout | ||
className={badgeVariants({ | ||
variant: 'outline', | ||
className: 'shrink-0 cursor-pointer [&>svg]:size-4', | ||
})} | ||
key={item.id} | ||
tabIndex={0} | ||
onClick={e => { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
onRemove(); | ||
}} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter') { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
onRemove(); | ||
} | ||
}} | ||
> | ||
{item.value} <X /> | ||
</motion.div> | ||
); | ||
})} | ||
</AnimatePresence> | ||
</div> | ||
)} | ||
|
||
<Input | ||
onChange={e => setTextInput(e.target.value)} | ||
value={textInput} | ||
disabled={(!multiple && values.length > 0) || disabled} | ||
onKeyDown={e => { | ||
if ((e.key === 'Enter' || e.key === ',') && textInput) { | ||
e.preventDefault(); | ||
const items = textInput.split(',').map(value => value.trim()); | ||
|
||
onChange([ | ||
...values, | ||
...items.map(value => ({ | ||
id: Math.random(), | ||
value, | ||
})), | ||
]); | ||
setTextInput(''); | ||
} | ||
}} | ||
{...rest} | ||
type="text" | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters