15
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
import { CommonModule } from '@angular/common' ;
18
- import { ChangeDetectionStrategy , Component , inject , NgModule , OnDestroy , OnInit , Type } from '@angular/core' ;
18
+ import { ChangeDetectionStrategy , ChangeDetectorRef , Component , inject , NgModule , OnDestroy , OnInit , Type } from '@angular/core' ;
19
19
import { ReactiveFormsModule } from '@angular/forms' ;
20
20
import { FieldType , FormlyFieldConfig , FormlyModule } from '@ngx-formly/core' ;
21
21
import { FormlySelectModule as FormlyCoreSelectModule , FormlyFieldSelectProps } from '@ngx-formly/core/select' ;
22
22
import { FormlyFieldProps } from '@ngx-formly/primeng/form-field' ;
23
23
import { TranslateModule , TranslateService } from '@ngx-translate/core' ;
24
24
import { DropdownModule } from 'primeng/dropdown' ;
25
- import { Subscription } from 'rxjs' ;
25
+ import { map , merge , Observable , Subscription , switchMap , tap } from 'rxjs' ;
26
26
import { TranslateLabelService } from './translate-label.service' ;
27
27
28
28
export interface ISelectProps extends FormlyFieldProps , FormlyFieldSelectProps {
@@ -46,6 +46,7 @@ export interface ISelectProps extends FormlyFieldProps, FormlyFieldSelectProps {
46
46
tooltipPosition : 'left' | 'top' | 'bottom' | 'right' ;
47
47
tooltipPositionStyle : string ;
48
48
tooltipStyleClass ?: string ;
49
+ options ?: Observable < any [ ] > ;
49
50
}
50
51
51
52
export interface IFormlySelectFieldConfig extends FormlyFieldConfig < ISelectProps > {
@@ -71,7 +72,7 @@ export interface IFormlySelectFieldConfig extends FormlyFieldConfig<ISelectProps
71
72
[formlyAttributes]="field"
72
73
[group]="props.group"
73
74
[loadingIcon]="props.loadingIcon"
74
- [options]="props.options "
75
+ [options]="optionValues$|async "
75
76
[optionLabel]="props.group ? undefined : 'label'"
76
77
[optionValue]="props.group ? undefined : 'value'"
77
78
[panelStyleClass]="props.panelStyleClass"
@@ -120,6 +121,7 @@ export class SelectComponent extends FieldType<FormlyFieldConfig<ISelectProps>>
120
121
121
122
private translateService : TranslateService = inject ( TranslateService ) ;
122
123
private translateLabelService : TranslateLabelService = inject ( TranslateLabelService ) ;
124
+ private ref : ChangeDetectorRef = inject ( ChangeDetectorRef ) ;
123
125
124
126
private subscription : Subscription = new Subscription ( ) ;
125
127
@@ -142,19 +144,26 @@ export class SelectComponent extends FieldType<FormlyFieldConfig<ISelectProps>>
142
144
}
143
145
} ;
144
146
147
+ optionValues$ : Observable < any [ ] > ;
148
+
145
149
ngOnInit ( ) : void {
146
- this . translateLabelService . translateLabel ( this . props . options ) ;
147
- this . subscription . add ( this . translateService . onLangChange . subscribe ( ( ) => {
148
- this . translateLabelService . translateLabel ( this . props . options ) ;
149
- } ) ) ;
150
+ const optionsObs = this . props . options ;
151
+ const changeObs = this . translateService . onLangChange . pipe ( switchMap ( ( ) => this . optionValues$ ) ) ;
152
+ this . optionValues$ = merge ( ...[ optionsObs , changeObs ] )
153
+ . pipe (
154
+ map ( options => {
155
+ this . ref . markForCheck ( ) ;
156
+ return this . translateLabelService . translateLabel ( options ) ;
157
+ } )
158
+ ) ;
150
159
}
151
160
152
161
ngOnDestroy ( ) : void {
153
162
this . subscription . unsubscribe ( ) ;
154
163
}
155
164
156
165
clearValidators ( ) {
157
- const errors = this . formControl . errors ;
166
+ const { errors } = this . formControl ;
158
167
this . formControl . setErrors ( errors . required ? { required : true } : null ) ;
159
168
}
160
169
}
0 commit comments