Skip to content

Commit c2bf93f

Browse files
committed
Fix: Added support for new signal-based inputs
1 parent be94169 commit c2bf93f

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44

55
## [Unreleased]
66

7+
## [3.0.4] - 2024-09-26
8+
### Maintenance
9+
- Fix: Added support for new signal-based inputs
10+
711
## [3.0.3] - 2024-09-05
812
### Maintenance
913
- Fix: Change detection is now manually triggered when dynamic component inputs change in zoneless mode with OnPush strategy
@@ -138,7 +142,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
138142
### Added
139143
- This was the initial release, so everything was added here, really.
140144

141-
[Unreleased]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.3...HEAD
145+
[Unreleased]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.4...HEAD
146+
[3.0.4]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.3...v3.0.4
142147
[3.0.3]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.2...v3.0.3
143148
[3.0.2]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.1...v3.0.2
144149
[3.0.1]: https://github.com/Angular-Dynamic-Hooks/ngx-dynamic-hooks/compare/v3.0.0...v3.0.1

projects/ngx-dynamic-hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-dynamic-hooks",
3-
"version": "3.0.3",
3+
"version": "3.0.4",
44
"description": "Automatically insert live Angular components into a dynamic string of content (based on their selector or any pattern of your choice) and render the result in the DOM.",
55
"person": "Marvin Tobisch <[email protected]>",
66
"license": "MIT",

projects/ngx-dynamic-hooks/src/lib/services/core/componentUpdater.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ export class ComponentUpdater {
158158

159159
// Set inputs in component
160160
for (const {propName, templateName, value} of existingInputs) {
161-
if (templateName) hook.componentRef?.setInput(templateName, value); // Sets property, triggers OnChanges and marks for OnPush
162-
if (propName) hook.componentRef!.instance[propName] = value; // Manual setting of input for acceptInputsForAnyProperty
161+
if (templateName) {
162+
hook.componentRef?.setInput(templateName, value); // Official method to set inputs. Sets property, triggers OnChanges and marks for OnPush. Also works with new signal inputs.
163+
} else if (propName) {
164+
hook.componentRef!.instance[propName] = value; // Resort to manual setting only if prop isn't a declared input (may happen with "acceptInputsForAnyProperty")
165+
}
163166
}
164167

165168
// Important: Still need to trigger cd, even with componentRef.setInput

projects/ngx-dynamic-hooks/src/tests/integration/componentBindings.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defaultBeforeEach } from './shared';
33
import { TestBed, TestBedStatic } from '@angular/core/testing';
44
import { DynamicHooksComponent, DynamicHooksService } from '../testing-api';
55
import { GenericSingleTagStringParser } from '../resources/parsers/genericSingleTagStringParser';
6+
import { SingleTagTestComponent } from '../resources/components/singleTag/singleTagTest.c';
67

78
describe('Component bindings', () => {
89
let testBed: TestBedStatic;
@@ -42,6 +43,28 @@ describe('Component bindings', () => {
4243
expect(loadedComp.simpleObject).toBe(someObj);
4344
});
4445

46+
it('#should also work with the newer signal inputs (ng17+)', () => {
47+
const someObj = {randomProperty: "Hopefully, it also works with signal inputs!"};
48+
49+
const genericSingleTagParser = TestBed.inject(GenericSingleTagStringParser);
50+
genericSingleTagParser.onGetBindings = (hookId, hookValue, context) => {
51+
return {
52+
inputs: {
53+
signalInput: someObj
54+
}
55+
}
56+
}
57+
58+
const testText = `Just some component: [singletag-string]">`;
59+
comp.content = testText;
60+
comp.context = context;
61+
comp.ngOnChanges({content: true, context: true, options: true} as any);
62+
63+
const loadedComp = comp.hookIndex[1].componentRef!.instance as SingleTagTestComponent;
64+
expect(loadedComp.signalInput).toBeInstanceOf(Function);
65+
expect(loadedComp.signalInput()).toBe(someObj);
66+
});
67+
4568
it('#should subscribe to outputs of dynamic components', () => {
4669
let testVar: any = null;
4770

projects/ngx-dynamic-hooks/src/tests/resources/components/abstractTest.c.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, AfterViewInit, OnDestroy, Input, OnChanges, ChangeDetectorRef, Output, EventEmitter, Inject, DoCheck, Optional, InjectionToken, EnvironmentInjector, Injector, NgZone } from '@angular/core';
1+
import { Component, OnInit, AfterViewInit, OnDestroy, Input, OnChanges, ChangeDetectorRef, Output, EventEmitter, Inject, DoCheck, Optional, InjectionToken, EnvironmentInjector, Injector, NgZone, input } from '@angular/core';
22
import { DynamicContentChild, OnDynamicData, OnDynamicChanges, OnDynamicMount } from '../../testing-api';
33
import { RootTestService } from '../services/rootTestService';
44
import { GENERICINJECTIONTOKEN } from '../services/genericInjectionToken';
@@ -34,6 +34,7 @@ export class AbstractTestComponent implements OnDynamicMount, OnDynamicChanges,
3434
@Input() nestedFunctions: any;
3535
@Input() nestedFunctionsInBrackets!: Array<any>;
3636
@Input() everythingTogether!: Array<any>;
37+
signalInput = input();
3738
nonOutputEventEmitter: EventEmitter<number> = new EventEmitter();
3839
@Output() genericOutput: EventEmitter<number> = new EventEmitter();
3940
@Output() genericOtherOutput: EventEmitter<number> = new EventEmitter();

0 commit comments

Comments
 (0)