-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventForm.js
141 lines (133 loc) · 4.19 KB
/
EventForm.js
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
import React, { useContext, useState } from "react";
import LanguageContext from "../2-context/LanguageContext";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBirthdayCake, faClock } from "@fortawesome/free-solid-svg-icons";
import {
addTextOptions,
charLimitTextOptions,
eventTitleTextOptions
} from "../3-data/siteText";
import Icons from "./Icons";
import TimeList from "./TimeList";
const EventForm = ({
errorMessage,
handleFormSubmit,
selectedEvent,
setShowIcons,
setShowTimeList,
showIcons,
showTimeList
}) => {
const { language } = useContext(LanguageContext);
const [titleInput, setTitleInput] = useState(
selectedEvent ? selectedEvent.title : ""
);
const [selectedIcon, setSelectedIcon] = useState(
selectedEvent ? selectedEvent.icon : ""
);
const [selectedTime, setSelectedTime] = useState(
selectedEvent ? selectedEvent.time : ""
);
const getFormValues = e => {
e.preventDefault();
handleFormSubmit(e, titleInput, selectedTime, selectedIcon);
};
const handleTitleInput = value => {
//char limit message also shown below input (see form below)
if (value.length > 40) {
return;
} else {
setTitleInput(value);
}
};
const handleSelectIcon = e => {
if (!e.target.classList.contains("icons__icon")) {
return;
} else {
setSelectedIcon(e.target.innerText);
setShowIcons(false);
}
};
const handleSelectTime = e => {
if (!e.target.classList.contains("time-list__time-option")) {
return;
} else {
setSelectedTime(e.target.innerText);
setShowTimeList(false);
}
};
return (
<div className="event-form">
<form className="event-form__form" onSubmit={getFormValues}>
<div className="event-form__preview">
{titleInput && (
<span className="event-form__title-preview"> {titleInput}</span>
)}
{selectedTime && (
<span className="event-form__time-preview">{selectedTime}</span>
)}
{selectedIcon && (
<span className="event-form__icon-preview">{selectedIcon}</span>
)}
</div>
<div className="event-form__form-fields">
<div className="event-form__form-field">
<input
id="event-title"
maxLength="40"
name="title"
onChange={e => handleTitleInput(e.target.value)}
placeholder={eventTitleTextOptions[language]}
type="text"
value={titleInput}
/>
<p
className={`event-form__char-limit-message${
titleInput.length > 39 ? "--show" : ""
}`}
>
{charLimitTextOptions[language]}
</p>
</div>
<div className="event-form__extra-inputs">
<button
className="event-form__clock"
title="Add time"
type="button"
onClick={() => setShowTimeList(!showTimeList)}
>
<FontAwesomeIcon icon={faClock} className="clock" />
</button>
{showTimeList && (
<TimeList
handleSelectTime={handleSelectTime}
setShowTimeList={setShowTimeList}
/>
)}
<button
className="event-form__icon-button"
title="Add icon"
type="button"
onClick={e => {
setShowIcons(!showIcons);
}}
>
<FontAwesomeIcon icon={faBirthdayCake} className="cake" />
</button>
{showIcons && (
<Icons
handleSelectIcon={handleSelectIcon}
setShowIcons={setShowIcons}
/>
)}
</div>
<div className="event-form__error-message">{errorMessage}</div>
</div>
<button className="event-form__button" type="submit">
{addTextOptions[language]}
</button>
</form>
</div>
);
};
export default EventForm;