mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-02-22 01:29:39 +08:00
feat: Trigger beforeInputUpdate when a candidate is selected in candidate box
This commit is contained in:
parent
aa9f98e73e
commit
9f917fea47
@ -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 =
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user