forked from irinazheltisheva/fastsite-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddSiteForm.js
More file actions
49 lines (43 loc) · 1.38 KB
/
AddSiteForm.js
File metadata and controls
49 lines (43 loc) · 1.38 KB
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
import React, { useState } from 'react'
const AddSiteForm = props => {
const initialFormState = { id: '', name: '', heading: '', categories: '' }
const [ user, setUser ] = useState(initialFormState)
const handleInputChange = event => {
const { name, value } = event.target
setUser({ ...user, [name]: value })
}
return (
<form
onSubmit={event => {
event.preventDefault()
if (!user.id || !user.name){
alert("Please provide site id and name");
return
}
user.id = user.id.trim().toLowerCase();
if(!user.heading){
user.heading = user.name;
}
if(user.categories){
user.categories = user.categories.trim().split(",");
user.categories = user.categories.map(string => string.trim())
}else{
user.categories = []
}
props.addUser(user)
setUser(initialFormState)
}}
>
<label>Id</label>
<input type="text" name="id" value={user.id} onChange={handleInputChange} />
<label>Name</label>
<input type="text" name="name" value={user.name} onChange={handleInputChange} />
<label>Heading</label>
<input type="text" name="heading" value={user.heading} onChange={handleInputChange} />
<label>Categories</label>
<input type="text" name="categories" value={user.categories} onChange={handleInputChange} />
<button>Add new site</button>
</form>
)
}
export default AddSiteForm