You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/forms.md
+38-38Lines changed: 38 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
id: forms
3
-
title: Forms
3
+
title: Form
4
4
permalink: docs/forms.html
5
5
prev: lists-and-keys.html
6
6
next: lifting-state-up.html
@@ -9,7 +9,7 @@ redirect_from:
9
9
- "docs/forms-zh-CN.html"
10
10
---
11
11
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
+
Elemen form HTML bekerja sedikit berbeda dari elemen DOM lainnya di React, karena elemen form secara natural menyimpan beberapa _state_ internal. Sebagai contoh, form ini pada HTML biasa menerima nama tunggal:
13
13
14
14
```html
15
15
<form>
@@ -21,15 +21,15 @@ HTML form elements work a little bit differently from other DOM elements in Reac
21
21
</form>
22
22
```
23
23
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
+
Form ini memiliki perilaku dasar dari form HTML biasa yakni menuju ke laman baru ketika user mengirim form tersebut. Jika Anda menginginkan perilaku seperti ini di React, ini sebenarnya dapat bekerja. Namun di banyak kasus, akan lebih mudah untuk memiliki sebuah fungsi JavaScript yang menangani sebuah submisi dari sebuah form dan memiliki akses terhadap data yang dimasukkan pengguna ke dalam form. Cara standar untuk mencapai hal ini adalah dengan teknik yang disebut "_controlled component_".
25
25
26
-
## Controlled Components {#controlled-components}
26
+
## Controlled Component {#controlled-components}
27
27
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
+
Pada HTML, elemen form seperti `<input>`, `<textarea>`, dan`<select>`biasanya menyimpan _state_ mereka sendiri dan memperbaruinya berdasarkan masukan dari pengguna. Di React, _state_ yang dapat berubah seperti ini biasanya disimpan pada properti dari komponen, dan hanya akan diubah menggunakan[`setState()`](/docs/react-component.html#setstate).
29
29
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
+
Kita dapat menggabungkan keduanya dengan menggunakan _state_ pada React sebagai "sumber kebenaran satu-satunya". Kemudian komponen React yang me-_render_ sebuah form juga mengontrol apa yang terjadi dalam form tersebut pada masukan pengguna selanjutnya. Sebuah elemen masukan form yang nilainya dikontrol oleh React melalui cara seperti ini disebut sebagai "_controlled component_".
31
31
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
+
Sebagai contoh, jika kita ingin membuat form pada contoh sebelumnya mencatat sebuah nama ketika nama dikirim, kita dapat menuliskan form sebagai sebuah _controlled component_:
33
33
34
34
```javascript{4,10-12,24}
35
35
class NameForm extends React.Component {
@@ -64,29 +64,29 @@ class NameForm extends React.Component {
64
64
}
65
65
```
66
66
67
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
67
+
[**Coba di CodePen**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
68
68
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
+
Karena atribut`value`telah kita set pada elemen form, nilai yang ditampilkan akan selalu sama dengan `this.state.value`, yang menjadikan React sebagai sumber kebenaran tunggal dari _state_. Dan karena `handleChange`dijalankan setiap ketikan untuk memperbarui _state_React, nilai yang ditampilkan akan terbarui ketika pengguna mengetik.
70
70
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
+
Dengan sebuah _controlled component_, setiap perubahan _state_ akan memiliki sebuah fungsi _handler_ yang terkait. Hal ini memudahkan untuk memodifikasi atau memvalidasi masukan pengguna. Sebagai contoh, jika kita ingin mengharuskan nama untuk seluruhnya ditulis dengan huruf kapital, kita dapat menuliskan`handleChange`sebagai:
In HTML, a`<textarea>`element defines its text by its children:
81
+
Pada HTML, elemen`<textarea>`mendefinisikan teks di dalamnya sebagai elemen anaknya:
82
82
83
83
```html
84
84
<textarea>
85
85
Hello there, this is some text in a text area
86
86
</textarea>
87
87
```
88
88
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
+
Di React, `<textarea>`menggunakan atribut`value`. Dengan cara ini, sebuah form yang menggunakan`<textarea>`dapat ditulis dengan cara yang sangat mirip dengan sebuah form yang menggunakan masukan satu baris:
90
90
91
91
```javascript{4-6,12-14,26}
92
92
class EssayForm extends React.Component {
@@ -123,11 +123,11 @@ class EssayForm extends React.Component {
123
123
}
124
124
```
125
125
126
-
Notice that`this.state.value`is initialized in the constructor, so that the text area starts off with some text in it.
126
+
Perhatikan bahwa`this.state.value`diinisialisasi di constructor, sehingga area teks akan memiliki teks di dalamnya.
127
127
128
-
## The select Tag {#the-select-tag}
128
+
## Tag select {#the-select-tag}
129
129
130
-
In HTML, `<select>`creates a drop-down list. For example, this HTML creates a drop-down list of flavors:
130
+
Pada HTML, `<select>`membuat sebuah daftar _drop-down_. Sebagai contoh, HTML ini membuat sebuah daftar _drop-down_ dari beberapa bumbu:
131
131
132
132
```html
133
133
<select>
@@ -138,7 +138,7 @@ In HTML, `<select>` creates a drop-down list. For example, this HTML creates a d
138
138
</select>
139
139
```
140
140
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
+
Perhatikan bahwa opsi Coconut mula-mula dipilih, karena adanya atribut`selected`. Di React, alih-alih menggunakan atribut `selected`, kita menggunakan atribut `value`di tag `select`. Hal ini lebih mudah dalam sebuah _controlled component_ karena Anda hanya perlu mengubahnya di satu tempat saja. Sebagai contoh:
142
142
143
143
```javascript{4,10-12,24}
144
144
class FlavorForm extends React.Component {
@@ -178,33 +178,33 @@ class FlavorForm extends React.Component {
178
178
}
179
179
```
180
180
181
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
181
+
[**Coba di CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
182
182
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
+
Secara keseluruhan, perubahan-perubahan ini membuat `<input type="text">`, `<textarea>`, dan`<select>`bekerja dengan cara yang mirip - mereka masing-masing menerima atribut`value`yang dapat Anda gunakan untuk mengimplementasikan _controlled component_.
184
184
185
-
> Note
185
+
> Catatan
186
186
>
187
-
> You can pass an array into the`value` attribute, allowing you to select multiple options in a`select` tag:
187
+
> Anda bisa memasukan array ke atribut`value`, yang memungkinkan anda memilih beberapa opsi dalam tag`select`:
188
188
>
189
189
>```js
190
190
><select multiple={true} value={['B', 'C']}>
191
191
>```
192
192
193
-
## The file input Tag {#the-file-input-tag}
193
+
## Masukkan Tag File {#the-file-input-tag}
194
194
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
+
Dalam HTML, sebuah`<input type="file">`membiarkan pengguna untuk memilih satu atau lebih berkas dari penyimpanan perangkat mereka untuk diunggah ke sebuah server atau dimanipulasi oleh JavaScript melalui [Berkas API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).
196
196
197
197
```html
198
198
<input type="file"/>
199
199
```
200
200
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
+
Karena nilai yang dimiliki adalah _read-only_, ini termasuk ke dalam _***uncontrolled*** component_ di React. Hal ini akan dibahas bersama dengan _uncontrolled component_ lainnya [selanjutnya di dokumentasi](/docs/uncontrolled-components.html#the-file-input-tag).
## Menangani Banyak Input {#handling-multiple-inputs}
204
204
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
+
Ketika Anda membutuhkan penanganan banyak elemen `input`terkontrol, Anda dapat menambahkan atribut`name`pada setiap elemen dan membiarkan fungsi _handler_ memilih apa yang harus dilakukan berdasarkan nilai dari`event.target.name`.
206
206
207
-
For example:
207
+
Sebagai contoh:
208
208
209
209
```javascript{15,18,28,37}
210
210
class Reservation extends React.Component {
@@ -254,31 +254,31 @@ class Reservation extends React.Component {
254
254
}
255
255
```
256
256
257
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
257
+
[**Coba di CodePen**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
258
258
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
+
Perhatikan bagaimana kita meggunakan sintaks [_computed property name_](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names)ES6 untuk mengubah _state_ dengan _key_ yang sesuai dengan nama dari masukan:
260
260
261
261
```js{2}
262
262
this.setState({
263
263
[name]: value
264
264
});
265
265
```
266
266
267
-
It is equivalent to this ES5 code:
267
+
Kode diatas setara dengan kode ES5 berikut:
268
268
269
269
```js{2}
270
270
var partialState = {};
271
271
partialState[name] = value;
272
272
this.setState(partialState);
273
273
```
274
274
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.
275
+
Dan juga, karena`setState()`secara otomatis [menggabungkan _state_ parsial ke _state_ yang sudah ada](/docs/state-and-lifecycle.html#state-updates-are-merged), kita hanya perlu memanggilnya dengan bagian yang berubah saja.
276
276
277
-
## Controlled Input Null Value {#controlled-input-null-value}
277
+
## Mengendalikan Masukkan Nilai Kosong {#controlled-input-null-value}
278
278
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`.
279
+
Menentukan nilai _prop_ pada [controlled component](/docs/forms.html#controlled-components)mencegah pengguna mengubah masukan kecuali Anda menginginkannya. Jika Anda telah menetapkan nilai `value`namun masukan masih dapat diubah, Anda mungkin telah secara tidak sengaja menetapkan `value`ke`undefined`atau`null`.
280
280
281
-
The following code demonstrates this. (The input is locked at first but becomes editable after a short delay.)
281
+
Kode berikut menunjukkan contoh ini. (Mula-mula masukan terkunci tetapi menjadi dapat diubah setelah jeda singkat.)
282
282
283
283
```javascript
284
284
ReactDOM.render(<input value="hi"/>, mountNode);
@@ -289,10 +289,10 @@ setTimeout(function() {
289
289
290
290
```
291
291
292
-
## Alternatives to Controlled Components {#alternatives-to-controlled-components}
292
+
## Alternatif dari Controlled Components {#alternatives-to-controlled-components}
293
293
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.
294
+
Terkadang akan menjadi sulit untuk menggunakan _controlled components_, karena Anda perlu menulis _event handler_ untuk setiap cara data Anda berubah dan menyalurkan semua masukan _state_ melalui komponen React. Ini dapat menjadi sangat menjengkelkan ketika anda sedang mengkonversi basis kode yang sudah ada ke React, atau mengintegrasikan sebuah aplikasi React dengan sebuah _library_ bukan-React. Pada situasi seperti ini, Anda mungkin ingin menggunakan [uncontrolled components](/docs/uncontrolled-components.html), sebuah teknik alternatif untuk mengimplementasikan masukan form.
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.
298
+
Jika anda mencari solusi komplit termasuk validasi, melacak _fields_ yang dikunjungi, dan menangani pengiriman form, [Formik](https://jaredpalmer.com/formik)adalah salah satu pilihan populer. Namun, itu dibuat dengan prinsip yang sama dengan _controlled components_ dan pengelolaan _state_ — jadi jangan lalai untuk mempelajarinya.
0 commit comments