I have an Angular component in a private npm package using ng-packagr and hosted on AWS (CodeArtifact).
The purpose of the component is to be a reactive form field that shows and hides errors based on user input, and has a couple of variations. I use PrimeNg for each variation to get a consistent look.
The general structure of the TS is:
@Component({
selector: 'lib-reactive-form-field',
...
})
export class ReactiveFormField {
public readonly control = input.required<FormControl>();
public readonly inputType = input<'phone' | 'text' | ...>('text');
// logic for controlling errors and other inputs
}
and the structure of the HTML is:
<div class="field-container">
<label [for]="fieldId()">
<!-- label for field -->
</label>
@switch (inputType()) {
@case ('phone') {
<app-phone-wrapper
[control]="control()"
/>
}
<!-- bunch of other types -->
@default {
<input
[formControl]="control()"
pInputText
/>
}
}
@if (errorMessage(); as error) {
<span class="error-message">{{ error }}</span>
}
</div>
The PhoneWrapper (app-phone-wrapper) uses the intl-tel-input package. A consumer of this package might not want to use the phone variation so I marked intl-tel-input as an optional peer dependency. The PhoneWrapper is not included in the npm packages public API; it is only useable through the ReactiveFormField.
I can install my private package just fine without intl-tel-input, but when running my app I see an error saying that intl-tel-input is missing.
I had a thought that I could fix this by using a @defer block inside the @case('phone') block, so the relevant HTML section looks like:
@switch (inputType()) {
@case ('phone') {
@defer (when true) {
<app-phone-wrapper
[control]="control()"
/>
}
}
<!-- bunch of other types -->
@default {
<input
[formControl]="control()"
pInputText
/>
}
}
However I am not sure this is a correct use case for @defer as the package will already have been built and presumably intl-tel-input may have been included as it is used in the PhoneWrapper.
Can anyone shed some light on this?