Skip to content

[Done] Translation of uncontrolled-components.md #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 59 additions & 20 deletions content/docs/uncontrolled-components.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
id: uncontrolled-components
title: Uncontrolled Components
title: المكونات غير المضبوطة
permalink: docs/uncontrolled-components.html
---

In most cases, we recommend using [controlled components](/docs/forms.html) to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.
نُفضِّل في معظم الحالات استخدام [المُكوّنات المضبوطة](/docs/forms.html) من أجل حقول الإدخال، ففي المُكوّنات المضبوطة يتعامل مُكوّن React مع بيانات الحقول. البديل لها هو المُكوّنات غير المضبوطة والتي يتعامل فيها DOM مع بيانات الحقول.

To write an uncontrolled component, instead of writing an event handler for every state update, you can [use a ref](/docs/refs-and-the-dom.html) to get form values from the DOM.
لكتابة مُكوّن غير مضبوط بدلًا من كتابة معالج أحداث لكل تحديث للحالة، فبإمكانك [استخدام المراجع](/docs/refs-and-the-dom.html) للحصول على قيم الحقول من DOM.

For example, this code accepts a single name in an uncontrolled component:
مثلًا تقبل هذه الشيفرة اسمًا واحدًا في المُكوّن غير المضبوط:

```javascript{5,9,18}
class NameForm extends React.Component {
Expand All @@ -19,7 +19,7 @@ class NameForm extends React.Component {
}

handleSubmit(event) {
alert('A name was submitted: ' + this.input.current.value);
alert('قُدِّم اسم : ' + this.input.current.value);
event.preventDefault();
}

Expand All @@ -30,22 +30,22 @@ class NameForm extends React.Component {
Name:
<input type="text" ref={this.input} />
</label>
<input type="submit" value="Submit" />
<input type="submit" value="تقديم الاسم" />
</form>
);
}
}
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/WooRWa?editors=0010)
[**جرّب المثال على موقع CodePen**](https://codepen.io/gaearon/pen/WooRWa?editors=0010).

Since an uncontrolled component keeps the source of truth in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components. It can also be slightly less code if you want to be quick and dirty. Otherwise, you should usually use controlled components.
لما كان المُكوّن غير المضبوط يحتفظ بمصدر الحقيقة ضمن DOM، فمن الأسهل أحيانًا دمج شيفرة React مع أي شيفرة أخرى عند استخدام المُكوّنات غير المضبوطة. قد يؤدي ذلك أيضًا إلى كتابة شيفرة أقل إن كانت تهمك السرعة. أما في غير ذلك فيجب عادةً استخدام المُكوّنات المضبوطة.

If it's still not clear which type of component you should use for a particular situation, you might find [this article on controlled versus uncontrolled inputs](https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/) to be helpful.
إن كنتَ لا تزال غير متأكد من نوع المُكوّن الذي يجب استخدامه لحالة محددة، فقد تجد [هذه المقالة](https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/) مفيدة.

### Default Values {#default-values}
### القيم الافتراضية {#default-values}

In the React rendering lifecycle, the `value` attribute on form elements will override the value in the DOM. With an uncontrolled component, you often want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a `defaultValue` attribute instead of `value`.
في دورة حياة تصيير React تتجاوز قيمة الخاصية `value` لعناصر الحقول القيمة الموجودة في DOM. ترغب عادة في المُكوّنات غير المضبوطة أن تُحدِّد React القيمة المبدئية، ولكن بنفس الوقت أن تترك التحديثات التالية غير مضبوطة. للتعامل مع هذه الحالة يمكنك تحديد الخاصية `defaultValue` بدلًا من `value`:

```javascript{7}
render() {
Expand All @@ -54,31 +54,70 @@ render() {
<label>
Name:
<input
defaultValue="Bob"
defaultValue="محمد علي"
type="text"
ref={this.input} />
</label>
<input type="submit" value="Submit" />
<input type="submit" value="تقديم الاسم" />
</form>
);
}
```

Likewise, `<input type="checkbox">` and `<input type="radio">` support `defaultChecked`, and `<select>` and `<textarea>` supports `defaultValue`.
يدعم العنصران `<input type="checkbox">` و `<input type="radio">`‎ الخاصية `defaultChecked`. ويدعم العنصران `<select>` و `<textarea>` الخاصية `defaultValue`.

## The file input Tag {#the-file-input-tag}
## عنصر إدخال الملفات {#the-file-input-tag}

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).
يُتيح العنصر `<input type="file">` في HTML للمستخدم اختيار ملف أو أكثر من جهازه لتحميلها والتعامل معها من خلال JavaScript عبر واجهة برمجة الملفات [File API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).

```html
<input type="file" />
```

In React, an `<input type="file" />` is always an uncontrolled component because its value can only be set by a user, and not programmatically.
يُعدُّ العنصر ‎`<input type="file" />`‎ في React دوما مكونًا غير منضبط لأنّ قيمته لا يُمكِن تعيينها إلّا عن طريق المستخدم وليس برمجيًّا. يجب عليك استخدام واجهة برمجة الملفات للتعامل مع الملفات. يُظهِر المثال التالي كيفية [إنشاء مرجع إلى عقدة DOM](/docs/refs-and-the-dom.html) للوصول إلى الملفات من خلال دالة التعامل مع تقديم الحقول (Submit):

You should use the File API to interact with the files. The following example shows how to create a [ref to the DOM node](/docs/refs-and-the-dom.html) to access file(s) in a submit handler:
```javascript
class FileInput extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.fileInput = React.createRef();
}
handleSubmit(event) {
event.preventDefault();
alert(
`Selected file - ${
this.fileInput.current.files[0].name
}`
);
}

render() {
return (
<form onSubmit={this.handleSubmit}>

<label>

تحميل الملف:


<input type="file" ref={this.fileInput} />

</label>

<br />

`embed:uncontrolled-components/input-type-file.js`
<button type="submit">تقديم الطلب</button>

[](codepen://uncontrolled-components/input-type-file)
</form>
);
}
}

ReactDOM.render(
<FileInput />,
document.getElementById('root')
);
```
[جرّب المثال على موقع CodePen.](codepen://uncontrolled-components/input-type-file)