Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve navigated.current value #19539

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function Flow() {
const ref = useRef<HTMLOutputElement>(null);
const navigate = useNavigate();
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
navigated.current = nextLocation.pathname === currentLocation.pathname && nextLocation.search === currentLocation.search && nextLocation.hash === currentLocation.hash;
navigated.current = navigated.current || (nextLocation.pathname === currentLocation.pathname && nextLocation.search === currentLocation.search && nextLocation.hash === currentLocation.hash);
return true;
});
const {pathname, search, hash} = useLocation();
Expand Down Expand Up @@ -200,7 +200,7 @@ function Flow() {

const vaadinNavigateEventHandler = useCallback((event: CustomEvent<{state: unknown, url: string, replace?: boolean}>) => {
const path = '/' + event.detail.url;
navigated.current = true;
navigated.current = !event.detail.replace;
navigate(path, { state: event.detail.state, replace: event.detail.replace});
}, [navigate]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class NavigationView extends Div {

public static final String REACT_ANCHOR_ID = "anchor-react-navigation";
public static final String REACT_ID = "react-navigation";
public static final String SET_PARAMETER_COUNTER_ID = "set-parameter-counter";

public NavigationView() {
Anchor anchorNavigation = new Anchor("com.vaadin.flow.AnchorView",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.WildcardParameter;

@Route("com.vaadin.flow.ServerView")
public class ServerView extends Div {
public class ServerView extends Div implements HasUrlParameter<String> {

private final Span setParameterSpan = new Span();
private int setParameterCount = 0;

public ServerView() {
NativeButton serverNavigation = new NativeButton(
Expand All @@ -31,7 +37,15 @@ public ServerView() {
.navigate(NavigationView.class);
});
serverNavigation.setId(NavigationView.SERVER_ID);
setParameterSpan.setId(NavigationView.SET_PARAMETER_COUNTER_ID);

add(new Span("ServerView"), new Div(), serverNavigation, new Div(),
setParameterSpan);
}

add(new Span("ServerView"), new Div(), serverNavigation);
@Override
public void setParameter(BeforeEvent event,
@WildcardParameter String parameter) {
setParameterSpan.setText("" + ++setParameterCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,15 @@ public void testNavigatedForPostponeView() {
checkNavigatedEvent(
"navigated to /view/com.vaadin.flow.NavigationView");
}

@Test
public void testNavigation_HasUrlParameter_setParameterCalledOnce() {
open();

$(NativeButtonElement.class).id(NavigationView.SERVER_ID).click();
Assert.assertEquals("ServerView",
$(SpanElement.class).first().getText());
Assert.assertEquals("1", $(SpanElement.class)
.id(NavigationView.SET_PARAMETER_COUNTER_ID).getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testHistory() throws URISyntaxException {
Assert.assertEquals(baseUrl, getCurrentUrl());
// idx value in history state is added by react-router
Assert.assertEquals(
Arrays.asList("New location: asdf","New location: com.vaadin.flow.uitest.ui.HistoryView"),
Arrays.asList("New location: com.vaadin.flow.uitest.ui.HistoryView"),
getStatusMessages());
clearButton.click();

Expand Down
Loading