diff --git a/src/components/NewPostDialog.tsx b/src/components/NewPostDialog.tsx index 57dfcf37d..020d9a6f5 100644 --- a/src/components/NewPostDialog.tsx +++ b/src/components/NewPostDialog.tsx @@ -26,7 +26,9 @@ export const NewPostDialog = () => { const paramsObject = searchParamsToObject(searchParam); const path = usePathname(); const router = useRouter(); + const tagInputRef = useRef(null); const [value, setValue] = useState('**Hello world!!!**'); + const [tags, setTags] = useState([]); const containerRef = useRef(null); const { ref, onOpen, onClose } = useModal(); const handleMarkdownChange = (newValue?: string) => { @@ -54,6 +56,7 @@ export const NewPostDialog = () => { formRef?.current?.reset(); setValue(''); router.push(`/question/${data.slug}`); + setTags([]); handleOnCloseClick(); }, onError: (error) => { @@ -68,20 +71,41 @@ export const NewPostDialog = () => { setFieldErrors({}); } }; + const onSubmit = (event: React.FormEvent) => { event.preventDefault(); const formData = new FormData(event.currentTarget); const title = formData.get('title'); - - const tags = formData.get('tags'); - execute({ title: title?.toString() || '', content: value, - tags: (tags?.toString() || '').split(','), + tags, }); }; + const addTag = (event: React.KeyboardEvent) => { + if (event.key === ',') { + event.preventDefault(); + const formData = new FormData(formRef.current as HTMLFormElement); + const tag = formData.get('tags')?.toString().trim().replace(/,+$/, ''); + + if (tag) { + setTags((prevTags) => [ + ...prevTags, + tag + ]); + } + if (tagInputRef.current) { + tagInputRef.current.value = ''; + } + } + }; + + + const removeTag = (tag: string) => { + setTags(tags.filter((t) => t !== tag)); + }; + return (
@@ -130,12 +154,31 @@ export const NewPostDialog = () => {

Tags

- +
+
+ {tags.map((tag, index) => ( + + {tag} + removeTag(tag)} + /> + + ))} +
+ +
{ ); -}; +}; \ No newline at end of file diff --git a/src/components/posts/form/form-input.tsx b/src/components/posts/form/form-input.tsx index d68d11f9e..577bd81a9 100644 --- a/src/components/posts/form/form-input.tsx +++ b/src/components/posts/form/form-input.tsx @@ -1,5 +1,6 @@ 'use client'; +import React from 'react'; import { forwardRef } from 'react'; import { useFormStatus } from 'react-dom'; @@ -19,6 +20,7 @@ interface FormInputProps { className?: string; defaultValue?: string; onBlur?: () => void; + onKeyUp?: (event: React.KeyboardEvent) => void; } export const FormPostInput = forwardRef( @@ -33,6 +35,7 @@ export const FormPostInput = forwardRef( errors, className, defaultValue = '', + onKeyUp, onBlur, }, ref, @@ -57,6 +60,7 @@ export const FormPostInput = forwardRef( required={required} name={id} id={id} + onKeyUp={onKeyUp} placeholder={placeholder} type={type} disabled={pending || disabled} @@ -73,4 +77,4 @@ export const FormPostInput = forwardRef( }, ); -FormPostInput.displayName = 'FormPostInput'; +FormPostInput.displayName = 'FormPostInput'; \ No newline at end of file