1
1
import { useState } from 'react' ;
2
- import Error from './error' ;
3
2
import validateAndSanitizeCommentsForm from '../../validator/comments' ;
3
+ import TextArea from '../form-elements/text-area' ;
4
+ import Input from '../form-elements/input' ;
5
+ import Checkbox from '../form-elements/checkbox' ;
4
6
5
7
const CommentForm = ( ) => {
6
8
7
- const [ input , setInput ] = useState ( { } ) ;
9
+ const [ input , setInput ] = useState ( { wp_comment_cookies_consent : false } ) ;
8
10
9
11
const handleFormSubmit = ( event ) => {
10
12
event . preventDefault ( ) ;
11
13
12
14
const commentFormValidationResult = validateAndSanitizeCommentsForm ( input ) ;
13
- console . log ( 'commentFormValidationResult' , commentFormValidationResult , input ) ;
14
15
15
16
setInput ( {
16
17
...input ,
@@ -34,8 +35,8 @@ const CommentForm = () => {
34
35
*/
35
36
const handleOnChange = ( event ) => {
36
37
const { target } = event || { } ;
37
-
38
- const newState = { ...input , [ target . name ] : target . value } ;
38
+ console . log ( 'target.type' , target . type ) ;
39
+ const newState = 'checkbox' === target . type ? { ... input , [ target . name ] : ! input [ target . name ] } : { ...input , [ target . name ] : target . value } ;
39
40
setInput ( newState ) ;
40
41
} ;
41
42
@@ -46,95 +47,55 @@ const CommentForm = () => {
46
47
< span id = "email-notes" > Your email address will not be published.</ span >
47
48
< span className = "required-field-message" > Required fields are marked < span className = "required" > *</ span > </ span >
48
49
</ p >
49
- < div className = "comment-form-comment mb-2" >
50
- < label htmlFor = "comment" className = "block mb-2 font-medium text-gray-900 dark:text-white" >
51
- Comment
52
- < span className = "required" > *</ span >
53
- </ label >
54
- < textarea
55
- id = "comment"
56
- className = "bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
57
- name = "comment"
58
- cols = "45"
59
- rows = "5"
60
- maxLength = "65525"
61
- required
62
- value = { input ?. comment ?? '' }
63
- onChange = { ( event ) => handleOnChange ( event ) }
64
- />
65
- < Error errors = { input ?. errors ?? { } } fieldName = "comment" />
66
- </ div >
50
+ < TextArea
51
+ id = "comment"
52
+ containerClassNames = "comment-form-comment mb-2"
53
+ name = "comment"
54
+ label = "Comment"
55
+ cols = "45"
56
+ rows = "5"
57
+ required
58
+ textAreaValue = { input ?. comment ?? '' }
59
+ handleOnChange = { handleOnChange }
60
+ errors = { input ?. errors ?? { } }
61
+ />
67
62
< div className = "grid grid-cols-1 md:grid-cols-2 gap-4 mb-2" >
68
- < div className = "comment-form-author" >
69
- < label htmlFor = "author" className = "block mb-2 font-medium text-gray-900 dark:text-white" >
70
- Name
71
- < span className = "required" > *</ span >
72
- </ label >
73
- < input
74
- id = "author"
75
- className = "bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 w-full block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
76
- name = "author"
77
- type = "text"
78
- size = "30"
79
- maxLength = "245"
80
- autoComplete = "name"
81
- required = ""
82
- value = { input ?. author ?? '' }
83
- onChange = { ( event ) => handleOnChange ( event ) }
84
- />
85
- < Error errors = { input ?. errors ?? { } } fieldName = "author" />
86
- </ div >
87
- < div className = "comment-form-email border-0 mb-2" >
88
- < label htmlFor = "email" className = "block mb-2 font-medium text-gray-900 dark:text-white" >
89
- Email
90
- < span className = "required" > *</ span >
91
- </ label >
92
- < input
93
- id = "email"
94
- className = "bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 w-full block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
95
- name = "email"
96
- type = "email"
97
- size = "30"
98
- maxLength = "100"
99
- aria-describedby = "email-notes"
100
- autoComplete = "email"
101
- required
102
- value = { input ?. email ?? '' }
103
- onChange = { ( event ) => handleOnChange ( event ) }
104
- />
105
- < Error errors = { input ?. errors ?? { } } fieldName = "email" />
106
- </ div >
107
- </ div >
108
- < div className = "comment-form-url mb-2" >
109
- < label htmlFor = "url" className = "block mb-2 font-medium text-gray-900 dark:text-white" >
110
- Website
111
- </ label >
112
- < input
113
- id = "url"
114
- className = "bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
115
- name = "url"
116
- type = "url"
117
- size = "30"
118
- maxLength = "200"
119
- autoComplete = "url"
120
- value = { input ?. url ?? '' }
121
- onChange = { ( event ) => handleOnChange ( event ) }
63
+ < Input
64
+ name = "author"
65
+ inputValue = { input ?. author }
66
+ required
67
+ handleOnChange = { handleOnChange }
68
+ label = "Name"
69
+ errors = { input ?. errors ?? { } }
70
+ containerClassNames = "comment-form-author"
122
71
/>
123
- < Error errors = { input ?. errors ?? { } } fieldName = "url" />
124
- </ div >
125
- < div className = "comment-form-cookies-consent mb-4" >
126
- < input
127
- id = "wp-comment-cookies-consent"
128
- name = "wp_comment_cookies_consent"
129
- type = "checkbox"
130
- value = "yes"
131
- onChange = { ( event ) => handleOnChange ( event ) }
72
+ < Input
73
+ name = "email"
74
+ inputValue = { input ?. email }
75
+ required
76
+ handleOnChange = { handleOnChange }
77
+ label = "Email"
78
+ errors = { input ?. errors ?? { } }
79
+ containerClassNames = "comment-form-email mb-2"
132
80
/>
133
- < label htmlFor = "wp-comment-cookies-consent" className = "mb-2 ml-2 font-medium text-gray-900 dark:text-white" >
134
- Save my name, email, and website in this browser for the next time I comment.
135
- </ label >
136
- < Error errors = { input ?. errors ?? { } } fieldName = "wp_comment_cookies_consent" />
137
81
</ div >
82
+ < Input
83
+ name = "url"
84
+ inputValue = { input ?. url }
85
+ required
86
+ handleOnChange = { handleOnChange }
87
+ label = "Website"
88
+ errors = { input ?. errors ?? { } }
89
+ containerClassNames = "comment-form-url mb-2"
90
+ />
91
+ < Checkbox
92
+ name = "wp_comment_cookies_consent"
93
+ checked = { input . wp_comment_cookies_consent }
94
+ handleOnChange = { handleOnChange }
95
+ label = "Save my name, email, and website in this browser for the next time I comment."
96
+ errors = { input ?. errors ?? { } }
97
+ containerClassNames = "comment-form-url mb-2"
98
+ />
138
99
< div className = "form-submit" >
139
100
< input name = "submit" type = "submit" id = "submit" className = " cursor-pointer text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-16px uppercase w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" value = "Post Comment" />
140
101
< input type = "hidden" name = "comment_post_ID" value = "377" id = "comment_post_ID" />
0 commit comments