Skip to content

Commit 4cfe210

Browse files
committed
fix(input): copy custom attrs from ion-input to native input
1 parent 015361d commit 4cfe210

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

ionic/components/input/input-base.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {Item} from '../item/item';
88
import {IonicApp} from '../app/app';
99
import {isTrueProperty} from '../../util/util';
1010
import {Label} from '../label/label';
11-
import {pointerCoord, hasPointerMoved, closest} from '../../util/dom';
11+
import {pointerCoord, hasPointerMoved, closest, copyInputAttributes} from '../../util/dom';
1212
import {NavController} from '../nav/nav-controller';
1313
import {NativeInput, NextInput} from './native-input';
1414
import {Platform} from '../../platform/platform';
@@ -132,7 +132,7 @@ export class InputBase {
132132
if (val) {
133133
val = val.toLowerCase();
134134

135-
if (/password|email|number|search|tel|url|date|datetime|datetime-local|month|time|week/.test(val)) {
135+
if (/password|email|number|search|tel|url|date|month|time|week/.test(val)) {
136136
this._type = val;
137137
}
138138
}
@@ -175,6 +175,9 @@ export class InputBase {
175175

176176
this.checkHasValue(nativeInput.getValue());
177177
this.disabled = this._disabled;
178+
179+
// copy ion-input attributes to the native input element
180+
copyInputAttributes(this._elementRef.nativeElement, nativeInput.element());
178181
}
179182

180183
/**

ionic/components/input/native-input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class NativeInput {
127127
/**
128128
* @private
129129
*/
130-
private element(): HTMLInputElement {
130+
element(): HTMLInputElement {
131131
return this._elementRef.nativeElement;
132132
}
133133

ionic/components/input/test/form-inputs/main.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@
9090
<ion-input placeholder="Stand-alone textarea"></ion-input>
9191

9292
<ion-list>
93+
<ion-item>
94+
<ion-label>Custom Attrs</ion-label>
95+
<ion-input autocomplete="off"
96+
spellcheck="no"
97+
required
98+
some-weird-attr="value"
99+
accept="sure"
100+
class="no-copy" id="no-copy" checked=checked
101+
value="copy custom attributes"></ion-input>
102+
</ion-item>
103+
93104
<ion-item>
94105
<ion-label>Disabled Input</ion-label>
95106
<ion-input disabled value="Value"></ion-input>

ionic/util/dom.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ export function hasFocusedTextInput() {
198198
return false;
199199
}
200200

201+
const skipInputAttrsReg = /^(value|checked|disabled|type|class|style|id)$/i
202+
export function copyInputAttributes(srcElement, destElement) {
203+
// copy attributes from one element to another
204+
// however, skip over a few of them as they're already
205+
// handled in the angular world
206+
let attrs = srcElement.attributes;
207+
for (let i = 0; i < attrs.length; i++) {
208+
var attr = attrs[i];
209+
if (!skipInputAttrsReg.test(attr.name)) {
210+
destElement.setAttribute(attr.name, attr.value);
211+
}
212+
}
213+
}
214+
201215
let matchesFn: string;
202216
let matchesMethods: Array<string> = ['matches','webkitMatchesSelector','mozMatchesSelector','msMatchesSelector'];
203217
matchesMethods.some((fn: string) => {

0 commit comments

Comments
 (0)