Skip to content

Commit aa18392

Browse files
refactor: update onSearchChanged method to accept only string values across components
1 parent 7f29fbe commit aa18392

File tree

8 files changed

+54
-176
lines changed

8 files changed

+54
-176
lines changed

projects/composition/src/app/components/navigation-sidebar/navigation-sidebar.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ export class NavigationSidebarComponent implements OnInit {
168168
this.filteredComponents = [...this._components];
169169
}
170170

171-
onSearchChanged(value: string | number) {
172-
this._filterComponentsList(String(value));
171+
onSearchChanged(value: string) {
172+
this._filterComponentsList(value);
173173
}
174174

175175
private _filterComponentsList(searchStr: string) {

projects/composition/src/app/pages/colors-page/colors-page/colors-page.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ export class ColorsPageComponent implements OnInit {
151151
this.filteredColorsList = [...this.colorsList];
152152
}
153153

154-
onSearchChanged(value: string | number) {
155-
this._filterColors(String(value));
154+
onSearchChanged(value: string) {
155+
this._filterColors(value);
156156
}
157157

158158
private _filterColors(searchStr: string) {

projects/composition/src/app/pages/icons-page/icons-page/icons-page.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class IconsPageComponent implements OnInit {
3636
this.filteredIconsList = iconNames;
3737
}
3838

39-
onSearchChanged(value: string | number) {
40-
this._filterIcons(String(value));
39+
onSearchChanged(value: string) {
40+
this._filterIcons(value);
4141
}
4242

4343
private _filterIcons(name: string) {

projects/cps-ui-kit/src/lib/components/cps-datepicker/cps-datepicker.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {
1111
ViewChild
1212
} from '@angular/core';
1313
import { ControlValueAccessor, FormsModule, NgControl } from '@angular/forms';
14-
import { DatePickerModule } from 'primeng/datepicker';
14+
import { CpsInputComponent } from '../cps-input/cps-input.component';
1515
import { Subscription } from 'rxjs';
16-
import { CpsTooltipPosition } from '../../directives/cps-tooltip/cps-tooltip.directive';
1716
import { convertSize } from '../../utils/internal/size-utils';
18-
import { CpsInputComponent } from '../cps-input/cps-input.component';
17+
import { CpsTooltipPosition } from '../../directives/cps-tooltip/cps-tooltip.directive';
1918
import { CpsMenuComponent } from '../cps-menu/cps-menu.component';
19+
import { DatePickerModule } from 'primeng/datepicker';
2020

2121
/**
2222
* CpsDatepickerAppearanceType is used to define the border of the datepicker input.
@@ -226,13 +226,13 @@ export class CpsDatepickerComponent
226226
this.onTouched = fn;
227227
}
228228

229-
onInputValueChanged(val: string | number) {
230-
this.stringDate = String(val);
229+
onInputValueChanged(val: string) {
230+
this.stringDate = val;
231231
if (!val) {
232232
this._updateValue(null);
233233
return;
234234
}
235-
const dt = this._stringToDate(String(val));
235+
const dt = this._stringToDate(val);
236236
if (dt) this._updateValue(dt);
237237
}
238238

projects/cps-ui-kit/src/lib/components/cps-input/cps-input.component.spec.ts

Lines changed: 12 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ElementRef } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
3+
import { ReactiveFormsModule } from '@angular/forms';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
55
import { CpsInputComponent } from './cps-input.component';
66

@@ -22,118 +22,7 @@ describe('CpsInputComponent', () => {
2222
expect(component).toBeTruthy();
2323
});
2424

25-
describe('Zero Value Bug Tests', () => {
26-
it('should display numeric zero value', () => {
27-
component.writeValue(0);
28-
fixture.detectChanges();
29-
30-
expect(component.value).toBe(0);
31-
const inputElement = fixture.nativeElement.querySelector('input');
32-
expect(inputElement.value).toBe('0');
33-
});
34-
35-
it('should display string zero value', () => {
36-
component.writeValue('0');
37-
fixture.detectChanges();
38-
39-
expect(component.value).toBe('0');
40-
const inputElement = fixture.nativeElement.querySelector('input');
41-
expect(inputElement.value).toBe('0');
42-
});
43-
44-
it('should handle zero in form control', () => {
45-
const formControl = new FormControl<string | number>(0);
46-
component.registerOnChange((value) => formControl.setValue(value));
47-
48-
component.writeValue(0);
49-
fixture.detectChanges();
50-
51-
expect(component.value).toBe(0);
52-
});
53-
54-
it('should emit zero value on change', () => {
55-
const emitSpy = jest.spyOn(component.valueChanged, 'emit');
56-
57-
const inputElement = fixture.nativeElement.querySelector('input');
58-
inputElement.value = '0';
59-
inputElement.dispatchEvent(new Event('input'));
60-
fixture.detectChanges();
61-
62-
expect(emitSpy).toHaveBeenCalledWith('0');
63-
});
64-
65-
it('should handle zero with required validator', () => {
66-
const formControl = new FormControl(0, Validators.required);
67-
component.writeValue(0);
68-
fixture.detectChanges();
69-
70-
expect(formControl.valid).toBe(true);
71-
expect(component.value).toBe(0);
72-
});
73-
74-
it('should distinguish between zero and empty string', () => {
75-
// Test zero
76-
component.writeValue(0);
77-
fixture.detectChanges();
78-
expect(component.value).toBe(0);
79-
80-
// Test empty string
81-
component.writeValue('');
82-
fixture.detectChanges();
83-
expect(component.value).toBe('');
84-
85-
// They should be different
86-
component.writeValue(0);
87-
fixture.detectChanges();
88-
const zeroValue = component.value;
89-
90-
component.writeValue('');
91-
fixture.detectChanges();
92-
const emptyValue = component.value;
93-
94-
expect(zeroValue).not.toBe(emptyValue);
95-
});
96-
97-
it('should handle zero for numeric input type', () => {
98-
component.type = 'number';
99-
component.ngOnInit();
100-
component.writeValue(0);
101-
fixture.detectChanges();
102-
103-
expect(component.value).toBe(0);
104-
const inputElement = fixture.nativeElement.querySelector('input');
105-
106-
expect(inputElement.type).toBe('number');
107-
expect(inputElement.value).toBe('0');
108-
});
109-
110-
it('should preserve zero when patching form value', () => {
111-
const onChange = jest.fn();
112-
component.registerOnChange(onChange);
113-
114-
component.writeValue(0);
115-
fixture.detectChanges();
116-
117-
expect(component.value).toBe(0);
118-
expect(onChange).toHaveBeenCalledWith(expect.anything());
119-
});
120-
});
121-
12225
describe('Value Handling', () => {
123-
it('should handle null value', () => {
124-
component.writeValue(null);
125-
fixture.detectChanges();
126-
127-
expect(component.value).toBe('');
128-
});
129-
130-
it('should handle undefined value', () => {
131-
component.writeValue(undefined);
132-
fixture.detectChanges();
133-
134-
expect(component.value).toBe('');
135-
});
136-
13726
it('should handle empty string', () => {
13827
component.writeValue('');
13928
fixture.detectChanges();
@@ -149,24 +38,24 @@ describe('CpsInputComponent', () => {
14938
});
15039

15140
it('should handle numeric values', () => {
152-
component.writeValue(123);
41+
component.writeValue('123');
15342
fixture.detectChanges();
15443

155-
expect(component.value).toBe(123);
44+
expect(component.value).toBe('123');
15645
});
15746

15847
it('should handle negative numbers', () => {
159-
component.writeValue(-5);
48+
component.writeValue('-5');
16049
fixture.detectChanges();
16150

162-
expect(component.value).toBe(-5);
51+
expect(component.value).toBe('-5');
16352
});
16453

16554
it('should handle decimal numbers', () => {
166-
component.writeValue(3.14);
55+
component.writeValue('3.14');
16756
fixture.detectChanges();
16857

169-
expect(component.value).toBe(3.14);
58+
expect(component.value).toBe('3.14');
17059
});
17160
});
17261

@@ -563,7 +452,7 @@ describe('CpsInputComponent', () => {
563452

564453
describe('Edge Cases', () => {
565454
it('should handle rapid value changes', () => {
566-
const values = [0, 1, 2, 3, 4, 5];
455+
const values = ['0', '1', '2', '3', '4', '5'];
567456

568457
values.forEach((val) => {
569458
component.writeValue(val);
@@ -573,7 +462,7 @@ describe('CpsInputComponent', () => {
573462
});
574463

575464
it('should handle very large numbers', () => {
576-
const largeNumber = 999999999999;
465+
const largeNumber = '999999999999';
577466
component.writeValue(largeNumber);
578467
fixture.detectChanges();
579468

@@ -608,11 +497,11 @@ describe('CpsInputComponent', () => {
608497
describe('Type Consistency', () => {
609498
it('should maintain number type for numeric input', () => {
610499
component.type = 'number';
611-
component.writeValue(42);
500+
component.writeValue('42');
612501
fixture.detectChanges();
613502

614-
expect(typeof component.value).toBe('number');
615-
expect(component.value).toBe(42);
503+
expect(typeof component.value).toBe('string');
504+
expect(component.value).toBe('42');
616505
});
617506

618507
it('should maintain string type for text input', () => {
@@ -623,15 +512,5 @@ describe('CpsInputComponent', () => {
623512
expect(typeof component.value).toBe('string');
624513
expect(component.value).toBe('text');
625514
});
626-
627-
it('should handle type conversion properly', () => {
628-
// Write as number
629-
component.writeValue(123);
630-
expect(component.value).toBe(123);
631-
632-
// Write as string
633-
component.writeValue('456');
634-
expect(component.value).toBe('456');
635-
});
636515
});
637516
});

projects/cps-ui-kit/src/lib/components/cps-input/cps-input.component.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import {
1414
ViewChild
1515
} from '@angular/core';
1616
import { ControlValueAccessor, NgControl } from '@angular/forms';
17-
import { Subscription } from 'rxjs';
18-
import { CpsTooltipPosition } from '../../directives/cps-tooltip/cps-tooltip.directive';
19-
import { convertSize } from '../../utils/internal/size-utils';
2017
import {
2118
CpsIconComponent,
2219
IconType,
2320
iconSizeType
2421
} from '../cps-icon/cps-icon.component';
25-
import { CpsInfoCircleComponent } from '../cps-info-circle/cps-info-circle.component';
22+
import { Subscription } from 'rxjs';
23+
import { convertSize } from '../../utils/internal/size-utils';
2624
import { CpsProgressLinearComponent } from '../cps-progress-linear/cps-progress-linear.component';
25+
import { CpsInfoCircleComponent } from '../cps-info-circle/cps-info-circle.component';
26+
import { CpsTooltipPosition } from '../../directives/cps-tooltip/cps-tooltip.directive';
2727

2828
/**
2929
* CpsInputAppearanceType is used to define the border of the input field.
@@ -192,22 +192,22 @@ export class CpsInputComponent
192192
* @default ''
193193
* @group Props
194194
*/
195-
@Input() set value(value: string | number) {
196-
if (value === null || value === undefined) value = '';
195+
@Input() set value(value: string) {
196+
if (!value) value = '';
197197
this._value = value;
198198
this.onChange(value);
199199
}
200200

201-
get value(): string | number {
201+
get value(): string {
202202
return this._value;
203203
}
204204

205205
/**
206206
* Callback to invoke on value change.
207-
* @param {string | number} value - value changed.
207+
* @param {string} string - value changed.
208208
* @group Emits
209209
*/
210-
@Output() valueChanged = new EventEmitter<string | number>();
210+
@Output() valueChanged = new EventEmitter<string>();
211211

212212
/**
213213
* Callback to invoke when the component receives focus.
@@ -251,7 +251,7 @@ export class CpsInputComponent
251251
cvtWidth = '';
252252

253253
private _statusChangesSubscription?: Subscription;
254-
private _value: string | number = '';
254+
private _value = '';
255255

256256
constructor(
257257
@Self() @Optional() private _control: NgControl,
@@ -336,9 +336,8 @@ export class CpsInputComponent
336336
this.error = message || 'Unknown error';
337337
}
338338

339-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
340-
onChange = (value: string | number) => {};
341-
339+
// eslint-disable-next-line @typescript-eslint/no-empty-function
340+
onChange = (event: any) => {};
342341
// eslint-disable-next-line @typescript-eslint/no-empty-function
343342
onTouched = () => {};
344343

@@ -347,24 +346,24 @@ export class CpsInputComponent
347346
this.enterClicked.emit();
348347
}
349348

350-
registerOnChange(fn: (value: string | number) => void) {
349+
registerOnChange(fn: any) {
351350
this.onChange = fn;
352351
}
353352

354353
registerOnTouched(fn: any) {
355354
this.onTouched = fn;
356355
}
357356

358-
writeValue(value: string | number | null | undefined) {
359-
this.value = value ?? '';
357+
writeValue(value: string) {
358+
this.value = value;
360359
}
361360

362361
updateValueEvent(event: any) {
363-
const value = event?.target?.value ?? '';
362+
const value = event?.target?.value || '';
364363
this._updateValue(value);
365364
}
366365

367-
private _updateValue(value: string | number) {
366+
private _updateValue(value: string) {
368367
this.writeValue(value);
369368
this.onChange(value);
370369
this.valueChanged.emit(value);

0 commit comments

Comments
 (0)