Skip to content

Commit 552033e

Browse files
ericnakagawagaearon
authored andcommitted
Docs update - Additional (#8115)
* 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 * Fixed typo and removed unnecessary hyphen. * Reworded select, and highlighted line * Fixed string styles * Refactored <label> to use htmlFor * Another refactor of <label> * Removed name prop from radiobutton
1 parent 47e665a commit 552033e

File tree

1 file changed

+59
-34
lines changed

1 file changed

+59
-34
lines changed

docs/docs/forms.md

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To update the value in response to user input, you would use the `onChange` even
3434
class Form extends React.Component {
3535
constructor(props) {
3636
super(props);
37-
this.state = {value: ""};
37+
this.state = {value: ''};
3838
this.handleChange = this.handleChange.bind(this);
3939
this.handleSubmit = this.handleSubmit.bind(this);
4040
}
@@ -44,7 +44,7 @@ class Form extends React.Component {
4444
}
4545
4646
handleSubmit(event) {
47-
alert("Text field value is: '" + this.state.value + "'");
47+
alert('Text field value is: ' + this.state.value);
4848
}
4949
5050
render() {
@@ -99,7 +99,7 @@ If you wanted to listen to updates to the value, you could use the `onChange` ev
9999
class Form extends React.Component {
100100
constructor(props) {
101101
super(props);
102-
this.state = {value: ""};
102+
this.state = {value: ''};
103103
this.handleChange = this.handleChange.bind(this);
104104
this.handleSubmit = this.handleSubmit.bind(this);
105105
}
@@ -109,7 +109,7 @@ class Form extends React.Component {
109109
}
110110
111111
handleSubmit(event) {
112-
alert("Text field value is: '" + this.state.value + "'");
112+
alert('Text field value is: ' + this.state.value);
113113
}
114114
115115
render() {
@@ -161,7 +161,7 @@ Like all DOM events, the `onChange` prop is supported on all native components a
161161

162162
> Note:
163163
>
164-
> For `<input>` and `<textarea>`, `onChange` should generally used instead of the DOM's built-in [`oninput`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/oninput) event handler.
164+
> For `<input>` and `<textarea>`, `onChange` should generally be used instead of the DOM's built-in [`oninput`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/oninput) event handler.
165165
166166
## Advanced Topics
167167

@@ -204,9 +204,9 @@ If you *do* decide to use children, they will behave like `defaultValue`.
204204

205205
### Why Select Value?
206206

207-
The selected `<option>` in an HTML `<select>` is normally specified through that option's `selected` attribute. In React, in order to make components easier to manipulate, the following format is adopted instead:
207+
The selected `<option>` in an HTML `<select>` is normally specified through that option's `selected` attribute. In React we assign the `select` component a specific value by passing a `value` prop:
208208

209-
```javascript
209+
```javascript{1}
210210
<select value="B">
211211
<option value="A">Apple</option>
212212
<option value="B">Banana</option>
@@ -233,7 +233,7 @@ For instance, if you want to imperatively submit a form, one approach would be t
233233
class Form extends React.Component {
234234
constructor(props) {
235235
super(props);
236-
this.state = {value: ""};
236+
this.state = {value: ''};
237237
this.handleChange = this.handleChange.bind(this);
238238
this.handleSubmit = this.handleSubmit.bind(this);
239239
}
@@ -242,14 +242,18 @@ class Form extends React.Component {
242242
}
243243

244244
handleSubmit(event) {
245-
alert("Text field value is: '" + this.state.value + "'");
245+
alert('Text field value is: ' + this.state.value);
246246
}
247247

248248
render() {
249249
return (
250250
<div>
251-
<input type="text" placeholder="edit me"
252-
value={this.state.value} onChange={this.handleChange} />
251+
<input
252+
type="text"
253+
placeholder="edit me"
254+
value={this.state.value}
255+
onChange={this.handleChange}
256+
/>
253257
<button onClick={this.handleSubmit}>Submit</button>
254258
</div>
255259
);
@@ -268,7 +272,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
268272
class Form extends React.Component {
269273
constructor(props) {
270274
super(props);
271-
this.state = {value: ""};
275+
this.state = {value: ''};
272276
this.handleChange = this.handleChange.bind(this);
273277
this.handleSubmit = this.handleSubmit.bind(this);
274278
}
@@ -278,7 +282,7 @@ class Form extends React.Component {
278282
}
279283

280284
handleSubmit(event) {
281-
alert("Textarea value is: '" + this.state.value + "'");
285+
alert('Textarea value is: ' + this.state.value);
282286
}
283287

284288
render() {
@@ -306,7 +310,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
306310
class Form extends React.Component {
307311
constructor(props) {
308312
super(props);
309-
this.state = {value: "B"};
313+
this.state = {value: 'B'};
310314
this.handleChange = this.handleChange.bind(this);
311315
this.handleSubmit = this.handleSubmit.bind(this);
312316
}
@@ -316,7 +320,7 @@ class Form extends React.Component {
316320
}
317321

318322
handleSubmit(event) {
319-
alert("Select value is: '" + this.state.value + "'");
323+
alert('Select value is: ' + this.state.value);
320324
}
321325

322326
render() {
@@ -344,7 +348,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
344348
class Form extends React.Component {
345349
constructor(props) {
346350
super(props);
347-
this.state = {value: "B"};
351+
this.state = {value: 'B'};
348352
this.handleChange = this.handleChange.bind(this);
349353
this.handleSubmit = this.handleSubmit.bind(this);
350354
}
@@ -354,19 +358,30 @@ class Form extends React.Component {
354358
}
355359

356360
handleSubmit(event) {
357-
alert("Radio button value is: '" + this.state.value + "'");
361+
alert('Radio button value is: ' + this.state.value);
358362
}
359363

360364
render() {
361365
return (
362366
<div>
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 />
367+
<label>
368+
<input type="radio" value="A" id="choice-a"
369+
onChange={this.handleChange} />
370+
Option A
371+
</label>
372+
<br />
373+
<label>
374+
<input type="radio" value="B" id="choice-b"
375+
onChange={this.handleChange} defaultChecked={true} />
376+
Option B
377+
</label>
369378
<br />
379+
<label>
380+
<input type="radio" value="C" id="choice-c"
381+
onChange={this.handleChange} />
382+
Option C
383+
</label>
384+
<br /><br />
370385
<button onClick={this.handleSubmit}>Submit</button>
371386
</div>
372387
);
@@ -385,7 +400,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
385400
class Form extends React.Component {
386401
constructor(props) {
387402
super(props);
388-
this.state = {checked: {"A": false, "B": true, "C": false}};
403+
this.state = {checked: {'A': false, 'B': true, 'C': false}};
389404
this.handleChange = this.handleChange.bind(this);
390405
this.handleSubmit = this.handleSubmit.bind(this);
391406
}
@@ -398,23 +413,33 @@ class Form extends React.Component {
398413
}
399414

400415
handleSubmit(event) {
401-
alert("Boxes checked: " +
402-
(this.state.checked.A ? "A " : "") +
403-
(this.state.checked.B ? "B " : "") +
404-
(this.state.checked.C ? "C" : "")
416+
alert('Boxes checked: ' +
417+
(this.state.checked.A ? 'A ' : '') +
418+
(this.state.checked.B ? 'B ' : '') +
419+
(this.state.checked.C ? 'C' : '')
405420
);
406421
}
407422

408423
render() {
409424
return (
410425
<div>
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 />
426+
<label>
427+
<input type="checkbox" value="A"
428+
onChange={this.handleChange} />
429+
Option A
430+
</label>
431+
<br />
432+
<label>
433+
<input type="checkbox" value="B"
434+
onChange={this.handleChange} defaultChecked={true} />
435+
Option B
436+
</label>
417437
<br />
438+
<label><input type="checkbox" value="C"
439+
onChange={this.handleChange} />
440+
Option C
441+
</label>
442+
<br /><br />
418443
<button onClick={this.handleSubmit}>Submit</button>
419444
</div>
420445
);

0 commit comments

Comments
 (0)