Skip to content

Commit 2d80cb8

Browse files
fajrizulfikarvferdiansyah
authored andcommitted
Translate Forms (#118)
Co-Authored-By: Veri Ferdiansyah <[email protected]>
1 parent 4e10ffb commit 2d80cb8

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

content/docs/forms.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: forms
3-
title: Forms
3+
title: Form
44
permalink: docs/forms.html
55
prev: lists-and-keys.html
66
next: lifting-state-up.html
@@ -9,7 +9,7 @@ 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+
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:
1313

1414
```html
1515
<form>
@@ -21,15 +21,15 @@ HTML form elements work a little bit differently from other DOM elements in Reac
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+
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_".
2525

26-
## Controlled Components {#controlled-components}
26+
## Controlled Component {#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+
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).
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+
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_".
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+
Sebagai contoh, jika kita ingin membuat form pada contoh sebelumnya mencatat sebuah nama ketika nama dikirim, kita dapat menuliskan form sebagai sebuah _controlled component_:
3333

3434
```javascript{4,10-12,24}
3535
class NameForm extends React.Component {
@@ -64,29 +64,29 @@ class NameForm extends React.Component {
6464
}
6565
```
6666

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)
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+
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.
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+
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:
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+
## Tag textarea {#the-textarea-tag}
8080

81-
In HTML, a `<textarea>` element defines its text by its children:
81+
Pada HTML, elemen `<textarea>` mendefinisikan teks di dalamnya sebagai elemen anaknya:
8282

8383
```html
8484
<textarea>
8585
Hello there, this is some text in a 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+
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:
9090

9191
```javascript{4-6,12-14,26}
9292
class EssayForm extends React.Component {
@@ -123,11 +123,11 @@ class EssayForm extends React.Component {
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+
Perhatikan bahwa `this.state.value` diinisialisasi di constructor, sehingga area teks akan memiliki teks di dalamnya.
127127

128-
## The select Tag {#the-select-tag}
128+
## Tag 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+
Pada HTML, `<select>` membuat sebuah daftar _drop-down_. Sebagai contoh, HTML ini membuat sebuah daftar _drop-down_ dari beberapa bumbu:
131131

132132
```html
133133
<select>
@@ -138,7 +138,7 @@ In HTML, `<select>` creates a drop-down list. For example, this HTML creates a d
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+
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:
142142

143143
```javascript{4,10-12,24}
144144
class FlavorForm extends React.Component {
@@ -178,33 +178,33 @@ class FlavorForm extends React.Component {
178178
}
179179
```
180180

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)
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+
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_.
184184

185-
> Note
185+
> Catatan
186186
>
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`:
188188
>
189189
>```js
190190
><select multiple={true} value={['B', 'C']}>
191191
>```
192192
193-
## The file input Tag {#the-file-input-tag}
193+
## Masukkan Tag File {#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+
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).
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+
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).
202202

203-
## Handling Multiple Inputs {#handling-multiple-inputs}
203+
## Menangani Banyak Input {#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+
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`.
206206

207-
For example:
207+
Sebagai contoh:
208208

209209
```javascript{15,18,28,37}
210210
class Reservation extends React.Component {
@@ -254,31 +254,31 @@ class Reservation extends React.Component {
254254
}
255255
```
256256

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)
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+
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:
260260

261261
```js{2}
262262
this.setState({
263263
[name]: value
264264
});
265265
```
266266

267-
It is equivalent to this ES5 code:
267+
Kode diatas setara dengan kode ES5 berikut:
268268

269269
```js{2}
270270
var partialState = {};
271271
partialState[name] = value;
272272
this.setState(partialState);
273273
```
274274

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.
276276

277-
## Controlled Input Null Value {#controlled-input-null-value}
277+
## Mengendalikan Masukkan Nilai Kosong {#controlled-input-null-value}
278278

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`.
280280

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.)
282282

283283
```javascript
284284
ReactDOM.render(<input value="hi" />, mountNode);
@@ -289,10 +289,10 @@ setTimeout(function() {
289289

290290
```
291291

292-
## Alternatives to Controlled Components {#alternatives-to-controlled-components}
292+
## Alternatif dari Controlled Components {#alternatives-to-controlled-components}
293293

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.
295295

296-
## Fully-Fledged Solutions {#fully-fledged-solutions}
296+
## Solusi Selengkapnya {#fully-fledged-solutions}
297297

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.
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

Comments
 (0)