From b2872b7809c8d0fe25ca7bd081da35deb5f720eb Mon Sep 17 00:00:00 2001 From: Francisco Hodge Date: Sat, 25 Feb 2023 13:23:34 -0500 Subject: [PATCH] Add enableLayoutCandidatesKeyPress option. Fixes #1866 --- src/lib/components/Keyboard.ts | 15 ++++++++++++++- src/lib/interfaces.ts | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/components/Keyboard.ts b/src/lib/components/Keyboard.ts index 7a082e14..889eb3aa 100644 --- a/src/lib/components/Keyboard.ts +++ b/src/lib/components/Keyboard.ts @@ -135,6 +135,7 @@ class SimpleKeyboard { * @property {number} layoutCandidatesPageSize Determines size of layout candidate list * @property {boolean} layoutCandidatesCaseSensitiveMatch Determines whether layout candidate match should be case sensitive. * @property {boolean} disableCandidateNormalization Disables the automatic normalization for selected layout candidates + * @property {boolean} enableLayoutCandidatesKeyPress Enables onKeyPress triggering for layoutCandidate items */ this.options = { layoutName: "default", @@ -377,7 +378,11 @@ class SimpleKeyboard { candidateValue, targetElement, onSelect: (selectedCandidate: string, e: MouseEvent) => { - const { layoutCandidatesCaseSensitiveMatch, disableCandidateNormalization } = this.options; + const { + layoutCandidatesCaseSensitiveMatch, + disableCandidateNormalization, + enableLayoutCandidatesKeyPress + } = this.options; let candidateStr = selectedCandidate; @@ -413,6 +418,14 @@ class SimpleKeyboard { this.setInput(newInput, this.options.inputName, true); this.setCaretPosition(newCaretPosition); + /** + * Calling onKeyPress + * We pass in the composed candidate instead of the decomposed one + * To prevent confusion for users + */ + if (enableLayoutCandidatesKeyPress && typeof this.options.onKeyPress === "function") + this.options.onKeyPress(selectedCandidate, e); + if (typeof this.options.onChange === "function") this.options.onChange( this.getInput(this.options.inputName, true), diff --git a/src/lib/interfaces.ts b/src/lib/interfaces.ts index 01fa3026..b15cbe24 100644 --- a/src/lib/interfaces.ts +++ b/src/lib/interfaces.ts @@ -256,6 +256,11 @@ export interface KeyboardOptions { */ disableCandidateNormalization?: boolean; + /** + * Enables onKeyPress triggering for layoutCandidate items + */ + enableLayoutCandidatesKeyPress?: boolean; + /** * Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts). */