Compare commits

...

2 Commits

Author SHA1 Message Date
Sean Perkins
b45852e9e0 fix: prevent navigating when button is disabled 2023-08-31 14:24:53 -04:00
Sean Perkins
cb6a7ecab0 fix(nav-link): prevent navigation when clicking disabled button 2023-08-30 23:59:25 -04:00
2 changed files with 40 additions and 1 deletions

View File

@@ -34,7 +34,14 @@ export class NavLink implements ComponentInterface {
@Prop() routerAnimation?: AnimationBuilder;
private onClick = () => {
return navLink(this.el, this.routerDirection, this.component, this.componentProps, this.routerAnimation);
const { el } = this;
const button = el.querySelector<HTMLIonButtonElement>('ion-button');
if (button?.disabled) {
// Do not navigate if the button is disabled
return;
}
return navLink(el, this.routerDirection, this.component, this.componentProps, this.routerAnimation);
};
render() {

View File

@@ -0,0 +1,32 @@
import { h } from '@stencil/core';
import { newSpecPage } from '@stencil/core/testing';
import { Button } from '../../button/button';
import { Nav } from '../../nav/nav';
import { NavLink } from '../nav-link';
describe('NavLink', () => {
it('should not navigate when button is disabled', async () => {
const page = await newSpecPage({
components: [Nav, NavLink, Button],
template: () => (
<ion-nav>
<ion-nav-link component="div">
<ion-button disabled>Disabled</ion-button>
</ion-nav-link>
</ion-nav>
),
});
const mock = jest.fn();
const nav = page.body.querySelector('ion-nav')!;
const ionButton = page.body.querySelector('ion-button')!;
nav.addEventListener('ionNavWillChange', mock);
await ionButton.click();
expect(mock).not.toHaveBeenCalled();
});
});