Skip to content

Commit a18c275

Browse files
authored
Merge pull request #95 from 3imed-jaberi/uncontrolled-components
[Done] Translation of uncontrolled-components.md
2 parents fd9d633 + 28b97c2 commit a18c275

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed
Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
id: uncontrolled-components
3-
title: Uncontrolled Components
3+
title: المكونات غير المضبوطة
44
permalink: docs/uncontrolled-components.html
55
---
66

7-
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.
7+
نُفضِّل في معظم الحالات استخدام [المُكوّنات المضبوطة](/docs/forms.html) من أجل حقول الإدخال، ففي المُكوّنات المضبوطة يتعامل مُكوّن React مع بيانات الحقول. البديل لها هو المُكوّنات غير المضبوطة والتي يتعامل فيها DOM مع بيانات الحقول.
88

9-
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.
9+
لكتابة مُكوّن غير مضبوط بدلًا من كتابة معالج أحداث لكل تحديث للحالة، فبإمكانك [استخدام المراجع](/docs/refs-and-the-dom.html) للحصول على قيم الحقول من DOM.
1010

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

1313
```javascript{5,9,18}
1414
class NameForm extends React.Component {
@@ -19,7 +19,7 @@ class NameForm extends React.Component {
1919
}
2020
2121
handleSubmit(event) {
22-
alert('A name was submitted: ' + this.input.current.value);
22+
alert('قُدِّم اسم : ' + this.input.current.value);
2323
event.preventDefault();
2424
}
2525
@@ -30,22 +30,22 @@ class NameForm extends React.Component {
3030
Name:
3131
<input type="text" ref={this.input} />
3232
</label>
33-
<input type="submit" value="Submit" />
33+
<input type="submit" value="تقديم الاسم" />
3434
</form>
3535
);
3636
}
3737
}
3838
```
3939

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

42-
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.
42+
لما كان المُكوّن غير المضبوط يحتفظ بمصدر الحقيقة ضمن DOM، فمن الأسهل أحيانًا دمج شيفرة React مع أي شيفرة أخرى عند استخدام المُكوّنات غير المضبوطة. قد يؤدي ذلك أيضًا إلى كتابة شيفرة أقل إن كانت تهمك السرعة. أما في غير ذلك فيجب عادةً استخدام المُكوّنات المضبوطة.
4343

44-
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.
44+
إن كنتَ لا تزال غير متأكد من نوع المُكوّن الذي يجب استخدامه لحالة محددة، فقد تجد [هذه المقالة](https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/) مفيدة.
4545

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

