mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-02-04 13:16:08 +08:00
fix: prevent navigating when button is disabled
This commit is contained in:
@@ -35,8 +35,8 @@ export class NavLink implements ComponentInterface {
|
||||
|
||||
private onClick = () => {
|
||||
const { el } = this;
|
||||
const button = el.querySelector('button,ion-button,a');
|
||||
if (button?.hasAttribute('disabled')) {
|
||||
const button = el.querySelector<HTMLIonButtonElement>('ion-button');
|
||||
if (button?.disabled) {
|
||||
// Do not navigate if the button is disabled
|
||||
return;
|
||||
}
|
||||
|
||||
32
core/src/components/nav-link/test/nav-link.spec.tsx
Normal file
32
core/src/components/nav-link/test/nav-link.spec.tsx
Normal 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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user