|
23 | 23 | import elemental2.dom.ClipboardEvent; |
24 | 24 | import elemental2.dom.Event; |
25 | 25 | import elemental2.dom.HTMLInputElement; |
26 | | -import elemental2.dom.KeyboardEvent; |
| 26 | +import elemental2.dom.InputEvent; |
27 | 27 | import java.util.Objects; |
28 | 28 | import java.util.Optional; |
29 | 29 | import java.util.function.Function; |
|
32 | 32 | import org.dominokit.domino.ui.events.EventType; |
33 | 33 | import org.dominokit.domino.ui.forms.validations.InputAutoValidator; |
34 | 34 | import org.dominokit.domino.ui.forms.validations.ValidationResult; |
| 35 | +import org.dominokit.domino.ui.keyboard.KeyEvent; |
35 | 36 | import org.dominokit.domino.ui.utils.ApplyFunction; |
36 | 37 | import org.dominokit.domino.ui.utils.DominoElement; |
37 | 38 | import org.dominokit.domino.ui.utils.HasMinMaxValue; |
@@ -98,8 +99,8 @@ public NumberBox() { |
98 | 99 | setAutoValidation(true); |
99 | 100 | enableFormatting(); |
100 | 101 |
|
101 | | - getInputElement().addEventListener(EventType.keydown, this::onKeyDown); |
102 | 102 | getInputElement().addEventListener(EventType.paste, this::onPaste); |
| 103 | + getInputElement().addEventListener(EventType.keydown, this::onKeyDown); |
103 | 104 | } |
104 | 105 |
|
105 | 106 | /** |
@@ -214,23 +215,43 @@ protected String createKeyMatch() { |
214 | 215 | } |
215 | 216 |
|
216 | 217 | /** |
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. |
219 | 221 | * |
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 |
221 | 223 | */ |
222 | 224 | 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; |
226 | 234 | } |
| 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(); |
227 | 248 | } |
228 | 249 |
|
229 | 250 | /** |
230 | 251 | * Handles the "paste" event for the number box. It ensures that pasted value is of a valid |
231 | 252 | * format. If the pasted value is not a valid number, the event is prevented. |
232 | 253 | * |
233 | | - * @param event The clipboard event associated with the paste action. |
| 254 | + * @param event The clipboard event associated with the paste action.-2 |
234 | 255 | */ |
235 | 256 | protected void onPaste(Event event) { |
236 | 257 | ClipboardEvent clipboardEvent = Js.uncheckedCast(event); |
|
0 commit comments