48-
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`.
48+
في دورة حياة تصيير React تتجاوز قيمة الخاصية `value` لعناصر الحقول القيمة الموجودة في DOM. ترغب عادة في المُكوّنات غير المضبوطة أن تُحدِّد React القيمة المبدئية، ولكن بنفس الوقت أن تترك التحديثات التالية غير مضبوطة. للتعامل مع هذه الحالة يمكنك تحديد الخاصية `defaultValue` بدلًا من `value`:
4949

5050
```javascript{7}
5151
render() {
@@ -54,31 +54,70 @@ render() {
5454
<label>
5555
Name:
5656
<input
57-
defaultValue="Bob"
57+
defaultValue="محمد علي"
5858
type="text"
5959
ref={this.input} />
6060
</label>
61-
<input type="submit" value="Submit" />
61+
<input type="submit" value="تقديم الاسم" />
6262
</form>
6363
);
6464
}
6565
```
6666

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

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

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

7373
```html
7474
<input type="file" />
7575
```
7676

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

79-
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:
79+
```javascript
80+
class FileInput extends React.Component {
81+
constructor(props) {
82+
super(props);
83+
this.handleSubmit = this.handleSubmit.bind(this);
84+
this.fileInput = React.createRef();
85+
}
86+
handleSubmit(event) {
87+
event.preventDefault();
88+
alert(
89+
`Selected file - ${
90+
this.fileInput.current.files[0].name
91+
}`
92+
);
93+
}
94+
95+
render() {
96+
return (
97+
<form onSubmit={this.handleSubmit}>
98+
99+
<label>
100+
101+
تحميل الملف:
102+
103+
104+
<input type="file" ref={this.fileInput} />
105+
106+
</label>
107+
108+
<br />
80109

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

83-
[](codepen://uncontrolled-components/input-type-file)
112+
</form>
113+
);
114+
}
115+
}
116+
117+
ReactDOM.render(
118+
<FileInput />,
119+
document.getElementById('root')
120+
);
121+
```
122+
[جرّب المثال على موقع CodePen.](codepen://uncontrolled-components/input-type-file)
84123

content/docs/web-components.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
---
22
id: web-components
3-
title: مكونات الويب
3+
title: Web Components
44
permalink: docs/web-components.html
55
redirect_from:
66
- "docs/webcomponents.html"
77
---
88

9-
إنّ مكوّنات React و[مكوّنات الويب](https://developer.mozilla.org/en-US/docs/Web/Web_Components) مبنية لحل مشاكل مختلفة. حيث تُزوِّدنا مكوّنات الويب بتغليف قوي لمكوّنات قابلة لإعادة الاستخدام، بينما تُزوِّدنا مكوّنات React بمكتبة تصريحات تُبقي DOM على تزامن مع بياناتنا. يكون هذا الهدفان متكاملين، وكمُطوّر لك حرية استخدام React في مكوّنات الويب لديك، أو استخدام مكوّنات الويب في React أو كليهما معًا.
9+
React and [Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components) are built to solve different problems. Web Components provide strong encapsulation for reusable components, while React provides a declarative library that keeps the DOM in sync with your data. The two goals are complementary. As a developer, you are free to use React in your Web Components, or to use Web Components in React, or both.
1010

11-
معظم من يستخدم React لا يستخدم مكوّنات الويب، ولكن قد ترغب في ذلك خاصة إذا كنت تستخدم مكوّنات لواجهة المستخدم من طرف ثالث والتي تكون مكتوبة باستخدام مكوّنات الويب.
11+
Most people who use React don't use Web Components, but you may want to, especially if you are using third-party UI components that are written using Web Components.
1212

13-
14-
## استخدام مكوّنات الويب في React {#using-web-components-in-react}
13+
## Using Web Components in React {#using-web-components-in-react}
1514

1615
```javascript
1716
class HelloMessage extends React.Component {
1817
render() {
19-
return <div>أهلًا <x-search>{this.props.name}</x-search>!</div>;
18+
return <div>Hello <x-search>{this.props.name}</x-search>!</div>;
2019
}
2120
}
2221
```
2322

24-
> ملاحظة:
23+
> Note:
2524
>
26-
>تعرض مكوّنات الويب عادة واجهة برمجة تطبيقات (API) إلزاميّة. على سبيل المثال قد يُعرِّض مكوّن الويب `video` الدالتين `play()`‎ و `pause()`، وللوصول إلى واجهة برمجة التطبيقات الإلزامية لمكوّنات الويب ستحتاج إلى استخدام مرجع للتفاعل مع عقدة DOM مباشرةً. إن كنت تستخدم مكوّنات ويب من طرف ثالث فالحل الأمثل هو كتابة مكوّن React يسلك سلوك غلاف لمكوّنات الويب لديك.
25+
> Web Components often expose an imperative API. For instance, a `video` Web Component might expose `play()` and `pause()` functions. To access the imperative APIs of a Web Component, you will need to use a ref to interact with the DOM node directly. If you are using third-party Web Components, the best solution is to write a React component that behaves as a wrapper for your Web Component.
2726
>
28-
> الأحداث الصادرة من قبل مكوّن الويب قد لا تنتشر بشكل صحيح عبر شجرة تصيير React.
29-
> ستحتاج إلى إرفاق معالج أحداث يدويًّا للتعامل مع هذه الأحداث ضمن مكوّنات React لديك.
27+
> Events emitted by a Web Component may not properly propagate through a React render tree.
28+
> You will need to manually attach event handlers to handle these events within your React components.
3029
31-
إحدى الأمور التي من الشائع الخطأ بها هي استخدام مكوّنات الويب للخاصيّة "class" بدلًا من "className":
30+
One common confusion is that Web Components use "class" instead of "className".
3231

3332
```javascript
3433
function BrickFlipbox() {
3534
return (
3635
<brick-flipbox class="demo">
37-
<div>الواجهة الأمامية</div>
38-
<div>الواجهة الخلفية</div>
36+
<div>front</div>
37+
<div>back</div>
3938
</brick-flipbox>
4039
);
4140
}
4241
```
4342

44-
## استخدام React في مكوّنات الويب لديك {#using-react-in-your-web-components}
43+
## Using React in your Web Components {#using-react-in-your-web-components}
4544

4645
```javascript
4746
class XSearch extends HTMLElement {
@@ -57,7 +56,7 @@ class XSearch extends HTMLElement {
5756
customElements.define('x-search', XSearch);
5857
```
5958

60-
>ملاحظة:
59+
>Note:
6160
>
62-
> **لن** تعمل هذه الشيفرة إن نقلت الأصناف باستخدام Babel. انظر إلى هذه المشكلة [من هنا](https://github.com/w3c/webcomponents/issues/587).
63-
> يجب عليك تضمين [custom-elements-es5-adapter](https://github.com/webcomponents/webcomponentsjs#custom-elements-es5-adapterjs) قبل تحميل مكوّنات الويب لإصلاح هذه المشكلة.
61+
>This code **will not** work if you transform classes with Babel. See [this issue](https://github.com/w3c/webcomponents/issues/587) for the discussion.
62+
>Include the [custom-elements-es5-adapter](https://github.com/webcomponents/webcomponentsjs#custom-elements-es5-adapterjs) before you load your web components to fix this issue.

0 commit comments

Comments
 (0)