Skip to content

Commit

Permalink
fix #983 Keypress is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Dec 2, 2024
1 parent ecab400 commit f0637f9
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ public interface EventType {
/** Represents the "keydown" event type. */
EventType keydown = () -> "keydown";

/** Represents the "keypress" event type. */
EventType keypress = () -> "keypress";
/**
* Represents the "keypress" event type.
*
* @deprecated use keydown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated EventType keypress = () -> "keypress";

/** Represents the "keyup" event type. */
EventType keyup = () -> "keyup";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public DateBox(
onDetached(mutationRecord -> popover.close());

getInputElement()
.onKeyPress(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
.onKeyDown(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
addValidator(
component -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public DateRangeBox(
onDetached(mutationRecord -> popover.close());

getInputElement()
.onKeyPress(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
.onKeyDown(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
addValidator(
component -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,11 @@ public FieldsGrouping onKeyUp(KeyEventsConsumer handler) {
*
* @param handler The event handler to execute on key press events.
* @return The current `FieldsGrouping` instance for method chaining.
* @deprecated use keydown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated
public FieldsGrouping onKeyPress(KeyEventsConsumer handler) {
HTMLElement[] elements = getInputElements();
Arrays.stream(elements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public InputFieldInitializer<T, V, E> init(HasInputElement<T, E> hasInput) {
countableElement.updateCounter(
hasLength.getLength(), countableElement.getMaxCount()));
}
inputElement.onKeyPress(
inputElement.onKeyDown(
keyEvents -> {
keyEvents.onEnter(
evt -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.utils.Domino.*;
import static org.dominokit.domino.ui.utils.Domino.div;
import static org.dominokit.domino.ui.utils.Domino.input;

Expand Down Expand Up @@ -89,7 +88,7 @@ public NumberBox() {
setAutoValidation(true);
enableFormatting();

getInputElement().addEventListener(EventType.keypress, this::onKeyPress);
getInputElement().addEventListener(EventType.keydown, this::onKeyDown);
getInputElement().addEventListener(EventType.paste, this::onPaste);
}

Expand Down Expand Up @@ -210,7 +209,7 @@ protected String createKeyMatch() {
*
* @param event The keyboard event associated with the key press.
*/
protected void onKeyPress(Event event) {
protected void onKeyDown(Event event) {
KeyboardEvent keyboardEvent = Js.uncheckedCast(event);
if (!keyboardEvent.key.matches(createKeyMatch())) event.preventDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public TimeBox(Date date, DateTimeFormatInfo dateTimeFormatInfo) {
onDetached(mutationRecord -> popover.close());

getInputElement()
.onKeyPress(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
.onKeyDown(keyEvents -> keyEvents.onEnter(evt -> doOpen()).onSpace(evt -> doOpen()));
addValidator(
component -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public AbstractSelect() {
.addEventListener("input", evt -> onTypingStart())
.addCss(dui_auto_type_input, dui_hidden)
.setTabIndex(-1)
.onKeyPress(keyEvents -> keyEvents.alphanumeric(Event::stopPropagation)));
.onKeyDown(keyEvents -> keyEvents.alphanumeric(Event::stopPropagation)));

DelayedTextInput.create(typingElement, getTypeAheadDelay())
.setDelayedAction(
Expand All @@ -126,7 +126,7 @@ public AbstractSelect() {
DomGlobal.setTimeout(p0 -> optionsMenu.focusFirstMatch(token), 0);
});

onKeyPress(
onKeyDown(
keyEvents -> {
keyEvents.alphanumeric(
evt -> {
Expand Down Expand Up @@ -192,7 +192,7 @@ public AbstractSelect() {
evt.stopPropagation();
openOptionMenu();
})
.onKeyPress(
.onKeyDown(
keyEvents ->
keyEvents.onEnter(
evt -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ public interface HasKeyboardEvents<T> {
*
* @param onKeyPress The {@link KeyEventsConsumer} to call when a key is pressed and held down.
* @return The instance of type {@code T} for method chaining.
* @deprecated use keydown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated
T onKeyPress(KeyEventsConsumer onKeyPress);

/**
* Stops listening for key press and hold events.
*
* @return The instance of type {@code T} for method chaining.
* @deprecated use keydown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated
T stopOnKeyPress();

/**
Expand All @@ -83,6 +91,21 @@ public interface HasKeyboardEvents<T> {
*/
T stopOnInput();

/**
* Registers an event listener to be called when an beforeinput is applied.
*
* @param onBeforeInput The {@link KeyEventsConsumer} to call when an beforeinput is applied.
* @return The instance of type {@code T} for method chaining.
*/
T onBeforeInput(KeyEventsConsumer onBeforeInput);

/**
* Stops listening for beforeinput events.
*
* @return The instance of type {@code T} for method chaining.
*/
T stopOnBeforeInput();

/**
* Gets the keyboard event options associated with this instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ public class KeyboardEvents<T extends Node>
/** The constant representing the "keyup" event type. */
public static final String KEYUP = "keyup";

/** The constant representing the "keypress" event type. */
public static final String KEYPRESS = "keypress";
/**
* The constant representing the "keypress" event type.
*
* @deprecated use keydown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated public static final String KEYPRESS = "keypress";

public static final String INPUT = "input";
public static final String BEFORE_INPUT = "beforeinput";

private KeyboardEventOptions defaultOptions = KeyboardEventOptions.create();
private final T element;
Expand All @@ -51,11 +58,13 @@ public class KeyboardEvents<T extends Node>
private KeyboardKeyListener keyDownListener = new KeyboardKeyListener(this);
private KeyboardKeyListener keyPressListener = new KeyboardKeyListener(this);
private KeyboardKeyListener inputListener = new KeyboardKeyListener(this);
private KeyboardKeyListener beforeinputListener = new KeyboardKeyListener(this);

private LazyInitializer keyUpListenerInitializer;
private LazyInitializer keyDownListenerInitializer;
private LazyInitializer keyPressListenerInitializer;
private LazyInitializer InputListenerInitializer;
private LazyInitializer inputListenerInitializer;
private LazyInitializer beforeInputListenerInitializer;

/**
* Creates a new instance of {@code KeyboardEvents} for the specified DOM element.
Expand All @@ -70,8 +79,10 @@ public KeyboardEvents(T element) {
new LazyInitializer(() -> element.addEventListener(KEYDOWN, keyDownListener));
keyPressListenerInitializer =
new LazyInitializer(() -> element.addEventListener(KEYPRESS, keyPressListener));
InputListenerInitializer =
inputListenerInitializer =
new LazyInitializer(() -> element.addEventListener(INPUT, inputListener));
beforeInputListenerInitializer =
new LazyInitializer(() -> element.addEventListener(BEFORE_INPUT, beforeinputListener));
}

/**
Expand Down Expand Up @@ -128,7 +139,11 @@ public KeyboardEvents<T> stopListenOnKeyUp() {
*
* @param onKeyPress The consumer that will receive keypress events.
* @return This {@code KeyboardEvents} instance for method chaining.
* @deprecated use listenOnKeyDown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated
public KeyboardEvents<T> listenOnKeyPress(KeyEventsConsumer onKeyPress) {
keyPressListenerInitializer.apply();
onKeyPress.accept(keyPressListener);
Expand All @@ -139,34 +154,62 @@ public KeyboardEvents<T> listenOnKeyPress(KeyEventsConsumer onKeyPress) {
* Removes the keypress event listener from the element.
*
* @return This {@code KeyboardEvents} instance for method chaining.
* @deprecated use listenOnKeyDown instead.
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div">Element: keypress
* event </a>MDN Web Docs (div element)</a>
*/
@Deprecated
public KeyboardEvents<T> stopListenOnKeyPress() {
element.removeEventListener(KEYPRESS, keyPressListener);
keyPressListenerInitializer.reset();
return this;
}

/**
* Adds a keypress event listener to the element and associates it with the provided {@code
* onInput} consumer.
* Adds a input event listener to the element and associates it with the provided {@code onInput}
* consumer.
*
* @param onInput The consumer that will receive keypress events.
* @param onInput The consumer that will receive input events.
* @return This {@code KeyboardEvents} instance for method chaining.
*/
public KeyboardEvents<T> listenOnInput(KeyEventsConsumer onInput) {
InputListenerInitializer.apply();
inputListenerInitializer.apply();
onInput.accept(inputListener);
return this;
}

/**
* Removes the keypress event listener from the element.
* Adds a keypress event listener to the element and associates it with the provided {@code
* onBeforeInput} consumer.
*
* @param onBeforeInput The consumer that will receive beforeinput events.
* @return This {@code KeyboardEvents} instance for method chaining.
*/
public KeyboardEvents<T> listenOnBeforeInput(KeyEventsConsumer onBeforeInput) {
beforeInputListenerInitializer.apply();
onBeforeInput.accept(beforeinputListener);
return this;
}

/**
* Removes the input event listener from the element.
*
* @return This {@code KeyboardEvents} instance for method chaining.
*/
public KeyboardEvents<T> stopListenOnInput() {
element.removeEventListener(INPUT, inputListener);
InputListenerInitializer.reset();
inputListenerInitializer.reset();
return this;
}

/**
* Removes the beforeinput event listener from the element.
*
* @return This {@code KeyboardEvents} instance for method chaining.
*/
public KeyboardEvents<T> stopListenOnBeforeInput() {
element.removeEventListener(BEFORE_INPUT, beforeinputListener);
beforeInputListenerInitializer.reset();
return this;
}

Expand Down
13 changes: 10 additions & 3 deletions domino-ui/src/main/java/org/dominokit/domino/ui/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public Menu() {
onAddMissingElement();
};

addClickListener(evt -> evt.stopPropagation());
addClickListener(Event::stopPropagation);

onKeyDown(
keyEvents -> {
Expand Down Expand Up @@ -1405,16 +1405,23 @@ private void doOpen(boolean focus) {
searchBox.get().clearSearch();
}
triggerOpenListeners(this);
boolean shouldFocus = focus;
onAttached(
mutationRecord -> {
position();
if (focus) {
if (shouldFocus) {
focus();
}
elementOf(getMenuAppendTarget()).onDetached(targetDetach -> close());
});
appendStrategy.onAppend(getMenuAppendTarget(), element.element());
onDetached(record -> close());
onDetached(
record -> {
close();
if (isDropDown()) {
triggerCloseListeners(this);
}
});
if (smallScreen && nonNull(parent) && parent.isDropDown()) {
parent.collapse();
menuHeader.get().insertFirst(backArrowContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public SearchBox setAutoSearch(boolean autoSearch) {
autoSearchTimer.cancel();
}

textBox.onKeyPress(keyEvents -> keyEvents.onEnter(evt -> doSearch()));
textBox.onKeyDown(keyEvents -> keyEvents.onEnter(evt -> doSearch()));

return this;
}
Expand Down
Loading

0 comments on commit f0637f9

Please sign in to comment.