Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions core/src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ export class Input implements ComponentInterface {
*/
@Watch('value')
protected valueChanged() {
if (this.nativeInput) {
/**
* Assigning the native input's value on attribute
* value change, allows `ionInput` implementations
* to override the control's value.
*
* Used for patterns such as input trimming (removing whitespace),
* or input masking.
*/
this.nativeInput.value = this.getValue();
}
this.emitStyle();
this.ionChange.emit({ value: this.value == null ? this.value : this.value.toString() });
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/components/input/test/masking/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { newE2EPage } from '@stencil/core/testing';

test('input: masking', async () => {
const page = await newE2EPage({
url: '/src/components/input/test/masking?ionic:_testing=true'
});

const inputTrimmed = await page.find('#inputTrimmed');

await inputTrimmed.click();

await page.keyboard.type('S p a c e s');

const currentValue = await page.$eval('#inputTrimmed', (el: any) => {
return el.value;
});

expect(currentValue).toEqual('Spaces');

});
40 changes: 40 additions & 0 deletions core/src/components/input/test/masking/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<title>Input - Masking</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet">
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet">
<script src="../../../../../scripts/testing/scripts.js"></script>
<script nomodule src="../../../../../dist/ionic/ionic.js"></script>
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
</head>

<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Input - Masking</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-item>
<ion-label position="stacked">Input with trimming</ion-label>
<ion-input id="inputTrimmed" placeholder="Trimmed Input" />
</ion-item>
</ion-content>

<script>
document
.getElementById('inputTrimmed')
.addEventListener('ionInput', e => {
e.target.value = e.target.value.trim();
});
</script>
</ion-app>
</body>

</html>