-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathUserInput.js
More file actions
80 lines (64 loc) · 2.4 KB
/
UserInput.js
File metadata and controls
80 lines (64 loc) · 2.4 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
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
import { useState } from "react";
import classes from './UserInput.module.css'
const initialUserInput ={
'current-savings':10000,
'yearly-contribution':1200,
'expected-return':7,
'duration': 10,
};
const UserInput =(props)=>{
const [userInput,setUserInput]=useState(initialUserInput);
const submitHandler=(event)=>{
event.preventDefault();
console.log('Submit');
props.onCalculate(userInput);
}
const resetHandler=()=>{
console.log('Reset');
setUserInput(initialUserInput);
}
const inputChangeHandler=(input,value)=>{
console.log('Input');
setUserInput((prevInput)=>{
return{
...prevInput,
[input]:value,
}
})
}
return(
<form onSubmit={submitHandler} className={classes.form}>
<div className={classes['input-group']}>
<p>
<label htmlFor="current-savings">Current Savings ($)</label>
<input onChange={(event)=>inputChangeHandler('current-savings',event.target.value)} value={userInput['current-savings']} type="number" id="current-savings" />
</p>
<p>
<label htmlFor="yearly-contribution">Yearly Savings ($)</label>
<input onChange={(event)=>inputChangeHandler('yearly-contribution',event.target.value)} value={userInput['yearly-contribution']} type="number" id="yearly-contribution" />
</p>
</div>
<div className={classes['input-group']}>
<p>
<label htmlFor="expected-return">
Expected Interest (%, per year)
</label>
<input onChange={(event)=>inputChangeHandler('expected-return',event.target.value)} value={userInput['expected-return']} type="number" id="expected-return" />
</p>
<p>
<label htmlFor="duration">Investment Duration (years)</label>
<input onChange={(event)=>inputChangeHandler('duration',event.target.value)} value={userInput['duration']} type="number" id="duration" />
</p>
</div>
<p className={classes.actions}>
<button onClick={resetHandler} type="reset" className={classes.buttonAlt}>
Reset
</button>
<button type="submit" className={classes.button}>
Calculate
</button>
</p>
</form>
)
}
export default UserInput;