fix(input): ion-input accepts any string value (#24606)

Resolves #19884
This commit is contained in:
Sean Perkins
2022-01-20 12:47:50 -05:00
committed by GitHub
parent 5925e7608e
commit 43c5977d48
3 changed files with 71 additions and 0 deletions

View File

@@ -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() });
}

View File

@@ -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');
});

View File

@@ -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>