feat: Trigger beforeInputUpdate when a candidate is selected in candidate box

This commit is contained in:
Herbert Lin 2024-05-13 23:30:35 -07:00
parent aa9f98e73e
commit 9f917fea47
2 changed files with 42 additions and 0 deletions

View File

@ -393,6 +393,13 @@ class SimpleKeyboard {
candidateStr = selectedCandidate.normalize("NFD");
}
/**
* Perform an action before any input change
*/
if (typeof this.options.beforeInputUpdate === "function") {
this.options.beforeInputUpdate(this);
}
const currentInput = this.getInput(this.options.inputName, true);
const initialCaretPosition = this.getCaretPositionEnd() || 0;
const inputSubstr =

View File

@ -371,6 +371,41 @@ it('CandidateBox selection should trigger onChange', () => {
keyboard.destroy();
});
it('CandidateBox selection should trigger beforeInputChange', () => {
const keyboard = new Keyboard({
layout: {
default: [
"a b {bksp}"
]
},
layoutCandidates: {
a: "1 2 3 4 5 6"
},
beforeInputUpdate: jest.fn(),
});
let candidateBoxOnItemSelected;
const onSelect = jest.fn().mockImplementation((selectedCandidate) => {
candidateBoxOnItemSelected(selectedCandidate);
keyboard.candidateBox.destroy();
});
const candidateBoxRenderFn = keyboard.candidateBox.renderPage;
jest.spyOn(keyboard.candidateBox, "renderPage").mockImplementation((params) => {
candidateBoxOnItemSelected = params.onItemSelected;
params.onItemSelected = onSelect;
candidateBoxRenderFn(params);
});
keyboard.getButtonElement("a").click();
keyboard.candidateBox.candidateBoxElement.querySelector("li").click();
expect(keyboard.options.beforeInputUpdate.mock.calls[0][0]).toMatchObject(keyboard);
keyboard.destroy();
});
it('CandidateBox normalization will work', () => {
const keyboard = new Keyboard({
layout: {