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
5 changes: 5 additions & 0 deletions .changeset/tidy-houses-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/web-elements": patch
---

Fix x-input number type forwarding to the inner input element.
1 change: 1 addition & 0 deletions .github/lynx-stack.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ applyTo: "packages/web-platform/web-elements/**/*"

When updating web element APIs, add targeted Playwright tests in packages/web-platform/web-elements/tests/web-elements.spec.ts and keep changes minimal.
Ensure Playwright browsers are installed (pnpm exec playwright install --with-deps <browser>) before running web-elements tests.
For x-input type="number" in web-elements, keep inner input type as text, set inputmode="decimal", and filter number input internally without setting input-filter explicitly.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class XInputEvents
#dom: HTMLElement;

#sendComposingInput = false;
#numberInputFilter = /[^0-9.]|\.(?=.*\.)/g;

#getInputElement = genDomGetter<HTMLInputElement>(
() => this.#dom.shadowRoot!,
Expand Down Expand Up @@ -75,10 +76,7 @@ export class XInputEvents

#teleportInput = (event: InputEvent) => {
const input = this.#getInputElement();
const inputFilter = this.#dom.getAttribute('input-filter');
const filterValue = inputFilter
? input.value.replace(new RegExp(inputFilter, 'g'), '')
: input.value;
const filterValue = this.#filterInputValue(input.value);
const isComposing = event.isComposing;
input.value = filterValue;
if (isComposing && !this.#sendComposingInput) return;
Expand All @@ -101,10 +99,7 @@ export class XInputEvents

#teleportCompositionendInput = () => {
const input = this.#getInputElement();
const inputFilter = this.#dom.getAttribute('input-filter');
const filterValue = inputFilter
? input.value.replace(new RegExp(inputFilter, 'g'), '')
: input.value;
const filterValue = this.#filterInputValue(input.value);
input.value = filterValue;
// if #sendComposingInput set true, #teleportInput will send detail
if (!this.#sendComposingInput) {
Expand All @@ -126,6 +121,18 @@ export class XInputEvents
}
};

#filterInputValue(value: string) {
let filterValue = value;
if (this.#dom.getAttribute('type') === 'number') {
filterValue = filterValue.replace(this.#numberInputFilter, '');
}
const inputFilter = this.#dom.getAttribute('input-filter');
if (inputFilter) {
filterValue = filterValue.replace(new RegExp(inputFilter, 'g'), '');
}
return filterValue;
}

@registerEventEnableStatusChangeHandler('selection')
_handleEnableSelectionEvent(status: boolean) {
if (status) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<!-- Copyright 2024 The Lynx Authors. All rights reserved.
Licensed under the Apache License Version 2.0 that can be found in the
LICENSE file in the root directory of this source tree. -->
<html>
<head>
<meta charset="utf-8">
<title>web playground</title>
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="default" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="portrait" name="screen-orientation">
<meta
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"
name="viewport"
>
<meta content="portrait" name="x5-orientation" />

<link
href="/main.css"
rel="stylesheet"
/>
</head>

<body>
<script src="/main.js" defer></script>
<x-view class="page">
<x-input style="height: 50px; width: 200px" type="number"></x-input>
</x-view>
</body>
</html>
14 changes: 12 additions & 2 deletions packages/web-platform/web-elements/tests/web-elements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,16 @@ test.describe('web-elements test suite', () => {
},
);

test(
'attribute-type-number-inner-input-type',
async ({ page }, { titlePath }) => {
const title = getTitle(titlePath);
await gotoWebComponentPage(page, title);
const inputType = await page.locator('input').getAttribute('type');
expect(inputType).toBe('text');
},
);

test(
'attribute-type-tel',
async ({ page }, { titlePath, title: simpleTitle }) => {
Expand Down Expand Up @@ -2811,9 +2821,9 @@ test.describe('web-elements test suite', () => {
return detail;
});
await page.mouse.click(100, 25);
await page.keyboard.type('2.');
await page.keyboard.type('1.2a');
await wait(200);
expect((await confirmValue.jsonValue()).value).toBe('2.');
expect((await confirmValue.jsonValue()).value).toBe('1.2');
},
);

Expand Down
Loading