Skip to content

Commit 206826a

Browse files
committed
[FIX] pivot: mouse events do not work in pivot text input
We added a behaviour to select the whole text of the measure name input when the input is focused. We select the whole input text on `focus` event, but we had to stop/prevent the `mouseup` events, otherwise the event would sometime unselect the text. It turns out (surprise) that entirely disabling `mouseup` event on the input was a bad idea: it prevented the user from moving the cursor inside the input when all the text was selected. This commit changes the way we handle all this: - if the input is not focused, we disable the `mousedown` event on it. This prevent the `focus` event from firing. - if the input is not focused and we catch a `mouseup` event on the input, we select the whole text and focus the input manually. - if the input is focused, we do not touch the events and let the browser handle them. closes #5414 Task: 4350439 X-original-commit: 85aaab6 Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
1 parent 8fcd63b commit 206826a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/components/text_input/text_input.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,21 @@ export class TextInput extends Component<Props, SpreadsheetChildEnv> {
8888
this.inputRef.el?.blur();
8989
}
9090

91-
focusInputAndSelectContent() {
92-
const inputEl = this.inputRef.el;
93-
if (!inputEl) return;
91+
onMouseDown(ev: MouseEvent) {
92+
// Stop the event if the input is not focused, we handle everything in onMouseUp
93+
if (ev.target !== document.activeElement) {
94+
ev.preventDefault();
95+
ev.stopPropagation();
96+
}
97+
}
9498

95-
// The onFocus event selects all text in the input.
96-
// The subsequent mouseup event can deselect this text,
97-
// so t-on-mouseup.prevent.stop is used to prevent this
98-
// default behavior and preserve the selection.
99-
inputEl.focus();
100-
inputEl.select();
99+
onMouseUp(ev: MouseEvent) {
100+
const target = ev.target as HTMLInputElement;
101+
if (target !== document.activeElement) {
102+
target.focus();
103+
target.select();
104+
ev.preventDefault();
105+
ev.stopPropagation();
106+
}
101107
}
102108
}

src/components/text_input/text_input.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
t-att-placeholder="props.placeholder"
1010
t-att-value="props.value"
1111
t-on-change="save"
12-
t-on-focus="focusInputAndSelectContent"
1312
t-on-blur="save"
14-
t-on-mouseup.prevent.stop=""
13+
t-on-pointerdown="onMouseDown"
14+
t-on-pointerup="onMouseUp"
1515
t-on-keydown="onKeyDown"
1616
/>
1717
</div>

tests/components/text_input.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Component, xml } from "@odoo/owl";
22
import { SpreadsheetChildEnv } from "../../src";
33
import { TextInput } from "../../src/components/text_input/text_input";
4-
import { click, keyDown, setInputValueAndTrigger } from "../test_helpers/dom_helper";
4+
import {
5+
click,
6+
keyDown,
7+
setInputValueAndTrigger,
8+
triggerMouseEvent,
9+
} from "../test_helpers/dom_helper";
510
import { mountComponent } from "../test_helpers/helpers";
611

712
let fixture: HTMLElement;
@@ -64,9 +69,9 @@ describe("TextInput", () => {
6469
expect(onChange).toHaveBeenCalledWith("world");
6570
});
6671

67-
test("selects input content upon focus", async () => {
72+
test("selects input content upon mouseup", async () => {
6873
await mountTextInput({ value: "hello", onChange: () => {} });
69-
fixture.querySelector("input")!.focus();
74+
triggerMouseEvent("input", "pointerup");
7075
expect(fixture.querySelector("input")!.selectionStart).toEqual(0);
7176
expect(fixture.querySelector("input")!.selectionEnd).toEqual(5);
7277
});

0 commit comments

Comments
 (0)