-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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: Make ion-input counter formatter more beginner-friendly #28694
Comments
Another possibility could be to use <ion-input
[counter]="true"
maxlength="20"
>
<ng-template
counterFormatter
let-maxLength="maxLength"
let-currentLength="currentLength"
>
<b>{{ maxLength - currentLength }}</b> {{ translated }}
</ng-template>
</ion-input> |
Thanks for the report. Can you please provide a reproduction of the original issues you are seeing? In particular, I'd like to see what happens when you do not use |
Thanks for the additional information. While I understand this behavior is not desired, Ionic is working correctly here. The I recommend reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this as it has a lot of helpful information regarding this behavior. In particular:
Setting |
Sorry if the issue is not formulated clear enough. We understand how
The one calling the callback is the ionic framework. Therefore it is the responsibility of ionic to call the callback with the appropriate One of the proposed solutions is that ionic binds the component to the callback. This would make the code above work automatically. in ionic, some code is calling the user-provided callback: ionic-framework/core/src/components/input/input.tsx Lines 617 to 624 in 516b844
instead, ionic should bind the appropriate getCounterText(value, maxlength, counterFormatter?.bind(componentModel)) Because the official ionic docs show us that the formatter should be located in the component model, it only makes sense to bind that component model as the callback's If this is not possible, the interface could accept an interface instead of a contextless function, which would automatically use the correct <ion-input label="Default counter" labelPlacement="floating" [counter]="true" maxlength="20"></ion-input>
<ion-input
id="custom-input"
label="Custom Counter Format"
labelPlacement="floating"
[counter]="true"
maxlength="20"
[counterFormatter]="customCounterFormatter.bind(this)"
></ion-input> Can you re-open the issue or should we create a new one? |
How the callback is called is determined by the implementer of the API, not the callback caller. I'll use the MDN example to clarify: function logThis() {
"use strict";
console.log(this);
}
[1, 2, 3].forEach(logThis); // undefined, undefined, undefined The API is In the context of Ionic, the API is In any event, I think it's worth updating the documentation to clarify this behavior. I can work on getting a PR up to note this. |
I just think it's stupid that Angular forces you to use classes, but at the same time doesn't bind instance methods automatically. In this angular code: <ion-input
[counterFormatter]="customCounterFormatter"
></ion-input>
@Component(...)
export class ExampleComponent {
customCounterFormatter(...) { ... }
} The custom counter formatter is looked up from the typescript component - but it's not automatically bound to the component. This seems stupid to me, because Angular does look into our ExampleComponent instance either way, but then doesn't go all the way. If Angular worked like this: I see it's the fault of Angular, and I understand if ionic doesn't want to fix their issues. |
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> In #28694 there was some confusion around how to access `this` inside of a callback function passed to a property on Ionic components. The root issue was due to how the `this` context is determined with developers being responsible for setting the appropriate `this` context. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - While this isn't an Ionic bug, I think it's worth calling out this behavior so developers are aware of how to account for it. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Note: The link in the docs will not work until ionic-team/ionic-docs#3333 is merged. --------- Co-authored-by: Amanda Johnston <[email protected]>
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. |
Prerequisites
Describe the Feature Request
We suggest that the
counterFormatter
attribute onion-input
can be passed an interface, in addition to a callback. This will avoid a potential pitfall, thus making ionic more beginner friendly.We added internationalization to our ion input counter formatter. But the current design is inconvenient, as it requires the use of
bind
or an unidiomatic arrow function in the class.Describe the Use Case
The example in the documentation is error-prone:
As soon as you add a class member, the code breaks, as the function looses the
this
context. It's not beginner friendly, and results in combersome code.For example, when adding internationalization:
This now results in an error:
Exception in provided 'counterFormatter'
, asthis
is not bound when ionic calls the formatter callback.Describe Preferred Solution
Introduce an
It should be possible to pass an object of type
CounterFormatter
to thecounterFormatter
attribute on theion-input
.We prefer this solution because it is the most explicit one.
Describe Alternatives
Either
callback.bind(component)
to the component before calling the callback inside theion-input
(100% backwards compatible, but not as explicit)bind
or an arrow functionRelated Code
No response
Additional Information
This change can also be applied to all callbacks, for example
PinFormatter
onion-range
, orion-textarea
.The text was updated successfully, but these errors were encountered: