Skip to content

Commit 308f396

Browse files
refactor(angular): apply range to numeric value accessor (#29029)
Issue number: Internal --------- <!-- 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. --> The ticket describes renaming `TextValueAccessorDirective` since `ion-range` is not a text-based control, however I think this was an incorrect assumption made during the original implementation. `ion-range` is a numeric based value accessor (either as a single number or an object accepting a numeric start/end value). ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Migrates the usage of `ion-range` value accessor implementation to the `NumericValueAccessorDirective` - Adds tests for validating the value accessor is functioning as expected ## 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. --> --------- Co-authored-by: Liam DeBeasi <[email protected]>
1 parent 58c795f commit 308f396

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

packages/angular/src/directives/control-value-accessors/numeric-value-accessor.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { ValueAccessor } from '@ionic/angular/common';
44

55
@Directive({
6-
selector: 'ion-input[type=number]',
6+
selector: 'ion-input[type=number],ion-range',
77
providers: [
88
{
99
provide: NG_VALUE_ACCESSOR,
@@ -13,18 +13,22 @@ import { ValueAccessor } from '@ionic/angular/common';
1313
],
1414
})
1515
export class NumericValueAccessorDirective extends ValueAccessor {
16-
constructor(injector: Injector, el: ElementRef) {
16+
constructor(injector: Injector, private el: ElementRef<HTMLInputElement | HTMLIonRangeElement>) {
1717
super(injector, el);
1818
}
1919

2020
@HostListener('ionInput', ['$event.target'])
21-
handleInputEvent(el: HTMLIonInputElement): void {
21+
handleInputEvent(el: HTMLIonInputElement | HTMLIonRangeElement): void {
2222
this.handleValueChange(el, el.value);
2323
}
2424

2525
registerOnChange(fn: (_: number | null) => void): void {
26-
super.registerOnChange((value: string) => {
27-
fn(value === '' ? null : parseFloat(value));
28-
});
26+
if (this.el.nativeElement.tagName === 'ION-INPUT') {
27+
super.registerOnChange((value: string) => {
28+
fn(value === '' ? null : parseFloat(value));
29+
});
30+
} else {
31+
super.registerOnChange(fn);
32+
}
2933
}
3034
}

packages/angular/src/directives/control-value-accessors/text-value-accessor.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { ElementRef, Injector, Directive, HostListener } from '@angular/core';
22
import { NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { ValueAccessor } from '@ionic/angular/common';
44

5-
// TODO(FW-5495): rename class since range isn't a text component
65
@Directive({
7-
selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar,ion-range',
6+
selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar',
87
providers: [
98
{
109
provide: NG_VALUE_ACCESSOR,
@@ -19,9 +18,7 @@ export class TextValueAccessorDirective extends ValueAccessor {
1918
}
2019

2120
@HostListener('ionInput', ['$event.target'])
22-
_handleInputEvent(
23-
el: HTMLIonInputElement | HTMLIonTextareaElement | HTMLIonSearchbarElement | HTMLIonRangeElement
24-
): void {
21+
_handleInputEvent(el: HTMLIonInputElement | HTMLIonTextareaElement | HTMLIonSearchbarElement): void {
2522
this.handleValueChange(el, el.value);
2623
}
2724
}

packages/angular/test/base/e2e/src/lazy/inputs.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('Inputs', () => {
1010
cy.get('ion-input').should('have.prop', 'value').and('equal', 'some text');
1111
cy.get('ion-datetime').should('have.prop', 'value').and('equal', '1994-03-15');
1212
cy.get('ion-select').should('have.prop', 'value').and('equal', 'nes');
13+
cy.get('ion-range').should('have.prop', 'value').and('equal', 50);
1314
});
1415

1516
it('should have reset value', () => {
@@ -27,6 +28,7 @@ describe('Inputs', () => {
2728
cy.get('ion-input').should('not.have.prop', 'value');
2829
cy.get('ion-datetime').should('not.have.prop', 'value');
2930
cy.get('ion-select').should('not.have.prop', 'value');
31+
cy.get('ion-range').should('not.have.prop', 'value');
3032
});
3133

3234
it('should get some value', () => {
@@ -39,6 +41,7 @@ describe('Inputs', () => {
3941
cy.get('ion-input').should('have.prop', 'value').and('equal', 'some text');
4042
cy.get('ion-datetime').should('have.prop', 'value').and('equal', '1994-03-15');
4143
cy.get('ion-select').should('have.prop', 'value').and('equal', 'nes');
44+
cy.get('ion-range').should('have.prop', 'value').and('equal', 50);
4245
});
4346

4447
it('change values should update angular', () => {

packages/angular/test/base/src/app/lazy/inputs/inputs.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
<ion-note slot="end">{{radio}}</ion-note>
100100
</ion-item>
101101

102+
<ion-item>
103+
<ion-range [(ngModel)]="range" min="0" max="100" id="first-range">
104+
<ion-label slot="start">Range</ion-label>
105+
</ion-range>
106+
</ion-item>
107+
102108
</ion-list>
103109
<p>
104110
<ion-button (click)="setValues()" id="set-button">Set values</ion-button>

packages/angular/test/base/src/app/lazy/inputs/inputs.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class InputsComponent {
1313
toggle = true;
1414
select? = 'nes';
1515
changes = 0;
16+
range? = 50;
1617

1718
setValues() {
1819
console.log('set values');
@@ -22,6 +23,7 @@ export class InputsComponent {
2223
this.radio = 'nes';
2324
this.toggle = true;
2425
this.select = 'nes';
26+
this.range = 50;
2527
}
2628

2729
resetValues() {
@@ -32,6 +34,7 @@ export class InputsComponent {
3234
this.radio = undefined;
3335
this.toggle = false;
3436
this.select = undefined;
37+
this.range = undefined;
3538
}
3639

3740
counter() {

0 commit comments

Comments
 (0)