Skip to content

Commit 1b8bab1

Browse files
committed
fix #1104 Datatable Number Header Filters: invalidValueException is shown when - is entered without entering a number before the DelayedHeaderFilterInput triggers
1 parent f7dd44d commit 1b8bab1

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

domino-ui/src/main/java/org/dominokit/domino/ui/forms/NumberBox.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import elemental2.dom.ClipboardEvent;
2424
import elemental2.dom.Event;
2525
import elemental2.dom.HTMLInputElement;
26-
import elemental2.dom.KeyboardEvent;
26+
import elemental2.dom.InputEvent;
2727
import java.util.Objects;
2828
import java.util.Optional;
2929
import java.util.function.Function;
@@ -32,6 +32,7 @@
3232
import org.dominokit.domino.ui.events.EventType;
3333
import org.dominokit.domino.ui.forms.validations.InputAutoValidator;
3434
import org.dominokit.domino.ui.forms.validations.ValidationResult;
35+
import org.dominokit.domino.ui.keyboard.KeyEvent;
3536
import org.dominokit.domino.ui.utils.ApplyFunction;
3637
import org.dominokit.domino.ui.utils.DominoElement;
3738
import org.dominokit.domino.ui.utils.HasMinMaxValue;
@@ -98,8 +99,8 @@ public NumberBox() {
9899
setAutoValidation(true);
99100
enableFormatting();
100101

101-
getInputElement().addEventListener(EventType.keydown, this::onKeyDown);
102102
getInputElement().addEventListener(EventType.paste, this::onPaste);
103+
getInputElement().addEventListener(EventType.keydown, this::onKeyDown);
103104
}
104105

105106
/**
@@ -214,23 +215,43 @@ protected String createKeyMatch() {
214215
}
215216

216217
/**
217-
* Handles the "keypress" event for the number box. Only allows key presses that match the pattern
218-
* created by the {@link #createKeyMatch()} method.
218+
* Handles the key-down event and processes user input accordingly. This method prevents invalid
219+
* characters, handles paste operations, and ensures that only specific keys are allowed based on
220+
* the defined criteria.
219221
*
220-
* @param event The keyboard event associated with the key press.
222+
* @param event the event triggered on key-down, representing the user's input action
221223
*/
222224
protected void onKeyDown(Event event) {
223-
KeyboardEvent keyboardEvent = Js.uncheckedCast(event);
224-
if (keyboardEvent.key.length() == 1 && !keyboardEvent.key.matches(createKeyMatch())) {
225-
event.preventDefault();
225+
clearInvalid();
226+
KeyEvent keyEvent = KeyEvent.of(Js.uncheckedCast(event));
227+
228+
if (keyEvent.isModifierKey()) {
229+
return;
230+
}
231+
232+
if (keyEvent.getNativeKey().length() != 1) {
233+
return;
226234
}
235+
236+
if (keyEvent.getNativeKey().matches(createKeyMatch())) {
237+
return;
238+
}
239+
240+
if (event instanceof InputEvent) {
241+
InputEvent inputEvent = Js.uncheckedCast(event);
242+
if ("insertFromPaste".equals(inputEvent.inputType)) {
243+
return;
244+
}
245+
}
246+
247+
event.preventDefault();
227248
}
228249

229250
/**
230251
* Handles the "paste" event for the number box. It ensures that pasted value is of a valid
231252
* format. If the pasted value is not a valid number, the event is prevented.
232253
*
233-
* @param event The clipboard event associated with the paste action.
254+
* @param event The clipboard event associated with the paste action.-2
234255
*/
235256
protected void onPaste(Event event) {
236257
ClipboardEvent clipboardEvent = Js.uncheckedCast(event);

domino-ui/src/main/java/org/dominokit/domino/ui/keyboard/KeyEvent.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public KeyEvent(KeyboardEvent event) {
5959
public KeyboardEvent getEvent() {
6060
return event;
6161
}
62+
6263
/**
6364
* Returns the normalized key code.
6465
*
@@ -67,6 +68,16 @@ public KeyboardEvent getEvent() {
6768
public KeyboardKeyCode getCode() {
6869
return code;
6970
}
71+
72+
/**
73+
* Retrieves the raw code value from the underlying DOM event.
74+
*
75+
* @return the value of the code property from the DOM KeyboardEvent
76+
*/
77+
public String getNativeCode() {
78+
return event.code;
79+
}
80+
7081
/**
7182
* Returns the normalized key value.
7283
*
@@ -76,6 +87,15 @@ public KeyboardKey getKey() {
7687
return key;
7788
}
7889

90+
/**
91+
* Retrieves the raw key value from the underlying DOM event.
92+
*
93+
* @return the value of the key property from the DOM KeyboardEvent
94+
*/
95+
public String getNativeKey() {
96+
return event.key;
97+
}
98+
7999
/**
80100
* Checks whether the event code matches the provided code.
81101
*

0 commit comments

Comments
 (0)