Skip to content

Commit 7d5498d

Browse files
ericnakagawagaearon
authored andcommitted
Forms Update (#8112)
* Reapplied fixes to updated docs from master * Reapplied fixes to Forms, removed ES2016 function includes() * Missing carriage return * Adding back some line breaks * Making requested changes. * Making space changes
1 parent e2c648e commit 7d5498d

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

docs/docs/forms.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ render() {
2626
}
2727
```
2828

29-
User input has no effect on the rendered element because React has declared the value to be "Hello!". To update the value in response to user input, you would use the onChange event:
29+
User input has no effect on the rendered element because React has declared the value to be "Hello!".
3030

31-
```javascript
31+
To update the value in response to user input, you would use the `onChange` event to save the new value, then pass that to the `value` prop of the form:
32+
33+
```javascript{10,22,23}
3234
class Form extends React.Component {
3335
constructor(props) {
3436
super(props);
@@ -91,9 +93,9 @@ An **uncontrolled** component manages its own state.
9193
}
9294
```
9395

94-
If you wanted to listen to updates to the value, you could use the `onChange` event just like you can with controlled components.
96+
If you wanted to listen to updates to the value, you could use the `onChange` event just like you can with controlled components. However, you would _not_ pass the value you saved to the component.
9597

96-
```javascript
98+
```javascript{10,22}
9799
class Form extends React.Component {
98100
constructor(props) {
99101
super(props);
@@ -115,7 +117,6 @@ class Form extends React.Component {
115117
<div>
116118
<input type="text"
117119
placeholder="Hello!"
118-
value={this.state.value}
119120
onChange={this.handleChange} />
120121
<button onClick={this.handleSubmit}>Submit</button>
121122
</div>
@@ -226,7 +227,7 @@ For instance, if you want to imperatively submit a form, one approach would be t
226227

227228
## Examples
228229

229-
### Basic Text Input
230+
### Basic Controlled Input
230231

231232
```javascript
232233
class Form extends React.Component {
@@ -236,7 +237,6 @@ class Form extends React.Component {
236237
this.handleChange = this.handleChange.bind(this);
237238
this.handleSubmit = this.handleSubmit.bind(this);
238239
}
239-
240240
handleChange(event) {
241241
this.setState({value: event.target.value});
242242
}
@@ -262,7 +262,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
262262
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/JRmZjz?editors=0010)
263263

264264

265-
### Basic Textarea
265+
### Basic Controlled Textarea
266266

267267
```javascript
268268
class Form extends React.Component {
@@ -300,7 +300,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
300300

301301
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/kkAQPp?editors=0010)
302302

303-
### Basic Select
303+
### Basic Uncontrolled Select
304304

305305
```javascript
306306
class Form extends React.Component {
@@ -360,9 +360,12 @@ class Form extends React.Component {
360360
render() {
361361
return (
362362
<div>
363-
<input type="radio" name="choice" value="A" onChange={this.handleChange} /> Option A<br />
364-
<input type="radio" name="choice" value="B" onChange={this.handleChange} defaultChecked={true} /> Option B<br />
365-
<input type="radio" name="choice" value="C" onChange={this.handleChange} /> Option C<br />
363+
<input type="radio" name="choice" value="A"
364+
onChange={this.handleChange} /> Option A<br />
365+
<input type="radio" name="choice" value="B"
366+
onChange={this.handleChange} defaultChecked={true} /> Option B<br />
367+
<input type="radio" name="choice" value="C"
368+
onChange={this.handleChange} /> Option C<br />
366369
<br />
367370
<button onClick={this.handleSubmit}>Submit</button>
368371
</div>
@@ -376,38 +379,41 @@ ReactDOM.render(<Form />, document.getElementById('root'));
376379
[Try this on CodePen.](https://codepen.io/ericnakagawa/pen/WGaYVg?editors=0010)
377380

378381

379-
### Basic Checkbox
382+
### Basic Uncontrolled Checkbox
380383

381384
```javascript
382385
class Form extends React.Component {
383386
constructor(props) {
384387
super(props);
385-
this.state = {checked: ["B"]};
388+
this.state = {checked: {"A": false, "B": true, "C": false}};
386389
this.handleChange = this.handleChange.bind(this);
387390
this.handleSubmit = this.handleSubmit.bind(this);
388391
}
389392

390393
handleChange(event) {
391-
let val = event.target.value;
392-
let checked = this.state.checked.slice(); // copy
393-
if(checked.includes(val)) {
394-
checked.splice(checked.indexOf(val), 1);
395-
} else {
396-
checked.push(val);
397-
}
394+
let value = event.target.value;
395+
let checked = this.state.checked; // copy
396+
if (!checked[value]) { checked[value] = true; } else { checked[value] = false; }
398397
this.setState({checked: checked})
399398
}
400399

401400
handleSubmit(event) {
402-
alert("Boxes checked are: '" + this.state.checked + "'");
401+
alert("Boxes checked: " +
402+
(this.state.checked.A ? "A " : "") +
403+
(this.state.checked.B ? "B " : "") +
404+
(this.state.checked.C ? "C" : "")
405+
);
403406
}
404407

405408
render() {
406409
return (
407410
<div>
408-
<input type="checkbox" value="A" onChange={this.handleChange} /> Option A<br />
409-
<input type="checkbox" value="B" onChange={this.handleChange} defaultChecked={true} /> Option B<br />
410-
<input type="checkbox" value="C" onChange={this.handleChange} /> Option C<br />
411+
<input type="checkbox" value="A"
412+
onChange={this.handleChange} /> Option A<br />
413+
<input type="checkbox" value="B"
414+
onChange={this.handleChange} defaultChecked={true} /> Option B<br />
415+
<input type="checkbox" value="C"
416+
onChange={this.handleChange} /> Option C<br />
411417
<br />
412418
<button onClick={this.handleSubmit}>Submit</button>
413419
</div>

0 commit comments

Comments
 (0)