Skip to content

Commit

Permalink
fix(material): add support of text prefix / suffix
Browse files Browse the repository at this point in the history
fix #3556
  • Loading branch information
aitboudad committed Jan 20, 2023
1 parent 611a02f commit 2779cd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export class FormlyWrapperAddons extends FieldWrapper implements AfterViewInit {

ngAfterViewInit() {
if (this.matPrefix) {
// Note: for text use `textPrefix` property (only in Angular Material >= 15)
this.props.prefix = this.matPrefix;
}

if (this.matSuffix) {
// Note: for text use `textSuffix` property (only in Angular Material >= 15)
this.props.suffix = this.matSuffix;
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/ui/material/form-field/src/field.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ export abstract class FieldType<F extends FormlyFieldConfig<FormlyFieldProps>>
this.props.prefix = prefix;
}
}
@ViewChild('matTextPrefix') set matTextPrefix(textPrefix: TemplateRef<any>) {
if (textPrefix) {
this.props.textPrefix = textPrefix;
}
}
@ViewChild('matSuffix') set matSuffix(suffix: TemplateRef<any>) {
if (suffix) {
this.props.suffix = suffix;
}
}
@ViewChild('matTextSuffix') set matTextSuffix(textSuffix: TemplateRef<any>) {
if (textSuffix) {
this.props.textSuffix = textSuffix;
}
}

@ViewChildren(MatFormFieldControl) set _controls(controls: QueryList<MatFormFieldControl<any>>) {
this.attachControl(controls.length === 1 ? controls.first : this);
Expand Down Expand Up @@ -105,7 +115,7 @@ export abstract class FieldType<F extends FormlyFieldConfig<FormlyFieldProps>>
ngControl.valueAccessor['_parentFormField'] = this.formField;
}

['prefix', 'suffix'].forEach((type) =>
['prefix', 'suffix', 'textPrefix', 'textSuffix'].forEach((type) =>
observe<TemplateRef<any>>(
this.field,
['props', type],
Expand Down
10 changes: 10 additions & 0 deletions src/ui/material/form-field/src/form-field.wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface MatFormlyFieldConfig extends FormlyFieldConfig<FormlyFieldProps> {
export interface FormlyFieldProps extends CoreFormlyFieldProps {
prefix?: TemplateRef<any>;
suffix?: TemplateRef<any>;
textPrefix?: TemplateRef<any>;
textSuffix?: TemplateRef<any>;
hideLabel?: boolean;
hideRequiredMarker?: boolean;
hideFieldUnderline?: boolean;
Expand Down Expand Up @@ -60,10 +62,18 @@ export interface FormlyFieldProps extends CoreFormlyFieldProps {
>
</mat-label>
<ng-containe matTextPrefix *ngIf="props.textPrefix">
<ng-container [ngTemplateOutlet]="props.textPrefix" [ngTemplateOutletContext]="{ field: field }"></ng-container>
</ng-containe>
<ng-containe matPrefix *ngIf="props.prefix">
<ng-container [ngTemplateOutlet]="props.prefix" [ngTemplateOutletContext]="{ field: field }"></ng-container>
</ng-containe>
<ng-containe matTextSuffix *ngIf="props.textSuffix">
<ng-container [ngTemplateOutlet]="props.textSuffix" [ngTemplateOutletContext]="{ field: field }"></ng-container>
</ng-containe>
<ng-containe matSuffix *ngIf="props.suffix">
<ng-container [ngTemplateOutlet]="props.suffix" [ngTemplateOutletContext]="{ field: field }"></ng-container>
</ng-containe>
Expand Down

0 comments on commit 2779cd2

Please sign in to comment.