Skip to content

Commit 504fb6a

Browse files
mehran-prsthetaPC
andauthored
feat(input, textarea): dir is inherited to native form control (#30102)
Issue number: resolves #30193 resolves #29577 Co-authored-by: Maria Hutt <thetaPC@users.noreply.github.com>
1 parent 18e26ac commit 504fb6a

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

‎core/src/components/input/input.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,26 @@ export class Input implements ComponentInterface {
338338
}
339339
}
340340

341+
/**
342+
* dir is a globally enumerated attribute.
343+
* As a result, creating these as properties
344+
* can have unintended side effects. Instead, we
345+
* listen for attribute changes and inherit them
346+
* to the inner `<input>` element.
347+
*/
348+
@Watch('dir')
349+
onDirChanged(newValue: string) {
350+
this.inheritedAttributes = {
351+
...this.inheritedAttributes,
352+
dir: newValue,
353+
};
354+
forceUpdate(this);
355+
}
356+
341357
componentWillLoad() {
342358
this.inheritedAttributes = {
343359
...inheritAriaAttributes(this.el),
344-
...inheritAttributes(this.el, ['tabindex', 'title', 'data-form-type']),
360+
...inheritAttributes(this.el, ['tabindex', 'title', 'data-form-type', 'dir']),
345361
};
346362
}
347363

‎core/src/components/input/test/input.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ describe('input: rendering', () => {
4444
const bottomContent = page.body.querySelector('ion-input .input-bottom');
4545
expect(bottomContent).toBe(null);
4646
});
47+
48+
it('should inherit watched attributes', async () => {
49+
const page = await newSpecPage({
50+
components: [Input],
51+
html: '<ion-input label="Input" dir="ltr"></ion-input>',
52+
});
53+
54+
const inputEl = page.body.querySelector('ion-input')!;
55+
const nativeEl = inputEl.querySelector('input')!;
56+
57+
expect(nativeEl.getAttribute('dir')).toBe('ltr');
58+
59+
inputEl.setAttribute('dir', 'rtl');
60+
61+
await page.waitForChanges();
62+
63+
expect(nativeEl.getAttribute('dir')).toBe('rtl');
64+
});
4765
});
4866

4967
/**

‎core/src/components/textarea/test/textarea.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ it('should inherit attributes', async () => {
1414
expect(nativeEl.getAttribute('data-form-type')).toBe('password');
1515
});
1616

17+
it('should inherit watched attributes', async () => {
18+
const page = await newSpecPage({
19+
components: [Textarea],
20+
html: '<ion-textarea dir="ltr"></ion-textarea>',
21+
});
22+
23+
const textareaEl = page.body.querySelector('ion-textarea')!;
24+
const nativeEl = textareaEl.querySelector('textarea')!;
25+
26+
expect(nativeEl.getAttribute('dir')).toBe('ltr');
27+
28+
textareaEl.setAttribute('dir', 'rtl');
29+
30+
await page.waitForChanges();
31+
32+
expect(nativeEl.getAttribute('dir')).toBe('rtl');
33+
});
34+
1735
/**
1836
* Textarea uses emulated slots, so the internal
1937
* behavior will not exactly match IonSelect's slots.

‎core/src/components/textarea/textarea.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,22 @@ export class Textarea implements ComponentInterface {
261261
this.runAutoGrow();
262262
}
263263

264+
/**
265+
* dir is a globally enumerated attribute.
266+
* As a result, creating these as properties
267+
* can have unintended side effects. Instead, we
268+
* listen for attribute changes and inherit them
269+
* to the inner `<textarea>` element.
270+
*/
271+
@Watch('dir')
272+
onDirChanged(newValue: string) {
273+
this.inheritedAttributes = {
274+
...this.inheritedAttributes,
275+
dir: newValue,
276+
};
277+
forceUpdate(this);
278+
}
279+
264280
/**
265281
* The `ionChange` event is fired when the user modifies the textarea's value.
266282
* Unlike the `ionInput` event, the `ionChange` event is fired when
@@ -331,7 +347,7 @@ export class Textarea implements ComponentInterface {
331347
componentWillLoad() {
332348
this.inheritedAttributes = {
333349
...inheritAriaAttributes(this.el),
334-
...inheritAttributes(this.el, ['data-form-type', 'title', 'tabindex']),
350+
...inheritAttributes(this.el, ['data-form-type', 'title', 'tabindex', 'dir']),
335351
};
336352
}
337353

0 commit comments

Comments
 (0)