1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
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<FormProps, 'className'> & {
/**
* 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<void>;
/**
* 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<CommentFormProps> = ({
Notice,
parentId,
saveComment,
title,
titleAlignment,
titleLevel = 2,
...props
}) => {
const intl = useIntl();
const [name, setName] = useState<string>('');
const [email, setEmail] = useState<string>('');
const [website, setWebsite] = useState<string>('');
const [comment, setComment] = useState<string>('');
const [isSubmitting, setIsSubmitting] = useState<boolean>(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 (
<Form
{...props}
aria-label={formAriaLabel}
aria-labelledby={formLabelledBy}
onSubmit={submitHandler}
>
{title && (
<Heading alignment={titleAlignment} id={formId} level={titleLevel}>
{title}
</Heading>
)}
<LabelledField
className={styles.field}
id="commenter-name"
label={nameLabel}
name="commenter-name"
required={true}
setValue={setName}
type="text"
value={name}
/>
<LabelledField
className={styles.field}
id="commenter-email"
label={emailLabel}
name="commenter-email"
required={true}
setValue={setEmail}
type="email"
value={email}
/>
<LabelledField
className={styles.field}
id="commenter-website"
label={websiteLabel}
name="commenter-website"
required={false}
setValue={setWebsite}
type="text"
value={website}
/>
<LabelledField
className={styles.field}
id="commenter-comment"
label={commentLabel}
name="commenter-comment"
required={true}
setValue={setComment}
type="textarea"
value={comment}
/>
<Button type="submit" kind="primary" className={styles.button}>
{intl.formatMessage({
defaultMessage: 'Publish',
description: 'CommentForm: submit button',
id: 'OL0Yzx',
})}
</Button>
{isSubmitting && (
<Spinner
message={intl.formatMessage({
defaultMessage: 'Submitting...',
description: 'CommentForm: spinner message on submit',
id: 'IY5ew6',
})}
/>
)}
{Notice}
</Form>
);
};
|