From c47a16d6c3c57f56b053727dd10ead0bfe547e50 Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Thu, 18 Jan 2024 13:15:22 -0500 Subject: [PATCH] fix(datetime): enter closes keyboard when typing time (#28848) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue number: resolves #28325 --------- ## What is the current behavior? When typing the time into the date picker pressing "Enter" does not close the on-screen keyboard. ## What is the new behavior? - Pressing "Enter" closes the on-screen keyboard ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information Test: ⚠️ While I have a test for this, please also test this on a physical Android device. 1. Go to `src/components/datetime/test/basic` 2. Open the time picker (in any of the date times) 3. Tap the time to open the keyboard 4. Press "Enter" on Android. Observe that the keyboard closes. Dev build: `7.6.6-dev.11705528328.1ef5e17b` --- .../picker-internal/picker-internal.tsx | 16 ++++++++ .../keyboard-entry/picker-internal.e2e.ts | 39 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/core/src/components/picker-internal/picker-internal.tsx b/core/src/components/picker-internal/picker-internal.tsx index af696d21b5..be57540567 100644 --- a/core/src/components/picker-internal/picker-internal.tsx +++ b/core/src/components/picker-internal/picker-internal.tsx @@ -265,6 +265,7 @@ export class PickerInternal implements ComponentInterface { inputEl.focus(); } else { + // TODO FW-5900 Use keydown instead el.addEventListener('keypress', this.onKeyPress); this.destroyKeypressListener = () => { el.removeEventListener('keypress', this.onKeyPress); @@ -550,6 +551,21 @@ export class PickerInternal implements ComponentInterface { tabindex={-1} inputmode="numeric" type="number" + onKeyDown={(ev: KeyboardEvent) => { + /** + * The "Enter" key represents + * the user submitting their time + * selection, so we should blur the + * input (and therefore close the keyboard) + * + * Updating the picker's state to no longer + * be in input mode is handled in the onBlur + * callback below. + */ + if (ev.key === 'Enter') { + this.inputEl?.blur(); + } + }} ref={(el) => (this.inputEl = el)} onInput={() => this.onInputChange()} onBlur={() => this.exitInputMode()} diff --git a/core/src/components/picker-internal/test/keyboard-entry/picker-internal.e2e.ts b/core/src/components/picker-internal/test/keyboard-entry/picker-internal.e2e.ts index e9b0239fa6..d69fac2ad9 100644 --- a/core/src/components/picker-internal/test/keyboard-entry/picker-internal.e2e.ts +++ b/core/src/components/picker-internal/test/keyboard-entry/picker-internal.e2e.ts @@ -133,5 +133,44 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(ionChange).toHaveReceivedEventDetail({ text: '00', value: 12 }); await expect(column).toHaveJSProperty('value', 12); }); + test('pressing Enter should dismiss the keyboard', async ({ page }) => { + test.info().annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/28325', + }); + await page.setContent( + ` + + + + + + `, + config + ); + + const column = page.locator('ion-picker-column-internal'); + await column.click(); + + const input = page.locator('ion-picker-internal input'); + await expect(input).toBeFocused(); + + // pressing Enter should blur the input and therefore close the keyboard + await page.keyboard.press('Enter'); + + await expect(input).not.toBeFocused(); + }); }); });