@@ -34,10 +34,15 @@ import {
3434 Actions ,
3535 composeWithUi ,
3636 ControlElement ,
37+ EnumOption ,
3738 isEnumControl ,
39+ JsonFormsState ,
40+ mapStateToEnumControlProps ,
3841 OwnPropsOfControl ,
42+ OwnPropsOfEnum ,
3943 RankedTester ,
4044 rankWith ,
45+ StatePropsOfControl ,
4146} from '@jsonforms/core' ;
4247import type { Observable } from 'rxjs' ;
4348import { map , startWith } from 'rxjs/operators' ;
@@ -92,9 +97,9 @@ import { MatAutocompleteModule } from '@angular/material/autocomplete';
9297 >
9398 <mat-option
9499 *ngFor="let option of filteredOptions | async"
95- [value]="option"
100+ [value]="option.value "
96101 >
97- {{ option }}
102+ {{ option.label }}
98103 </mat-option>
99104 </mat-autocomplete>
100105 <mat-hint *ngIf="shouldShowUnfocusedDescription() || focused">{{
@@ -127,14 +132,22 @@ export class AutocompleteControlRenderer
127132 extends JsonFormsControl
128133 implements OnInit
129134{
130- @Input ( ) options : string [ ] ;
131- filteredOptions : Observable < string [ ] > ;
135+ @Input ( ) options ?: EnumOption [ ] | string [ ] ;
136+ translatedOptions ?: EnumOption [ ] ;
137+ filteredOptions : Observable < EnumOption [ ] > ;
132138 shouldFilter : boolean ;
133139 focused = false ;
134140
135141 constructor ( jsonformsService : JsonFormsAngularService ) {
136142 super ( jsonformsService ) ;
137143 }
144+
145+ protected mapToProps (
146+ state : JsonFormsState
147+ ) : StatePropsOfControl & OwnPropsOfEnum {
148+ return mapStateToEnumControlProps ( state , this . getOwnProps ( ) ) ;
149+ }
150+
138151 getEventValue = ( event : any ) => event . target . value ;
139152
140153 ngOnInit ( ) {
@@ -146,6 +159,10 @@ export class AutocompleteControlRenderer
146159 ) ;
147160 }
148161
162+ mapAdditionalProps ( _props : StatePropsOfControl & OwnPropsOfEnum ) {
163+ this . translatedOptions = _props . options ;
164+ }
165+
149166 updateFilter ( event : any ) {
150167 // ENTER
151168 if ( event . keyCode === 13 ) {
@@ -164,24 +181,40 @@ export class AutocompleteControlRenderer
164181 this . triggerValidation ( ) ;
165182 }
166183
167- filter ( val : string ) : string [ ] {
168- return ( this . options || this . scopedSchema . enum || [ ] ) . filter (
184+ filter ( val : string ) : EnumOption [ ] {
185+ return ( this . translatedOptions || [ ] ) . filter (
169186 ( option ) =>
170187 ! this . shouldFilter ||
171188 ! val ||
172- option . toLowerCase ( ) . indexOf ( val . toLowerCase ( ) ) === 0
189+ option . label . toLowerCase ( ) . indexOf ( val . toLowerCase ( ) ) === 0
173190 ) ;
174191 }
175- protected getOwnProps ( ) : OwnPropsOfAutoComplete {
192+ protected getOwnProps ( ) : OwnPropsOfControl & OwnPropsOfEnum {
176193 return {
177194 ...super . getOwnProps ( ) ,
178- options : this . options ,
195+ options : this . stringOptionsToEnumOptions ( this . options ) ,
179196 } ;
180197 }
181- }
182198
183- export const enumControlTester : RankedTester = rankWith ( 2 , isEnumControl ) ;
199+ /**
200+ * For {@link options} input backwards compatibility
201+ */
202+ protected stringOptionsToEnumOptions (
203+ options : typeof this . options
204+ ) : EnumOption [ ] | undefined {
205+ if ( ! options ) {
206+ return undefined ;
207+ }
184208
185- interface OwnPropsOfAutoComplete extends OwnPropsOfControl {
186- options : string [ ] ;
209+ return options . every ( ( item ) => typeof item === 'string' )
210+ ? options . map ( ( str ) => {
211+ return {
212+ label : str ,
213+ value : str ,
214+ } satisfies EnumOption ;
215+ } )
216+ : options ;
217+ }
187218}
219+
220+ export const enumControlTester : RankedTester = rankWith ( 2 , isEnumControl ) ;
0 commit comments