-
Notifications
You must be signed in to change notification settings - Fork 422
Angular Material: OneOf Enum Control Renderer #2551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
815ba6e
40535b6
8a19821
bf1d6ed
bed6dd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <mat-form-field [ngStyle]="{ display: hidden ? 'none' : '' }"> | ||
| <mat-label>{{ label }}</mat-label> | ||
| <input | ||
| matInput | ||
| type="text" | ||
| (change)="onChange($event)" | ||
| [id]="id" | ||
| [formControl]="form" | ||
| [matAutocomplete]="auto" | ||
| (keydown)="updateFilter($event)" | ||
| (focus)="focused = true" | ||
| (focusout)="focused = false" | ||
| /> | ||
| <mat-autocomplete | ||
| autoActiveFirstOption | ||
| #auto="matAutocomplete" | ||
| (optionSelected)="onSelect($event)" | ||
| [displayWith]="displayFn" | ||
| > | ||
| @for (option of filteredOptions | async; track option.value) { | ||
| <mat-option [value]="option"> | ||
| {{ option.label }} | ||
| </mat-option> | ||
| } | ||
| </mat-autocomplete> | ||
| <mat-hint *ngIf="shouldShowUnfocusedDescription() || focused">{{ | ||
| description | ||
| }}</mat-hint> | ||
| <mat-error>{{ error }}</mat-error> | ||
| </mat-form-field> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| :host { | ||
| display: flex; | ||
| flex-direction: row; | ||
| } | ||
| mat-form-field { | ||
| flex: 1 1 auto; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,8 +36,10 @@ import { | |
| ControlElement, | ||
| EnumOption, | ||
| isEnumControl, | ||
| isOneOfEnumControl, | ||
| JsonFormsState, | ||
| mapStateToEnumControlProps, | ||
| mapStateToOneOfEnumControlProps, | ||
| OwnPropsOfControl, | ||
| OwnPropsOfEnum, | ||
| RankedTester, | ||
|
|
@@ -53,50 +55,9 @@ import { MatInputModule } from '@angular/material/input'; | |
| import { MatAutocompleteModule } from '@angular/material/autocomplete'; | ||
|
|
||
| @Component({ | ||
| selector: 'AutocompleteControlRenderer', | ||
| template: ` | ||
| <mat-form-field [ngStyle]="{ display: hidden ? 'none' : '' }"> | ||
| <mat-label>{{ label }}</mat-label> | ||
| <input | ||
| matInput | ||
| type="text" | ||
| (change)="onChange($event)" | ||
| [id]="id" | ||
| [formControl]="form" | ||
| [matAutocomplete]="auto" | ||
| (keydown)="updateFilter($event)" | ||
| (focus)="focused = true" | ||
| (focusout)="focused = false" | ||
| /> | ||
| <mat-autocomplete | ||
| autoActiveFirstOption | ||
| #auto="matAutocomplete" | ||
| (optionSelected)="onSelect($event)" | ||
| [displayWith]="displayFn" | ||
| > | ||
| @for (option of filteredOptions | async; track option.value) { | ||
| <mat-option [value]="option"> | ||
| {{ option.label }} | ||
| </mat-option> | ||
| } | ||
| </mat-autocomplete> | ||
| <mat-hint *ngIf="shouldShowUnfocusedDescription() || focused">{{ | ||
| description | ||
| }}</mat-hint> | ||
| <mat-error>{{ error }}</mat-error> | ||
| </mat-form-field> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| :host { | ||
| display: flex; | ||
| flex-direction: row; | ||
| } | ||
| mat-form-field { | ||
| flex: 1 1 auto; | ||
| } | ||
| `, | ||
| ], | ||
| selector: 'OneOfEnumControlRenderer', | ||
| templateUrl: './enum.renderer.html', | ||
| styleUrls: ['./enum.renderer.scss'], | ||
|
sdirix marked this conversation as resolved.
Outdated
|
||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| imports: [ | ||
| CommonModule, | ||
|
|
@@ -106,11 +67,11 @@ import { MatAutocompleteModule } from '@angular/material/autocomplete'; | |
| MatAutocompleteModule, | ||
| ], | ||
| }) | ||
| export class AutocompleteControlRenderer | ||
| export class OneOfEnumControlRenderer | ||
| extends JsonFormsControl | ||
| implements OnInit | ||
| { | ||
| @Input() options?: EnumOption[] | string[]; | ||
| @Input() options?: EnumOption[]; | ||
| valuesToTranslatedOptions?: Map<string, EnumOption>; | ||
| filteredOptions: Observable<EnumOption[]>; | ||
| shouldFilter: boolean; | ||
|
|
@@ -123,7 +84,7 @@ export class AutocompleteControlRenderer | |
| protected override mapToProps( | ||
| state: JsonFormsState | ||
| ): StatePropsOfControl & OwnPropsOfEnum { | ||
| return mapStateToEnumControlProps(state, this.getOwnProps()); | ||
| return mapStateToOneOfEnumControlProps(state, this.getOwnProps()); | ||
| } | ||
|
|
||
| getEventValue = (event: any) => event.target.value; | ||
|
|
@@ -209,29 +170,51 @@ export class AutocompleteControlRenderer | |
| protected getOwnProps(): OwnPropsOfControl & OwnPropsOfEnum { | ||
| return { | ||
| ...super.getOwnProps(), | ||
| options: this.stringOptionsToEnumOptions(this.options), | ||
| options: this.options, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * For {@link options} input backwards compatibility | ||
| */ | ||
| protected stringOptionsToEnumOptions( | ||
| options: typeof this.options | ||
| ): EnumOption[] | undefined { | ||
| if (!options) { | ||
| return undefined; | ||
| } | ||
| export const oneOfEnumControlTester: RankedTester = rankWith( | ||
| 5, | ||
| isOneOfEnumControl | ||
| ); | ||
|
|
||
| return options.every((item) => typeof item === 'string') | ||
| ? options.map((str) => { | ||
| return { | ||
| label: str, | ||
| value: str, | ||
| } satisfies EnumOption; | ||
| }) | ||
| : options; | ||
| @Component({ | ||
| selector: 'EnumControlRenderer, AutocompleteControlRenderer', | ||
| templateUrl: './enum.renderer.html', | ||
| styleUrls: ['./enum.renderer.scss'], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| imports: [ | ||
| CommonModule, | ||
| ReactiveFormsModule, | ||
| MatFormFieldModule, | ||
| MatInputModule, | ||
| MatAutocompleteModule, | ||
| ], | ||
| }) | ||
| export class EnumControlRenderer extends OneOfEnumControlRenderer { | ||
| // eslint-disable-next-line @angular-eslint/no-input-rename | ||
| @Input('options') | ||
| set stringOptions(strOptions: string[]) { | ||
| this.options = strOptions.map((str) => { | ||
| return { | ||
| label: str, | ||
| value: str, | ||
| }; | ||
| }); | ||
| } | ||
|
Comment on lines
+239
to
+249
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old We should remove the old component completely so they get an obvious error or make it backward compatible. If we remove it, we should mention it in the migration guide
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the Since #2535 is not part of any release, the |
||
|
|
||
| protected override mapToProps( | ||
| state: JsonFormsState | ||
| ): StatePropsOfControl & OwnPropsOfEnum { | ||
| return mapStateToEnumControlProps(state, this.getOwnProps()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * For {@link AutocompleteControlRenderer} class name backwards compatibility | ||
| */ | ||
| export { EnumControlRenderer as AutocompleteControlRenderer }; | ||
|
|
||
| export const enumControlTester: RankedTester = rankWith(2, isEnumControl); | ||
Uh oh!
There was an error while loading. Please reload this page.