ionChange will only emit from user committed changes #25532
Replies: 6 comments 1 reply
-
Thanks so much @sean-perkins for this awesome RFC! |
Beta Was this translation helpful? Give feedback.
-
You are awesome and an inspiration of mine sir |
Beta Was this translation helpful? Give feedback.
-
This sounds awesome thanks 😊 |
Beta Was this translation helpful? Give feedback.
-
This is a great suggestion. It is very weird that a basic use case like loading data for |
Beta Was this translation helpful? Give feedback.
-
what if user autofill their FORMS from password managers or anything like that? there should be an option to enable / disable for ionChange to trigger! |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, thank you so much for expressing your feedback surrounding this RFC. We will be moving forward with this change to form controls in an upcoming major release of Ionic Framework. |
Beta Was this translation helpful? Give feedback.
-
Summary
Depending on the kind of element being changed and the way the user interacts with the element, the
ionChange
event should fire at a different moment:ion-checkbox
orion-radio
element is checked or unchecked (by clicking or using the keyboard);ion-select
dropdown with a mouse click, by selecting a date from theion-datetime
, etc.);ion-textarea
orion-input
).The
ionChange
event should not fire as a result of external value changes (e.g.: manually assigning a value).Motivation
Ionic Framework has had this API design for
ionChange
for quite some time. As a result, there have been a number of issues over the years and frustrations from developers; such as:ionChange
is emitted multiple times from a single (external) change eventionChange
was a result of the user interacting with the control or their app setting the value on the control.ionChange
is emitted at un-expected times, such as resetting the state of a form.This pattern is also inconsistent with HTML form elements, such as
input
,textarea
andselect
.Related Issues:
ionChange
event: determine whether it by user interaction #19651Proposed Design
There will be no change to the public API for
ionChange
, while the behavior for whenionChange
is fired will change.The
ionChange
event for each component will align to their equivalent HTML element, if available. Custom components without an equivalent, will fireionChange
when the user commits the change explicitly. This will align the behavior ofionChange
with thechange
event: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_eventInternally, the Ionic team will move away from this pattern:
This approach of listening for any internal/external change to
value
, is the root cause of this architectural issue.The following components will be affected:
ion-accordion-group
ion-checkbox
(with thechecked
property)ion-datetime
ion-input
ion-radio-group
ion-range
ion-searchbar
ion-segment
ion-select
ion-textarea
ion-toggle
Usage
With the following example:
ionChange
is fired when the user types within theion-input
and the control is blurred.ionChange
will not fire whenmyValue
is set to “Hello world”Benefits
change
event pattern that developers expect from existing form elements (e.g.input
,textarea
).ionChange
will only be emitted as a result of committed value changes to the control. This provides developers a consistent API to respond to user interaction vs. setting the state of a form control.change
andinput
, allowing Ionic to explore features such as input masking.Drawbacks
Since this pattern has existed in Ionic Framework for awhile, it is expected that developers have either written applications that have dependency on this pattern or have written custom code within their
ionChange
event handlers, that uses comparisons to try and determine if the change is a result of user interaction. As a result, developers would need to audit their implementations after this change, to make sure that their applications continue to behave consistently or refactor them accordingly.Alternatives
Comparing Previous Value
Developers have been able to work around this pattern by comparing the previous value against the new value before handling the interaction.
The downsides of this implementation pattern is that it requires a lot of boilerplate in each
ionChange
event handler. It also is difficult to discern between if the change event is from the initial value being set on the control or from the user changing the value for the first time.Skip First Emission
Developers may also have been able to work around this issue by ignoring the first emission of
ionChange
, as it is likely a result of the value being set.This approach has many downsides. If the user was to interact with the control before the API response has been resolved, the interaction would be skipped. It also assumes that only a single interaction will manually set
this.myValue
.Using
ionInput
eventDevelopers can use the
ionInput
on the components that support the event API, to only subscribe to changes when the user types.While this API suffices for
ion-input
, it is not available for all Ionic form controls, such asion-datetime
,ion-range
andion-select
.Breaking Changes
Listening for changes while focused (input, textarea)
Developers using the
ionChange
event to listen for immediate value changes as the user types forion-input
orion-textarea
, should migrate their implementations to use theionInput
event.Before
After
Developers can continue to use
ionChange
to listen for when the value is committed to the input. This happens when the control loses focus.debounce
propertyThe
debounce
property onion-input
,ion-textarea
andion-searchbar
controls the time in milliseconds to wait to triggerionChange
after each keystroke. This behavior is invalid now thationChange
emits on blur.This behavior will be migrated to apply to the
ionInput
event instead. Developers usingdebounce
onion-input
,ion-textarea
orion-searchbar
will delay the emission of theionInput
event after each keystroke.Beta Was this translation helpful? Give feedback.
All reactions