Skip to content

Commit 9cd092a

Browse files
committed
fix: Use ngOnChanges for options updates and simplify ngModel bindings
1 parent 28cd5cb commit 9cd092a

4 files changed

Lines changed: 16 additions & 14 deletions

File tree

demo/src/app/components/playground/preview-area/preview-area.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h3>Preview</h3>
4646

4747
<div class="selected-value">
4848
<strong>Selected Value:</strong>
49-
<pre>{{selectedValue() | json}}</pre>
49+
<pre>{{selectedValue | json}}</pre>
5050
</div>
5151
</div>
5252
</div>

demo/src/app/components/playground/preview-area/preview-area.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type PreviewTheme = 'light' | 'dark' | 'gray';
1515
export class PreviewAreaComponent {
1616
@Input() props: Record<string, any> = {};
1717

18-
selectedValue = signal<any>(null);
18+
selectedValue: any = null;
1919
previewTheme = signal<PreviewTheme>('light');
2020

2121
onSelectionChange(event: any): void {
22-
console.log('Selection changed:', this.selectedValue());
22+
console.log('Selection changed:', event);
2323
}
2424

2525
cycleTheme(): void {

demo/src/app/pages/home/home.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Feature {
1818
styleUrls: ['./home.component.scss']
1919
})
2020
export class HomeComponent {
21-
demoValue = signal<any>(null);
21+
demoValue: any = null;
2222

2323
demoOptions: SelectOption[] = [
2424
{ id: 'react', label: 'React', value: 'react' },
@@ -96,7 +96,7 @@ export class DemoComponent {
9696
];
9797
}`;
9898

99-
onDemoChange(value: any): void {
100-
this.demoValue.set(value);
99+
onDemoChange(event: any): void {
100+
console.log('Demo selection changed:', event);
101101
}
102102
}

src/lib/components/perfect-select/perfect-select.component.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
HostListener,
1010
OnInit,
1111
OnDestroy,
12+
OnChanges,
13+
SimpleChanges,
1214
ElementRef,
1315
ViewChild,
1416
effect,
@@ -44,7 +46,7 @@ import {
4446
multi: true
4547
}]
4648
})
47-
export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnDestroy {
49+
export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnChanges, OnDestroy {
4850
private destroyRef = inject(DestroyRef);
4951
private sanitizer = inject(DomSanitizer);
5052

@@ -280,13 +282,6 @@ export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnD
280282
private onTouched: any = () => {};
281283

282284
constructor() {
283-
// Watch for options changes
284-
effect(() => {
285-
if (this.options.length > 0 && !this.loadOptions) {
286-
this.internalOptions.set([...this.options]);
287-
}
288-
});
289-
290285
// Update closeMenuOnSelect based on isMulti
291286
effect(() => {
292287
if (this.isMulti && this.closeMenuOnSelect === true) {
@@ -295,6 +290,13 @@ export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnD
295290
});
296291
}
297292

293+
ngOnChanges(changes: SimpleChanges): void {
294+
// Update internal options when the options input changes
295+
if (changes['options'] && this.options.length > 0 && !this.loadOptions) {
296+
this.internalOptions.set([...this.options]);
297+
}
298+
}
299+
298300
writeValue(value: any): void {
299301
this.internalValue.set(value);
300302
}

0 commit comments

Comments
 (0)