Skip to content

Commit 708591c

Browse files
committed
Add extra solution for form exercise
1 parent d383066 commit 708591c

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Exercise
3+
//
4+
// - When the checkbox is checked:
5+
// - Fill in the shipping fields with the values from billing
6+
// - Disable the shipping fields so they are not directly editable
7+
// - Keep the shipping fields up to date as billing fields change
8+
// - Hint: you can get the checkbox value from `event.target.checked`
9+
// - When the form submits, console.log the values
10+
//
11+
// Got extra time?
12+
//
13+
// - If there are more than two characters in the "state" field, let the user
14+
// know they should use the two-character abbreviation
15+
// - Save the state of the form and restore it when the page first loads, in
16+
// case the user accidentally closes the tab before the form is submitted
17+
////////////////////////////////////////////////////////////////////////////////
18+
import React from "react";
19+
import ReactDOM from "react-dom";
20+
import serializeForm from "form-serialize";
21+
22+
class CheckoutForm extends React.Component {
23+
constructor(props) {
24+
super(props);
25+
26+
this.state = {
27+
shippingSameAsBilling: true,
28+
billingName: "",
29+
billingState: "",
30+
shippingName: "",
31+
shippingState: ""
32+
};
33+
34+
this.handleSubmit = event => {
35+
event.preventDefault();
36+
const values = serializeForm(event.target, { hash: true });
37+
console.log(values);
38+
};
39+
40+
this.handleBeforeUnload = () => {
41+
localStorage.formState = JSON.stringify(this.state);
42+
};
43+
44+
if (localStorage.formState) {
45+
Object.assign(this.state, JSON.parse(localStorage.formState));
46+
}
47+
}
48+
49+
componentDidMount() {
50+
window.addEventListener("beforeunload", this.handleBeforeUnload);
51+
}
52+
53+
componentWillUnmount() {
54+
window.removeEventListener("beforeunload", this.handleBeforeUnload);
55+
}
56+
57+
render() {
58+
return (
59+
<div>
60+
<h1>Checkout</h1>
61+
<form onSubmit={this.handleSubmit}>
62+
<fieldset>
63+
<legend>Billing Address</legend>
64+
<p>
65+
<label>
66+
Billing Name:{" "}
67+
<input
68+
type="text"
69+
name="billing_name"
70+
defaultValue={this.state.billingName}
71+
onChange={event =>
72+
this.setState({ billingName: event.target.value })
73+
}
74+
/>
75+
</label>
76+
</p>
77+
{this.state.billingState.length > 2 && (
78+
<p style={{ color: "red" }}>
79+
Please use the 2-character abbreviation for the state.
80+
</p>
81+
)}
82+
<p>
83+
<label>
84+
Billing State:{" "}
85+
<input
86+
type="text"
87+
size="2"
88+
name="billing_state"
89+
defaultValue={this.state.billingState}
90+
onChange={event =>
91+
this.setState({ billingState: event.target.value })
92+
}
93+
/>
94+
</label>
95+
</p>
96+
</fieldset>
97+
98+
<br />
99+
100+
<fieldset>
101+
<label>
102+
<input
103+
type="checkbox"
104+
defaultChecked={this.state.shippingSameAsBilling}
105+
onChange={event =>
106+
this.setState({
107+
shippingSameAsBilling: event.target.checked
108+
})
109+
}
110+
/>{" "}
111+
Same as billing
112+
</label>
113+
<legend>Shipping Address</legend>
114+
<p>
115+
<label>
116+
Shipping Name:{" "}
117+
<input
118+
type="text"
119+
name="shipping_name"
120+
value={
121+
this.state.shippingSameAsBilling
122+
? this.state.billingName
123+
: this.state.shippingName
124+
}
125+
onChange={event =>
126+
this.setState({ shippingName: event.target.value })
127+
}
128+
readOnly={this.state.shippingSameAsBilling}
129+
/>
130+
</label>
131+
</p>
132+
<p>
133+
<label>
134+
Shipping State:{" "}
135+
<input
136+
type="text"
137+
size="2"
138+
name="shipping_state"
139+
value={
140+
this.state.shippingSameAsBilling
141+
? this.state.billingState
142+
: this.state.shippingState
143+
}
144+
onChange={event =>
145+
this.setState({ shippingState: event.target.value })
146+
}
147+
readOnly={this.state.shippingSameAsBilling}
148+
/>
149+
</label>
150+
</p>
151+
</fieldset>
152+
153+
<p>
154+
<button>Submit</button>
155+
</p>
156+
</form>
157+
</div>
158+
);
159+
}
160+
}
161+
162+
ReactDOM.render(<CheckoutForm />, document.getElementById("app"));

0 commit comments

Comments
 (0)