Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
We suggest that the counterFormatter
attribute on ion-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:
<ion-input
[counter]="true"
maxlength="20"
[counterFormatter]="customCounterFormatter"
></ion-input>
@Component(...)
export class ExampleComponent {
customCounterFormatter(inputLength: number, maxLength: number) {
return `${maxLength - inputLength} characters remaining`;
}
}
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:
export class ExampleComponent {
translated: string = "characters remaining" // probably injected dynamically
customCounterFormatter(inputLength: number, maxLength: number) {
return `${maxLength - inputLength} {this.translated}`;
}
}
This now results in an error: Exception in provided 'counterFormatter'
, as this
is not bound when ionic calls the formatter callback.
Describe Preferred Solution
Introduce an
interface CounterFormatter {
format(current: number, limit: number): string
}
It should be possible to pass an object of type CounterFormatter
to the counterFormatter
attribute on the ion-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)- change the example to include a call
bind
or an arrow function
Related Code
No response
Additional Information
This change can also be applied to all callbacks, for example PinFormatter
on ion-range
, or ion-textarea
.