Skip to content

Commit 30d6e23

Browse files
committed
fix: Remove improper effect() usage to fix runtime error
Removed effect() calls from constructors that were tracking @input properties instead of signals, which caused 'Cannot read properties of null' errors. Moved logic to ngOnChanges() and ngOnInit() lifecycle hooks.
1 parent a099a9d commit 30d6e23

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, signal, computed, OnInit, effect } from '@angular/core';
1+
import { Component, Input, signal, computed, OnInit } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { ComponentMetadata, CodeExample } from '../../../models/playground.types';
44
import { SELECT_EXAMPLES } from '../../../data/select-examples';
@@ -38,12 +38,7 @@ export class ComponentPlaygroundComponent implements OnInit {
3838
});
3939

4040
constructor(private codeGenerator: CodeGeneratorService) {
41-
// Initialize props when metadata is available
42-
effect(() => {
43-
if (this.metadata && Object.keys(this.currentProps()).length === 0) {
44-
this.currentProps.set({ ...this.metadata.defaultProps });
45-
}
46-
});
41+
// Initialization happens in ngOnInit
4742
}
4843

4944
ngOnInit(): void {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
SimpleChanges,
1414
ElementRef,
1515
ViewChild,
16-
effect,
1716
inject,
1817
DestroyRef
1918
} from '@angular/core';
@@ -282,19 +281,19 @@ export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnC
282281
private onTouched: any = () => {};
283282

284283
constructor() {
285-
// Update closeMenuOnSelect based on isMulti
286-
effect(() => {
287-
if (this.isMulti && this.closeMenuOnSelect === true) {
288-
this.closeMenuOnSelect = false;
289-
}
290-
});
284+
// Constructor intentionally empty - initialization happens in ngOnInit
291285
}
292286

293287
ngOnChanges(changes: SimpleChanges): void {
294288
// Update internal options when the options input changes
295289
if (changes['options'] && this.options.length > 0 && !this.loadOptions) {
296290
this.internalOptions.set([...this.options]);
297291
}
292+
293+
// Update closeMenuOnSelect based on isMulti
294+
if (changes['isMulti'] && this.isMulti && this.closeMenuOnSelect === true) {
295+
this.closeMenuOnSelect = false;
296+
}
298297
}
299298

300299
writeValue(value: any): void {
@@ -319,6 +318,11 @@ export class PerfectSelectComponent implements ControlValueAccessor, OnInit, OnC
319318
this.internalOptions.set([...this.options]);
320319
}
321320

321+
// Update closeMenuOnSelect based on isMulti (initial setup)
322+
if (this.isMulti && this.closeMenuOnSelect === true) {
323+
this.closeMenuOnSelect = false;
324+
}
325+
322326
// Load default options if async
323327
if (this.loadOptions && this.defaultOptions) {
324328
this.handleLoadOptions('');

0 commit comments

Comments
 (0)