Skip to content

Commit 8ef7e33

Browse files
zhangxd6hijiangtao
authored andcommitted
doc(cn) - translate form (#95)
1 parent 74715d6 commit 8ef7e33

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

content/docs/forms.md

+63-61
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: forms
3-
title: Forms
3+
title: 表单
44
permalink: docs/forms.html
55
prev: lists-and-keys.html
66
next: lifting-state-up.html
@@ -9,27 +9,27 @@ redirect_from:
99
- "docs/forms-zh-CN.html"
1010
---
1111

12-
HTML form elements work a little bit differently from other DOM elements in React, because form elements naturally keep some internal state. For example, this form in plain HTML accepts a single name:
12+
在 React 里,HTML 表单元素的工作方式和其他的 DOM 元素有些不同,这是因为表单元素通常会保持一些内部的 state。例如这个纯 HTML 表单只接受一个名称:
1313

1414
```html
1515
<form>
1616
<label>
17-
Name:
17+
名字:
1818
<input type="text" name="name" />
1919
</label>
20-
<input type="submit" value="Submit" />
20+
<input type="submit" value="提交" />
2121
</form>
2222
```
2323

24-
This form has the default HTML form behavior of browsing to a new page when the user submits the form. If you want this behavior in React, it just works. But in most cases, it's convenient to have a JavaScript function that handles the submission of the form and has access to the data that the user entered into the form. The standard way to achieve this is with a technique called "controlled components".
24+
此表单具有默认的 HTML 表单行为,即在用户提交表单后浏览到新页面。如果你在 React 中执行相同的代码,它依然有效。但大多数情况下,使用 JavaScript 函数可以很方便的处理表单的提交, 同时还可以访问用户填写的表单数据。实现这种效果的标准方式是使用“受控组件”。
2525

26-
## Controlled Components {#controlled-components}
26+
## 受控组件 {#controlled-components}
2727

28-
In HTML, form elements such as `<input>`, `<textarea>`, and `<select>` typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with [`setState()`](/docs/react-component.html#setstate).
28+
HTML 中,表单元素(如`<input>` `<textarea>` `<select>`)之类的表单元素通常自己维护 state,并根据用户输入进行更新。而在 React 中,可变状态(mutable state)通常保存在组件的 state 属性中,并且只能通过使用 [`setState()`](/docs/react-component.html#setstate)来更新。
2929

30-
We can combine the two by making the React state be the "single source of truth". Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a "controlled component".
30+
我们可以把两者结合起来,使 React state 成为“唯一数据源”。渲染表单的 React 组件还控制着用户输入过程中表单发生的操作。被 React 以这种方式控制取值的表单输入元素就叫做“受控组件”。
3131

32-
For example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component:
32+
例如,如果我们想让前一个示例在提交时打印出名称,我们可以将表单写为受控组件:
3333

3434
```javascript{4,10-12,24}
3535
class NameForm extends React.Component {
@@ -46,54 +46,54 @@ class NameForm extends React.Component {
4646
}
4747
4848
handleSubmit(event) {
49-
alert('A name was submitted: ' + this.state.value);
49+
alert('提交的名字: ' + this.state.value);
5050
event.preventDefault();
5151
}
5252
5353
render() {
5454
return (
5555
<form onSubmit={this.handleSubmit}>
5656
<label>
57-
Name:
57+
名字:
5858
<input type="text" value={this.state.value} onChange={this.handleChange} />
5959
</label>
60-
<input type="submit" value="Submit" />
60+
<input type="submit" value="提交" />
6161
</form>
6262
);
6363
}
6464
}
6565
```
6666

67-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
67+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
6868

69-
Since the `value` attribute is set on our form element, the displayed value will always be `this.state.value`, making the React state the source of truth. Since `handleChange` runs on every keystroke to update the React state, the displayed value will update as the user types.
69+
由于在表单元素上设置了 `value` 属性,因此显示的值将始终为 `this.state.value`,这使得 React state 成为唯一数据源。由于 `handlechange` 在每次按键时都会执行并更新 React 的 state,因此显示的值将随着用户输入而更新。
7070

71-
With a controlled component, every state mutation will have an associated handler function. This makes it straightforward to modify or validate user input. For example, if we wanted to enforce that names are written with all uppercase letters, we could write `handleChange` as:
71+
对于受控组件来说,每个 state 突变都有一个相关的处理函数。这使得修改或验证用户输入变得简单。例如,如果我们要强制要求所有名称都用大写字母书写,我们可以将 `handlechange` 改写为:
7272

7373
```javascript{2}
7474
handleChange(event) {
7575
this.setState({value: event.target.value.toUpperCase()});
7676
}
7777
```
7878

79-
## The textarea Tag {#the-textarea-tag}
79+
## textarea 标签 {#the-textarea-tag}
8080

81-
In HTML, a `<textarea>` element defines its text by its children:
81+
HTML 中, `<textarea>` 元素通过其子元素定义其文本:
8282

8383
```html
8484
<textarea>
85-
Hello there, this is some text in a text area
85+
你好, 这是在 text area 里的文本
8686
</textarea>
8787
```
8888

89-
In React, a `<textarea>` uses a `value` attribute instead. This way, a form using a `<textarea>` can be written very similarly to a form that uses a single-line input:
89+
而在 React 中,`<textarea>` 使用 `value` 属性代替。这样,可以使得使用 `<textarea>` 的表单和使用单行 input 的表单非常类似:
9090

9191
```javascript{4-6,12-14,26}
9292
class EssayForm extends React.Component {
9393
constructor(props) {
9494
super(props);
9595
this.state = {
96-
value: 'Please write an essay about your favorite DOM element.'
96+
value: '请撰写一篇关于你喜欢的 DOM 元素的文章.'
9797
};
9898
9999
this.handleChange = this.handleChange.bind(this);
@@ -105,40 +105,40 @@ class EssayForm extends React.Component {
105105
}
106106
107107
handleSubmit(event) {
108-
alert('An essay was submitted: ' + this.state.value);
108+
alert('提交的文章: ' + this.state.value);
109109
event.preventDefault();
110110
}
111111
112112
render() {
113113
return (
114114
<form onSubmit={this.handleSubmit}>
115115
<label>
116-
Essay:
116+
文章:
117117
<textarea value={this.state.value} onChange={this.handleChange} />
118118
</label>
119-
<input type="submit" value="Submit" />
119+
<input type="submit" value="提交" />
120120
</form>
121121
);
122122
}
123123
}
124124
```
125125

126-
Notice that `this.state.value` is initialized in the constructor, so that the text area starts off with some text in it.
126+
请注意,`this.state.value` 初始化于构造函数中,因此文本区域默认有初值。
127127

128-
## The select Tag {#the-select-tag}
128+
## select 标签 {#the-select-tag}
129129

130-
In HTML, `<select>` creates a drop-down list. For example, this HTML creates a drop-down list of flavors:
130+
HTML 中,`<select>` 创建下拉列表标签。例如,如下 HTML 创建了水果相关的下拉列表:
131131

132132
```html
133133
<select>
134-
<option value="grapefruit">Grapefruit</option>
135-
<option value="lime">Lime</option>
136-
<option selected value="coconut">Coconut</option>
137-
<option value="mango">Mango</option>
134+
<option value="grapefruit">葡萄柚</option>
135+
<option value="lime">柠檬</option>
136+
<option selected value="coconut">椰子</option>
137+
<option value="mango">芒果</option>
138138
</select>
139139
```
140140

141-
Note that the Coconut option is initially selected, because of the `selected` attribute. React, instead of using this `selected` attribute, uses a `value` attribute on the root `select` tag. This is more convenient in a controlled component because you only need to update it in one place. For example:
141+
请注意,由于 `selected` 属性的缘故,椰子选项默认被选中。React 并不会使用 `selected` 属性,而是在根 `select` 标签上使用 `value` 属性。这在受控组件中更便捷,因为您只需要在根标签中更新它。例如:
142142

143143
```javascript{4,10-12,24}
144144
class FlavorForm extends React.Component {
@@ -155,56 +155,56 @@ class FlavorForm extends React.Component {
155155
}
156156
157157
handleSubmit(event) {
158-
alert('Your favorite flavor is: ' + this.state.value);
158+
alert('你喜欢的风味是: ' + this.state.value);
159159
event.preventDefault();
160160
}
161161
162162
render() {
163163
return (
164164
<form onSubmit={this.handleSubmit}>
165165
<label>
166-
Pick your favorite flavor:
166+
选择你喜欢的风味:
167167
<select value={this.state.value} onChange={this.handleChange}>
168-
<option value="grapefruit">Grapefruit</option>
169-
<option value="lime">Lime</option>
170-
<option value="coconut">Coconut</option>
171-
<option value="mango">Mango</option>
168+
<option value="grapefruit">葡萄柚</option>
169+
<option value="lime">柠檬</option>
170+
<option value="coconut">椰子</option>
171+
<option value="mango">芒果</option>
172172
</select>
173173
</label>
174-
<input type="submit" value="Submit" />
174+
<input type="submit" value="提交" />
175175
</form>
176176
);
177177
}
178178
}
179179
```
180180

181-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
181+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
182182

183-
Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select>` all work very similarly - they all accept a `value` attribute that you can use to implement a controlled component.
183+
总的来说,这使得 `<input type="text">`, `<textarea>` `<select>` 之类的标签都非常相似--它们都接受一个 `value` 属性,你可以使用它来实现受控组件。
184184

185-
> Note
185+
> 注意
186186
>
187-
> You can pass an array into the `value` attribute, allowing you to select multiple options in a `select` tag:
187+
> 你可以将数组传递到 `value` 属性中,以支持在 `select` 标签中选择多个选项:
188188
>
189189
>```js
190190
><select multiple={true} value={['B', 'C']}>
191191
>```
192192
193-
## The file input Tag {#the-file-input-tag}
193+
## 文件 input 标签 {#the-file-input-tag}
194194
195-
In HTML, an `<input type="file">` lets the user choose one or more files from their device storage to be uploaded to a server or manipulated by JavaScript via the [File API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).
195+
HTML 中,`<input type=file>` 允许用户从存储设备中选择一个或多个文件,将其上传到服务器,或通过使用 JavaScript [File API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications) 进行控制。
196196
197197
```html
198198
<input type="file" />
199199
```
200200
201-
Because its value is read-only, it is an **uncontrolled** component in React. It is discussed together with other uncontrolled components [later in the documentation](/docs/uncontrolled-components.html#the-file-input-tag).
201+
因为它的 value 只读,所以它是 React 中的一个**非受控**组件。将与其他非受控组件[在后续文档中](/docs/uncontrolled-components.html#the-file-input-tag)一起讨论。
202202

203-
## Handling Multiple Inputs {#handling-multiple-inputs}
203+
## 处理多个输入 {#handling-multiple-inputs}
204204

205-
When you need to handle multiple controlled `input` elements, you can add a `name` attribute to each element and let the handler function choose what to do based on the value of `event.target.name`.
205+
当需要处理多个 `input` 元素时,我们可以给每个元素添加 `name` 属性,并让处理函数根据 `event.target.name` 的值选择要执行的操作。
206206

207-
For example:
207+
例如:
208208

209209
```javascript{15,18,28,37}
210210
class Reservation extends React.Component {
@@ -232,7 +232,7 @@ class Reservation extends React.Component {
232232
return (
233233
<form>
234234
<label>
235-
Is going:
235+
参与:
236236
<input
237237
name="isGoing"
238238
type="checkbox"
@@ -241,7 +241,7 @@ class Reservation extends React.Component {
241241
</label>
242242
<br />
243243
<label>
244-
Number of guests:
244+
来宾人数:
245245
<input
246246
name="numberOfGuests"
247247
type="number"
@@ -254,31 +254,33 @@ class Reservation extends React.Component {
254254
}
255255
```
256256

257-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
257+
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
258258

259-
Note how we used the ES6 [computed property name](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) syntax to update the state key corresponding to the given input name:
259+
这里使用了 ES6 [计算属性名称](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names)的语法更新给定输入名称对应的 state 值:
260+
261+
例如:
260262

261263
```js{2}
262264
this.setState({
263265
[name]: value
264266
});
265267
```
266268

267-
It is equivalent to this ES5 code:
269+
等同 ES5:
268270

269271
```js{2}
270272
var partialState = {};
271273
partialState[name] = value;
272274
this.setState(partialState);
273275
```
274276

275-
Also, since `setState()` automatically [merges a partial state into the current state](/docs/state-and-lifecycle.html#state-updates-are-merged), we only needed to call it with the changed parts.
277+
另外,由于 `setState()` 自动[将部分 state 合并到当前 state](/docs/state-and-lifecycle.html#state-updates-are-merged), 只需调用它更改部分 state 即可。
276278

277-
## Controlled Input Null Value {#controlled-input-null-value}
279+
## 受控输入空值 {#controlled-input-null-value}
278280

279-
Specifying the value prop on a [controlled component](/docs/forms.html#controlled-components) prevents the user from changing the input unless you desire so. If you've specified a `value` but the input is still editable, you may have accidentally set `value` to `undefined` or `null`.
281+
[受控组件](/docs/forms.html#controlled-components)上指定 value 的 prop 可以防止用户更改输入。如果指定了 `value`,但输入仍可编辑,则可能是意外地将`value` 设置为 `undefined` `null`
280282

281-
The following code demonstrates this. (The input is locked at first but becomes editable after a short delay.)
283+
下面的代码演示了这一点。(输入最初被锁定,但在短时间延迟后变为可编辑。)
282284

283285
```javascript
284286
ReactDOM.render(<input value="hi" />, mountNode);
@@ -289,10 +291,10 @@ setTimeout(function() {
289291

290292
```
291293

292-
## Alternatives to Controlled Components {#alternatives-to-controlled-components}
294+
## 受控组件的替代品 {#alternatives-to-controlled-components}
293295

294-
It can sometimes be tedious to use controlled components, because you need to write an event handler for every way your data can change and pipe all of the input state through a React component. This can become particularly annoying when you are converting a preexisting codebase to React, or integrating a React application with a non-React library. In these situations, you might want to check out [uncontrolled components](/docs/uncontrolled-components.html), an alternative technique for implementing input forms.
296+
有时使用受控组件会很麻烦,因为你需要为数据变化的每种方式都编写事件处理函数,并通过一个 React 组件传递所有的输入 state。当你将之前的代码库转换为 React 或将 React 应用程序与非 React 库集成时,这可能会令人厌烦。在这些情况下,你可能希望使用[非受控组件](/docs/uncontrolled-components.html), 这是实现输入表单的另一种方式。
295297

296-
## Fully-Fledged Solutions {#fully-fledged-solutions}
298+
## 成熟的解决方案 {#fully-fledged-solutions}
297299

298-
If you're looking for a complete solution including validation, keeping track of the visited fields, and handling form submission, [Formik](https://jaredpalmer.com/formik) is one of the popular choices. However, it is built on the same principles of controlled components and managing state — so don't neglect to learn them.
300+
如果你想寻找包含验证、追踪访问字段以及处理表单提交的完整解决方案,使用 [Formik](https://jaredpalmer.com/formik) 是不错的选择。然而,它也是建立在受控组件和管理 state 的基础之上——所以不要忽视学习它们。

0 commit comments

Comments
 (0)