From b836f0a9f8b783e3328983ad087aa2b7b297b43a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 15 Apr 2022 14:38:54 +0200 Subject: chore: add a CommentForm component --- src/components/organisms/forms/comment-form.tsx | 174 ++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create 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 new file mode 100644 index 0000000..6acbf94 --- /dev/null +++ b/src/components/organisms/forms/comment-form.tsx @@ -0,0 +1,174 @@ +import Button from '@components/atoms/buttons/button'; +import Form from '@components/atoms/forms/form'; +import Heading, { type HeadingLevel } from '@components/atoms/headings/heading'; +import Spinner from '@components/atoms/loaders/spinner'; +import LabelledField from '@components/molecules/forms/labelled-field'; +import { ReactNode, useState, VFC } from 'react'; +import { useIntl } from 'react-intl'; +import styles from './comment-form.module.scss'; + +export type CommentFormProps = { + /** + * Set additional classnames to the form wrapper. + */ + className?: string; + /** + * Pass a component to print a success/error message. + */ + Notice?: ReactNode; + /** + * A callback function to save comment. It takes a function as parameter to + * reset the form. + */ + saveComment: (reset: () => void) => void; + /** + * The form title. + */ + title?: string; + /** + * The title level. + */ + titleLevel?: HeadingLevel; +}; + +const CommentForm: VFC = ({ + className = '', + Notice, + saveComment, + title, + titleLevel = 2, +}) => { + 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(resetForm); + }; + + return ( +
+ {title && ( + + {title} + + )} + + + + + + {isSubmitting && ( + + )} + {Notice} + + ); +}; + +export default CommentForm; -- cgit v1.2.3