From a6ff5eee45215effb3344cb5d631a27a7c0369aa Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 22 Sep 2023 19:34:01 +0200 Subject: refactor(components): rewrite form components --- src/components/organisms/forms/comment-form.tsx | 193 ------------------------ 1 file changed, 193 deletions(-) delete mode 100644 src/components/organisms/forms/comment-form.tsx (limited to 'src/components/organisms/forms/comment-form.tsx') diff --git a/src/components/organisms/forms/comment-form.tsx b/src/components/organisms/forms/comment-form.tsx deleted file mode 100644 index e4140dd..0000000 --- a/src/components/organisms/forms/comment-form.tsx +++ /dev/null @@ -1,193 +0,0 @@ -import { FC, ReactNode, useState } from 'react'; -import { useIntl } from 'react-intl'; -import { - Button, - Form, - type FormProps, - Heading, - type HeadingLevel, - type HeadingProps, - Spinner, -} from '../../atoms'; -import { LabelledField } from '../../molecules'; -import styles from './comment-form.module.scss'; - -export type CommentFormData = { - comment: string; - email: string; - name: string; - parentId?: number; - website?: string; -}; - -export type CommentFormProps = Pick & { - /** - * Pass a component to print a success/error message. - */ - Notice?: ReactNode; - /** - * The comment parent id. - */ - parentId?: number; - /** - * A callback function to save comment. It takes a function as parameter to - * reset the form. - */ - saveComment: (data: CommentFormData, reset: () => void) => Promise; - /** - * The form title. - */ - title?: string; - /** - * The form title alignment. Default: left. - */ - titleAlignment?: HeadingProps['alignment']; - /** - * The title level. Default: 2. - */ - titleLevel?: HeadingLevel; -}; - -export const CommentForm: FC = ({ - Notice, - parentId, - saveComment, - title, - titleAlignment, - titleLevel = 2, - ...props -}) => { - const intl = useIntl(); - const [name, setName] = useState(''); - const [email, setEmail] = useState(''); - const [website, setWebsite] = useState(''); - const [comment, setComment] = useState(''); - const [isSubmitting, setIsSubmitting] = useState(false); - - /** - * Reset all the form fields. - */ - const resetForm = () => { - setName(''); - setEmail(''); - setWebsite(''); - setComment(''); - setIsSubmitting(false); - }; - - const nameLabel = intl.formatMessage({ - defaultMessage: 'Name:', - description: 'CommentForm: name label', - id: 'ZIrTee', - }); - - const emailLabel = intl.formatMessage({ - defaultMessage: 'Email:', - description: 'CommentForm: email label', - id: 'Bh7z5v', - }); - - const websiteLabel = intl.formatMessage({ - defaultMessage: 'Website:', - description: 'CommentForm: website label', - id: 'u41qSk', - }); - - const commentLabel = intl.formatMessage({ - defaultMessage: 'Comment:', - description: 'CommentForm: comment label', - id: 'A8hGaK', - }); - - const formTitle = intl.formatMessage({ - defaultMessage: 'Comment form', - description: 'CommentForm: aria label', - id: 'dz2kDV', - }); - - const formAriaLabel = title ? undefined : formTitle; - const formId = 'comment-form-title'; - const formLabelledBy = title ? formId : undefined; - - /** - * Handle form submit. - */ - const submitHandler = () => { - setIsSubmitting(true); - saveComment({ comment, email, name, parentId, website }, resetForm).then( - () => setIsSubmitting(false) - ); - }; - - return ( -
- {title && ( - - {title} - - )} - - - - - - {isSubmitting && ( - - )} - {Notice} - - ); -}; -- cgit v1.2.3