@@ -23,6 +23,7 @@ interface UserReferenceProps extends Omit<PConnFieldProps, 'value'> {
2323 value ?: any ;
2424 showAsFormattedText ?: boolean ;
2525 additionalProps ?: object ;
26+ onRecordChange ?: any ;
2627}
2728
2829@Component ( {
@@ -66,6 +67,7 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
6667 fieldControl = new FormControl ( '' , null ) ;
6768 actionsApi : Object ;
6869 propName : string ;
70+ onRecordChange : any ;
6971
7072 constructor (
7173 private angularPConnect : AngularPConnectService ,
@@ -83,11 +85,11 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
8385 if ( this . formGroup$ ) {
8486 // add control to formGroup
8587 this . formGroup$ . addControl ( this . controlName$ , this . fieldControl ) ;
86- this . fieldControl . setValue ( this . value$ ) ;
88+ this . fieldControl . setValue ( this . getValue ( this . value$ ) ) ;
8789 }
8890
8991 this . filteredOptions = this . fieldControl . valueChanges . pipe (
90- startWith ( '' ) ,
92+ startWith ( this . getValue ( this . value$ ) || '' ) ,
9193 map ( value => this . _filter ( value || '' ) )
9294 ) ;
9395 }
@@ -126,6 +128,21 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
126128 return this . options$ ?. filter ( option => option . value ?. toLowerCase ( ) . includes ( filterVal ) ) ;
127129 }
128130
131+ isUserNameAvailable = user => {
132+ return typeof user === 'object' && user !== null && user . userName ;
133+ } ;
134+
135+ getUserName = user => {
136+ return user . userName ;
137+ } ;
138+
139+ getValue = user => {
140+ if ( this . displayAs$ === DROPDOWN_LIST ) {
141+ return this . utils . getUserId ( user ) || this . getUserName ( user ) ;
142+ }
143+ return this . isUserNameAvailable ( user ) ? this . getUserName ( user ) : this . utils . getUserId ( user ) ;
144+ } ;
145+
129146 async checkAndUpdate ( ) {
130147 // Should always check the bridge to see if the component should
131148 // update itself (re-render)
@@ -140,6 +157,7 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
140157 async updateSelf ( ) {
141158 const props = this . pConn$ . getConfigProps ( ) as UserReferenceProps ;
142159 this . testId = props . testId ;
160+ this . onRecordChange = props ?. onRecordChange ;
143161
144162 const { label, displayAs, value, showAsFormattedText, helperText, placeholder, displayMode } = props ;
145163
@@ -150,20 +168,18 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
150168 this . placeholder = placeholder || '' ;
151169 this . displayMode$ = displayMode ;
152170
171+ this . value$ = this . pConn$ . getConfigProps ( ) ?. value ;
172+
153173 const { readOnly, required } = props ;
154174 [ this . bReadonly$ , this . bRequired$ ] = [ readOnly , required ] . map ( prop => prop === true || ( typeof prop === 'string' && prop === 'true' ) ) ;
155175
156176 this . actionsApi = this . pConn$ . getActionsApi ( ) ;
157177 this . propName = this . pConn$ . getStateProps ( ) . value ;
158178
159- const isUserNameAvailable = user => {
160- return typeof user === 'object' && user !== null && user . userName ;
161- } ;
162-
163179 this . userID$ = this . utils . getUserId ( value ) ;
164180
165181 if ( this . userID$ && this . bReadonly$ && this . showAsFormattedText$ ) {
166- if ( isUserNameAvailable ( value ) ) {
182+ if ( this . isUserNameAvailable ( value ) ) {
167183 this . userName$ = value . userName ;
168184 } else {
169185 // if same user ref field is referred in view as editable & readonly formatted text
@@ -201,7 +217,12 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
201217 if ( event ?. target ) {
202218 this . filterValue = ( event . target as HTMLInputElement ) . value ;
203219 }
204- const value = event ?. target ?. value ;
220+ const value = event ?. value ;
221+ handleEvent ( this . actionsApi , 'change' , this . propName , value ) ;
222+ }
223+
224+ optionChanged ( event : any ) {
225+ const value = event ?. option ?. value ;
205226 handleEvent ( this . actionsApi , 'change' , this . propName , value ) ;
206227 }
207228
@@ -211,11 +232,12 @@ export class UserReferenceComponent implements OnInit, OnDestroy {
211232 const index = this . options$ ?. findIndex ( element => element . value === event . target . value ) ;
212233 key = index > - 1 ? ( key = this . options$ [ index ] . key ) : event . target . value ;
213234 }
214-
215- const value = {
216- value : key
217- } ;
235+ const value = key ;
218236 handleEvent ( this . actionsApi , 'changeNblur' , this . propName , value ) ;
237+ if ( this . onRecordChange ) {
238+ event . target . value = value ;
239+ this . onRecordChange ( event ) ;
240+ }
219241 }
220242
221243 getErrorMessage ( ) {
0 commit comments