question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: ios, delegatesFocus always focuses first focusable element

See original GitHub issue

Feature Request

Ionic version:

[x] 5.5.5

Describe the Feature Request Right now, if I add an ion-input inside an ion-item, it takes over the click function of the ion-item. If we add button=false on an ion-item, the expected result is that items placed within, such as a ion-input, will not make the ion-item work like a button.

Describe Preferred Solution It should be possible to deactivate the clickListener of the ion-item in some cases. To be more precise this if should be modified with a new property or using the button attribute: https://github.com/ionic-team/ionic-framework/blob/943e3f6ae37ecc56f21168f057dde77a05e4e144/core/src/components/item/item.tsx#L161.

Describe Alternatives There is currently no other way to implement this behavior. On Android devices, it helps to call stopPropagation and preventDefault in the child-element that was clicked. But on iOS the click event of the ion-item is called anyway so in the following example the button event is triggered, but the ion-input is focused which is quite bad behavior.

Related Code

The following code solves the issue on Android devices (and Chrome browsers) but not works for iOS (and the Safari browser)

<ion-item>
  <ion-label slot="start"> {{ labelText }}</ion-label>
  <ion-input
    type="number"
    [(ngModel)]="amount"
    [placeholder]="placeholder"
  ></ion-input>
  <ion-button
    (click)="minusAmount($event)"
  >
    <ion-icon slot="icon-only" name="remove-circle-outline"></ion-icon>
  </ion-button>
  <ion-button (click)="plusAmount($event)">
    <ion-icon slot="icon-only" name="add-circle-outline"></ion-icon>
  </ion-button>
</ion-item>
  plusAmount($event: MouseEvent) {
    $event.stopPropagation();
    $event.preventDefault();
   ...
  }

  minusAmount($event: MouseEvent) {
    $event.stopPropagation();
    $event.preventDefault();
   ...
  }

Additional Context

https://github.com/ionic-team/ionic-framework/issues/19590 https://forum.ionicframework.com/t/clickable-button-inside-an-ion-item-whereas-the-ion-item-itself-is-also-clickable/79262/3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NLuegcommented, Oct 12, 2021

I actually was able to solve this issue by providing a tabindex of 0 at the items which should not propagate the event to the item (and so to the input). So I adjusted the above code to this:

<ion-item>
  <ion-label slot="start"> {{ labelText }}</ion-label>
  <ion-input
    type="number"
    [(ngModel)]="amount"
    [placeholder]="placeholder"
  ></ion-input>
  <ion-button  tabindex="0"
    (click)="minusAmount($event)"
  >
    <ion-icon slot="icon-only" name="remove-circle-outline"></ion-icon>
  </ion-button>
  <ion-button tabindex="0" (click)="plusAmount($event)">
    <ion-icon slot="icon-only" name="add-circle-outline"></ion-icon>
  </ion-button>
</ion-item>

I actually can’t say why this works but it solves my issue perfectly.

0reactions
ionitron-bot[bot]commented, Nov 11, 2021

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UIAccessibility - Set focus on pre… | Apple Developer Forums
I am implementing accessibility into our current project and have some issues on setting focus on an element in a previous view. An...
Read more >
ShadowRoot.delegatesFocus - Web APIs | MDN
focus () is called on the host element, the first focusable part is given focus, and the shadow host is given any available...
Read more >
Applying :focus CSS to input-field in shadowDOM and
Of course my first thought was to encapsulate the attribute within the shadow DOM (or even toggle a class) on a container element...
Read more >
Handle Focus - Salesforce Lightning Component Library
Handle Focus. To make your components accessible to people with disabilities, program them to handle which element has focus and can receive input....
Read more >
The autofocus attribute - HTML Standard
Being focusable is a statement about whether an element can be focused programmatically, e.g. via the focus() method or autofocus attribute. In contrast, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found