Skip to content

Commit a77b430

Browse files
vishalshrm539Vishal
andauthored
Fix User Reference issues (#261)
* Fix User Reference issues * Made getValue an arrow function, just to be consistent --------- Co-authored-by: Vishal <[email protected]>
1 parent ffa6582 commit a77b430

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

packages/angular-sdk-components/src/lib/_components/field/user-reference/user-reference.component.html

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
</div>
99
<div [formGroup]="formGroup$" *ngIf="type === 'dropdown'">
1010
<mat-form-field class="psdk-full-width" subscriptSizing="dynamic" [hintLabel]="helperText">
11-
<mat-select
12-
[value]="value$"
13-
[required]="bRequired$"
14-
[formControl]="fieldControl"
15-
[attr.data-test-id]="testId"
16-
(selectionChange)="fieldOnChange($event)"
17-
>
11+
<mat-select [required]="bRequired$" [formControl]="fieldControl" [attr.data-test-id]="testId" (selectionChange)="fieldOnChange($event)">
1812
<mat-option *ngFor="let opt of options$" [value]="opt.key">
1913
{{ opt.value }}
2014
</mat-option>
@@ -32,14 +26,12 @@
3226
matInput
3327
[placeholder]="placeholder"
3428
[formControl]="fieldControl"
35-
[value]="value$"
3629
[required]="bRequired$"
3730
[matAutocomplete]="auto"
3831
[attr.data-test-id]="testId"
39-
(change)="fieldOnChange($event)"
4032
(blur)="fieldOnBlur($event)"
4133
/>
42-
<mat-autocomplete #auto="matAutocomplete">
34+
<mat-autocomplete #auto="matAutocomplete" autoActiveFirstOption (optionSelected)="optionChanged($event)">
4335
<mat-option *ngFor="let opt of filteredOptions | async" [value]="opt.value">
4436
<span>{{ opt.value }}</span>
4537
</mat-option>

packages/angular-sdk-components/src/lib/_components/field/user-reference/user-reference.component.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)