-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reset focus on active element in ShortcutRegistration (#18126)
* feat: reset focus on active element in ShortcutRegistration Adds setResetFocusOnActiveElement, resetFocusOnActiveElement and isResetFocusOnActiveElement to ShortcutRegistration class to optionally reset focus on active element (focused element in browser). Resetting means calling blur() and focus() on the active element to ensure that input fields with ValueChangeMode ON_CHANGE will fire value change event before shortcut handler is run. Updates ClickNotifier#addClickShortcut(Key,KeyModifier...) to reset focus on active element by default for click shortcuts. Fixes: #5959 * test: fixed tests * chore: added null checking and text changes * chore: added window.Vaadin.Flow.resetFocus function * fix: revert addClickShortcut to not reset focus by default Removes a call `setResetFocusOnActiveElement(true)` in `addClickShortcut` method in `ClickNotifier` to revert default behavior changes from #17826. Fixes: #18048 * chore: update Reset focus on active element to work in 23 Adjusted reset focus script snippet from Flow 24 to work with 23. * chore: formatting --------- Co-authored-by: Mikhail Shabarov <[email protected]>
- Loading branch information
Showing
10 changed files
with
347 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...t/src/main/java/com/vaadin/flow/uitest/ui/ShadowRootShortcutsWithValueChangeModeView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2000-2023 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.vaadin.flow.uitest.ui; | ||
|
||
import com.vaadin.flow.component.Key; | ||
import com.vaadin.flow.component.KeyModifier; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.Input; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.component.html.Paragraph; | ||
import com.vaadin.flow.data.value.ValueChangeMode; | ||
import com.vaadin.flow.dom.Element; | ||
import com.vaadin.flow.dom.ElementFactory; | ||
import com.vaadin.flow.dom.ShadowRoot; | ||
import com.vaadin.flow.router.AfterNavigationEvent; | ||
import com.vaadin.flow.router.AfterNavigationObserver; | ||
import com.vaadin.flow.router.Route; | ||
import com.vaadin.flow.uitest.servlet.ViewTestLayout; | ||
|
||
@Route(value = "com.vaadin.flow.uitest.ui.ShadowRootShortcutsWithValueChangeModeView", layout = ViewTestLayout.class) | ||
public class ShadowRootShortcutsWithValueChangeModeView extends Div | ||
implements AfterNavigationObserver { | ||
|
||
private final Input input; | ||
|
||
public ShadowRootShortcutsWithValueChangeModeView() { | ||
setId("test-element"); | ||
|
||
ShadowRoot shadowRoot = getElement().attachShadow(); | ||
Element shadowDiv = ElementFactory.createDiv(); | ||
shadowDiv.setText("Div inside shadow DOM"); | ||
shadowDiv.setAttribute("id", "shadow-div"); | ||
|
||
input = new Input(); | ||
input.setId("input"); | ||
shadowDiv.appendChild(input.getElement()); | ||
|
||
NativeButton button = new NativeButton("Report value"); | ||
button.setId("button"); | ||
|
||
Paragraph value = new Paragraph(); | ||
value.setId("value"); | ||
value.setText(""); | ||
|
||
input.setValueChangeMode(ValueChangeMode.LAZY); | ||
// make this really big to make testing easier | ||
input.setValueChangeTimeout(3000); | ||
|
||
// clickShortcutWorks | ||
button.setText( | ||
"Button triggered by CTRL + ALT + S and CTRL + ENTER (with reset focus)"); | ||
button.addClickShortcut(Key.KEY_S, KeyModifier.CONTROL, | ||
KeyModifier.ALT); | ||
button.addClickShortcut(Key.ENTER, KeyModifier.CONTROL) | ||
.setResetFocusOnActiveElement(true); | ||
button.addClickListener(e -> value.setText(input.getValue())); | ||
|
||
shadowRoot.appendChild(shadowDiv); | ||
shadowRoot.appendChild(button.getElement()); | ||
shadowRoot.appendChild(value.getElement()); | ||
} | ||
|
||
@Override | ||
public void afterNavigation(AfterNavigationEvent event) { | ||
String valueChangeMode = event.getLocation().getQueryParameters() | ||
.getQueryString(); | ||
if (valueChangeMode != null && !valueChangeMode.isBlank()) { | ||
input.setValueChangeMode(ValueChangeMode.valueOf(valueChangeMode)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.