Skip to content

Commit 4b27587

Browse files
committed
Fix bug in mobile, when typing in a
form and hitting enter doesn't show validation errors
1 parent f7a6375 commit 4b27587

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

client/src/components/Blog/Comments/CommentFormView.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from "react";
1+
import React, { useRef, useEffect } from "react";
22
import PropTypes from "prop-types";
33
import styled from "styled-components/macro";
44
import FormBase from "../../Form";
@@ -11,8 +11,13 @@ const Form = styled(FormBase)`
1111
padding: 10px;
1212
`;
1313

14-
const CommentForm = ({ onComment, pending }) => {
14+
const CommentForm = ({ onComment, pending, toggleableOpen }) => {
1515
const formRef = useRef();
16+
useEffect(() => {
17+
if (toggleableOpen) {
18+
formRef.current.focus();
19+
}
20+
}, [toggleableOpen]);
1621

1722
const handleSubmit = async ({ comment }) => {
1823
const action = await onComment(comment);
@@ -31,6 +36,7 @@ const CommentForm = ({ onComment, pending }) => {
3136
valid={validity.comment}
3237
error={errors.comment}
3338
placeholder="Comment on this blog"
39+
autoComplete="off"
3440
/>
3541
</Form.Group>
3642
<Row cols={1}>
@@ -51,7 +57,7 @@ const CommentForm = ({ onComment, pending }) => {
5157

5258
CommentForm.propTypes = {
5359
onComment: PropTypes.func.isRequired,
54-
pending: PropTypes.bool
60+
pending: PropTypes.bool,
5561
};
5662

5763
export default CommentForm;

client/src/components/Blog/Comments/CommentsContainer.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212
Heading,
1313
Actions,
1414
SortDropdown,
15-
Toggleable
15+
Toggleable,
1616
} from "./styled/CommentsContainer";
1717

1818
const SORT_OPTIONS = [
1919
{ value: "createdAt-desc", title: "By newest first" },
20-
{ value: "createdAt-asc", title: "By oldest First" }
20+
{ value: "createdAt-asc", title: "By oldest First" },
2121
];
2222

2323
const SORT_BY_NEWEST = SORT_OPTIONS[0].value;
@@ -30,7 +30,7 @@ const CommentsContainer = ({
3030
fetchComments,
3131
page,
3232
history,
33-
location
33+
location,
3434
}) => {
3535
const commentFormRef = useRef();
3636
useEffect(() => {
@@ -39,7 +39,7 @@ const CommentsContainer = ({
3939
const sort = params.get("sort") || SORT_BY_NEWEST;
4040
fetchComments({ blogId: match.params.id, page, sort });
4141
}, [fetchComments, match.params.id, location]);
42-
const handleComment = comment =>
42+
const handleComment = (comment) =>
4343
addComment(match.params.id, comment, history);
4444
const handleChange = (type, value) => {
4545
const params = new URLSearchParams(location.search);
@@ -59,7 +59,7 @@ const CommentsContainer = ({
5959
<Actions>
6060
<SortDropdown
6161
options={SORT_OPTIONS}
62-
onChange={sort => handleChange("sort", sort)}
62+
onChange={(sort) => handleChange("sort", sort)}
6363
defaultValue={
6464
new URLSearchParams(location.search).get("sort") || SORT_BY_NEWEST
6565
}
@@ -69,25 +69,31 @@ const CommentsContainer = ({
6969
buttonIcon={AddIcon}
7070
ref={commentFormRef}
7171
>
72-
<CommentForm onComment={handleComment} pending={isCommenting} />
72+
{(open) => (
73+
<CommentForm
74+
onComment={handleComment}
75+
pending={isCommenting}
76+
toggleableOpen={open}
77+
/>
78+
)}
7379
</Toggleable>
7480
</Actions>
7581
<CommentList comments={page?.items} pending={pending} />
7682
<Pager
7783
currentPage={page?.currentPage}
7884
lastPage={page?.lastPage}
7985
maxNavPages={6}
80-
onClick={page => handleChange("page", page)}
86+
onClick={(page) => handleChange("page", page)}
8187
pending={pending}
8288
/>
8389
</div>
8490
);
8591
};
8692

87-
const mapStateToProps = state => ({
93+
const mapStateToProps = (state) => ({
8894
isCommenting: selectors.getPending(state, actionTypes.ADD_COMMENT_REQUEST),
8995
isFetching: selectors.getPending(state, actionTypes.FETCH_COMMENTS_REQUEST),
90-
page: selectors.getPage(state, actionTypes.FETCH_COMMENTS_REQUEST)
96+
page: selectors.getPage(state, actionTypes.FETCH_COMMENTS_REQUEST),
9197
});
9298

9399
export default connect(mapStateToProps, { addComment, fetchComments })(

client/src/components/Form/Control/ControlView.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const ControlView = ({
99
value,
1010
error,
1111
onChange,
12+
onKeyPress,
1213
valid,
1314
id,
1415
type,
1516
noValidStyle,
16-
placeholder
17+
placeholder,
18+
autoFocus,
19+
autoComplete,
1720
}) => {
1821
return (
1922
<>
@@ -22,8 +25,11 @@ const ControlView = ({
2225
id={id}
2326
value={value}
2427
onChange={onChange}
28+
onKeyPress={onKeyPress}
2529
valid={valid}
2630
placeholder={placeholder}
31+
autoFocus={autoFocus}
32+
autoComplete={autoComplete}
2733
/>
2834
) : (
2935
<Input
@@ -33,8 +39,11 @@ const ControlView = ({
3339
onClear={onClear}
3440
value={value}
3541
onChange={onChange}
42+
onKeyPress={onKeyPress}
3643
valid={valid}
3744
placeholder={placeholder}
45+
autoFocus={autoFocus}
46+
autoComplete={autoComplete}
3847
/>
3948
)}
4049
<InputError>{error}</InputError>
@@ -49,7 +58,7 @@ ControlView.propTypes = {
4958
value: PropTypes.string.isRequired,
5059
valid: PropTypes.bool,
5160
error: PropTypes.string,
52-
id: PropTypes.string
61+
id: PropTypes.string,
5362
};
5463

5564
export default ControlView;

client/src/components/Login/LoginFormView.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const LoginFormView = ({ onLogin, pending }) => {
1414
const handleSubmit = ({ username, password }) => {
1515
onLogin({
1616
username,
17-
password
17+
password,
1818
});
1919
};
2020

@@ -71,7 +71,7 @@ const LoginFormView = ({ onLogin, pending }) => {
7171

7272
LoginFormView.propTypes = {
7373
onLogin: PropTypes.func.isRequired,
74-
pending: PropTypes.bool
74+
pending: PropTypes.bool,
7575
};
7676

7777
export default LoginFormView;

0 commit comments

Comments
 (0)