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

feat: Helper & Error Text slot support in inputs #29100

Closed
3 tasks done
Marius-Romanus opened this issue Feb 29, 2024 · 9 comments
Closed
3 tasks done

feat: Helper & Error Text slot support in inputs #29100

Marius-Romanus opened this issue Feb 29, 2024 · 9 comments
Assignees
Labels

Comments

@Marius-Romanus
Copy link

Marius-Romanus commented Feb 29, 2024

Prerequisites

Describe the Feature Request

Hello!

Previously the helpers and errors were separate tags:

  <div slot="helper">Enter an email</div>
  <div slot="error">Please enter a valid email</div>

with the new inputs this has become helperText and errorText as an attribute within the ion-input itself (errorText).
This is fine if you only have one type of error, but when you have more than one or even custom errors, it forces you to create monstrous things like this:

<ion-input 
[errorText]="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) &&
         getForm['email'].errors?.['required'] ? 'Text error 1' : 
         getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) &&
         getForm['email'].errors?.['pattern'] ?
          'Text error 2' :  ..."
></ion-input>

In that example I have only put required and pattern errors, but there could be many more

It would be nice to at least have a slot to be able to add the if to each corresponding error message and not have to make ternaries within an attribute.

Greetings!

Describe the Use Case

ion-inputs where you may have more than one type of error

Describe Preferred Solution

Something similar to what there is now with label:
https://ionicframework.com/docs/api/input#label-slot-experimental

Describe Alternatives

No response

Related Code

No response

Additional Information

No response

@liamdebeasi
Copy link
Contributor

Thanks for the report. How were you doing things with the deprecated helper/error slots? From the example you provided it looks like you would still need to conditionally render text based on the validation state. For the helprText property, you could extract this logic into a function so you don't need to do nested ternaries.

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Feb 29, 2024
@ionitron-bot ionitron-bot bot removed the triage label Feb 29, 2024
@Marius-Romanus
Copy link
Author

Hello, I have never really used the helper, I have put it because they go together in the documentation (although I suppose it could also be the case that you want different help depending on an if). The one I use is the error, in version 6 it had something similar to this:

<ion-item>
  <ion-label position="floating">Email</ion-label>
  <ion-input formControlName="email" type="email" clearOnEdit="false"></ion-input>
  <ion-icon
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && (getForm['email'].errors?.['required'] || getForm['email'].errors?.['pattern'])"
    class="center-icon-item" slot="end" name="alert-circle" color="danger"></ion-icon>
  <ion-note
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && getForm['email'].errors?.['required']"
    slot="error">email error 1</ion-note>
  <ion-note
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && getForm['email'].errors?.['pattern']"
    slot="error">email error 2</ion-note>
  ...
</ion-item>

Or to make it a little less repetitive:

<ion-item>
  <ion-label position="floating">Email</ion-label>
  <ion-input formControlName="email" type="email" clearOnEdit="false"></ion-input>
  <ng-container *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched)">
    <ion-icon *ngIf="getForm['email'].errors?.['required'] || getForm['email'].errors?.['pattern']"
      class="center-icon-item" slot="end" name="alert-circle" color="danger"></ion-icon>
    <ion-note *ngIf="getForm['email'].errors?.['required']" slot="error">email error 1</ion-note>
    <ion-note *ngIf="getForm['email'].errors?.['pattern']" slot="error">email error 2</ion-note>
    ...
  </ng-container>
</ion-item>

Greetings

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Feb 29, 2024
@Marius-Romanus
Copy link
Author

I add a little context, it would be very similar to the Angular documentation with error handling:

https://angular.io/guide/form-validation#validating-input-in-template-driven-forms

@liamdebeasi
Copy link
Contributor

Does your application architecture support the ability to extract the logic to a helper function? The benefit here is it would let you achieve the desired result and also have a template that is a bit easier to parse. Some rough pseudo code:

<ion-input [errorText]="getErrorText()"></ion-input>
getErrorText() {
  if (email.invalid && (email.dirty || email.touched) && email.errors?.required) {
    return 'email error 1'
  }
  
  ...
}

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Feb 29, 2024
@ionitron-bot ionitron-bot bot removed the triage label Feb 29, 2024
@Marius-Romanus
Copy link
Author

Hello, yes, that logic can be implemented, but does it seem cleaner to us in the html instead of in ts?
At least to me personally it seemed cleaner to have the error messages in the html.

It is also assumed that with zone.js it is better not to have a function in a template that affects performance, but well that would be minimal and I understand that the get form also does the same in terms of performance.
But in cases where the if comes from a variable and not a function, it would improve performance.

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Mar 1, 2024
@liamdebeasi
Copy link
Contributor

Have you tried listening for status changes on the form control? In this callback you can check the validity state. From there, you can assign an error message to a variable. This variable you can then render in the template. That should avoid any performance issues with rendering a function in a template.

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Mar 6, 2024
@ionitron-bot ionitron-bot bot removed the triage label Mar 6, 2024
@Marius-Romanus
Copy link
Author

Hello, I'm sorry I couldn't answer sooner. Yes, it would be another valid option. Personally, I find it more readable and concise as it was done in the past, in HTML. Please consider that option, if you do not feel free to close the issue. :)

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Mar 8, 2024
@liamdebeasi
Copy link
Contributor

Thanks for the added info! We don't have plans to add this new API at the moment because the desired behavior is achievable with the existing API. I am going to close this, but let me know if you have any questions.

@liamdebeasi liamdebeasi closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
Copy link

ionitron-bot bot commented Apr 10, 2024

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Apr 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants