[AAE-1539] People/Group - Add a formControl for the chip field (#5445)

* Add a formControl for the chip component

* on search reset the error and trim later on
This commit is contained in:
Maurizio Vitale
2020-02-04 16:53:37 +00:00
committed by GitHub
parent bd3957dcee
commit bab4906632
4 changed files with 54 additions and 10 deletions

View File

@@ -62,7 +62,7 @@
<mat-error *ngIf="searchGroupsControl.hasError('minlength')"> <mat-error *ngIf="searchGroupsControl.hasError('minlength')">
<mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon> <mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon>
{{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.INVALID_MIN_LENGTH' | translate: { requiredLength: getValidationMinLength() } }}</mat-error> {{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.INVALID_MIN_LENGTH' | translate: { requiredLength: getValidationMinLength() } }}</mat-error>
<mat-error *ngIf="searchGroupsControl.hasError('required')"> <mat-error *ngIf="searchGroupsControl.hasError('required') || groupChipsCtrl.hasError('required')">
<mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon> <mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon>
{{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.REQUIRED' | translate }} </mat-error> {{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.REQUIRED' | translate }} </mat-error>
<mat-error *ngIf="searchGroupsControl.hasError('searchTypingError') && !this.isFocused" data-automation-id="invalid-groups-typing-error"> <mat-error *ngIf="searchGroupsControl.hasError('searchTypingError') && !this.isFocused" data-automation-id="invalid-groups-typing-error">

View File

@@ -84,6 +84,10 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
@Input() @Input()
readOnly: boolean = false; readOnly: boolean = false;
/** FormControl to list of group */
@Input()
groupChipsCtrl: FormControl = new FormControl({ value: '', disabled: false });
/** FormControl to search the group */ /** FormControl to search the group */
@Input() @Input()
searchGroupsControl: FormControl = new FormControl({ value: '', disabled: false }); searchGroupsControl: FormControl = new FormControl({ value: '', disabled: false });
@@ -173,15 +177,19 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
initSearch(): void { initSearch(): void {
this.searchGroupsControl.valueChanges.pipe( this.searchGroupsControl.valueChanges.pipe(
debounceTime(500),
distinctUntilChanged(),
filter((value) => { filter((value) => {
return typeof value === 'string'; return typeof value === 'string';
}), }),
tap((value: string) => {
if (value) {
this.setTypingError();
}
}),
debounceTime(500),
distinctUntilChanged(),
tap((value) => { tap((value) => {
if (value.trim()) { if (value.trim()) {
this.searchedValue = value; this.searchedValue = value;
this.setTypingError();
} else { } else {
this.searchGroupsControl.markAsPristine(); this.searchGroupsControl.markAsPristine();
this.searchGroupsControl.markAsUntouched(); this.searchGroupsControl.markAsUntouched();
@@ -298,7 +306,7 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
} else { } else {
this.selectedGroups = this.removeDuplicatedGroups(this.preSelectGroups); this.selectedGroups = this.removeDuplicatedGroups(this.preSelectGroups);
} }
this.groupChipsCtrl.setValue(this.selectedGroups[0].name);
if (this.isValidationEnabled()) { if (this.isValidationEnabled()) {
this.isLoading = true; this.isLoading = true;
await this.validatePreselectGroups(); await this.validatePreselectGroups();
@@ -327,6 +335,7 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
this.groupInput.nativeElement.value = ''; this.groupInput.nativeElement.value = '';
this.searchGroupsControl.setValue(''); this.searchGroupsControl.setValue('');
this.groupChipsCtrlValue(this.selectedGroups[0].name);
this.changedGroups.emit(this.selectedGroups); this.changedGroups.emit(this.selectedGroups);
this.resetSearchGroups(); this.resetSearchGroups();
@@ -336,7 +345,14 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
this.removeGroup.emit(groupToRemove); this.removeGroup.emit(groupToRemove);
this.removeGroupFromSelected(groupToRemove); this.removeGroupFromSelected(groupToRemove);
this.changedGroups.emit(this.selectedGroups); this.changedGroups.emit(this.selectedGroups);
if (this.selectedGroups.length === 0) {
this.groupChipsCtrlValue('');
} else {
this.groupChipsCtrlValue(this.selectedGroups[0].name);
}
this.searchGroupsControl.markAsDirty(); this.searchGroupsControl.markAsDirty();
this.searchGroupsControl.markAsTouched();
if (this.isValidationEnabled()) { if (this.isValidationEnabled()) {
this.removeGroupFromValidation(groupToRemove); this.removeGroupFromValidation(groupToRemove);
@@ -344,6 +360,12 @@ export class GroupCloudComponent implements OnInit, OnChanges, OnDestroy {
} }
} }
private groupChipsCtrlValue(value: string) {
this.groupChipsCtrl.setValue(value);
this.groupChipsCtrl.markAsDirty();
this.groupChipsCtrl.markAsTouched();
}
private removeGroupFromSelected({ id, name }: IdentityGroupModel): void { private removeGroupFromSelected({ id, name }: IdentityGroupModel): void {
const indexToRemove = this.selectedGroups.findIndex(group => { const indexToRemove = this.selectedGroups.findIndex(group => {
return group.id === id && group.name === name; return group.id === id && group.name === name;

View File

@@ -56,7 +56,7 @@
<mat-error *ngIf="searchUserCtrl.hasError('minlength')"> <mat-error *ngIf="searchUserCtrl.hasError('minlength')">
<mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon> <mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon>
{{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.INVALID_MIN_LENGTH' | translate: { requiredLength: getValidationMinLength() } }}</mat-error> {{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.INVALID_MIN_LENGTH' | translate: { requiredLength: getValidationMinLength() } }}</mat-error>
<mat-error *ngIf="searchUserCtrl.hasError('required')"> <mat-error *ngIf="searchUserCtrl.hasError('required') || userChipsCtrl.hasError('required')">
<mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon> <mat-icon class="adf-start-task-cloud-error-icon">warning</mat-icon>
{{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.REQUIRED' | translate }} </mat-error> {{ 'ADF_CLOUD_PEOPLE_GROUPS.ERROR.REQUIRED' | translate }} </mat-error>
<mat-error *ngIf="searchUserCtrl.hasError('searchTypingError') && !this.isFocused" data-automation-id="invalid-users-typing-error"> <mat-error *ngIf="searchUserCtrl.hasError('searchTypingError') && !this.isFocused" data-automation-id="invalid-users-typing-error">

View File

@@ -92,6 +92,10 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
@Input() @Input()
preSelectUsers: IdentityUserModel[] = []; preSelectUsers: IdentityUserModel[] = [];
/** FormControl to list of users */
@Input()
userChipsCtrl: FormControl = new FormControl({ value: '', disabled: false });
/** FormControl to search the user */ /** FormControl to search the user */
@Input() @Input()
searchUserCtrl = new FormControl({ value: '', disabled: false }); searchUserCtrl = new FormControl({ value: '', disabled: false });
@@ -178,15 +182,19 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
private initSearch(): void { private initSearch(): void {
this.searchUserCtrl.valueChanges.pipe( this.searchUserCtrl.valueChanges.pipe(
debounceTime(500),
distinctUntilChanged(),
filter((value) => { filter((value) => {
return typeof value === 'string'; return typeof value === 'string';
}), }),
tap((value: string) => {
if (value) {
this.setTypingError();
}
}),
debounceTime(500),
distinctUntilChanged(),
tap((value: string) => { tap((value: string) => {
if (value.trim()) { if (value.trim()) {
this.searchedValue = value; this.searchedValue = value;
this.setTypingError();
} else { } else {
this.searchUserCtrl.markAsPristine(); this.searchUserCtrl.markAsPristine();
this.searchUserCtrl.markAsUntouched(); this.searchUserCtrl.markAsUntouched();
@@ -273,7 +281,7 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
} else { } else {
this.selectedUsers = this.removeDuplicatedUsers(this.preSelectUsers); this.selectedUsers = this.removeDuplicatedUsers(this.preSelectUsers);
} }
this.userChipsCtrl.setValue(this.selectedUsers[0].username);
if (this.isValidationEnabled()) { if (this.isValidationEnabled()) {
this.isLoading = true; this.isLoading = true;
await this.validatePreselectUsers(); await this.validatePreselectUsers();
@@ -386,6 +394,7 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
this.userInput.nativeElement.value = ''; this.userInput.nativeElement.value = '';
this.searchUserCtrl.setValue(''); this.searchUserCtrl.setValue('');
this.userChipsCtrlValue(this.selectedUsers[0].username);
this.changedUsers.emit(this.selectedUsers); this.changedUsers.emit(this.selectedUsers);
this.resetSearchUsers(); this.resetSearchUsers();
@@ -395,7 +404,14 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
this.removeUser.emit(userToRemove); this.removeUser.emit(userToRemove);
this.removeUserFromSelected(userToRemove); this.removeUserFromSelected(userToRemove);
this.changedUsers.emit(this.selectedUsers); this.changedUsers.emit(this.selectedUsers);
if (this.selectedUsers.length === 0) {
this.userChipsCtrlValue('');
} else {
this.userChipsCtrlValue(this.selectedUsers[0].username);
}
this.searchUserCtrl.markAsDirty(); this.searchUserCtrl.markAsDirty();
this.searchUserCtrl.markAsTouched();
if (this.isValidationEnabled()) { if (this.isValidationEnabled()) {
this.removeUserFromValidation(userToRemove); this.removeUserFromValidation(userToRemove);
@@ -403,6 +419,12 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
} }
} }
private userChipsCtrlValue(value: string) {
this.userChipsCtrl.setValue(value);
this.userChipsCtrl.markAsDirty();
this.userChipsCtrl.markAsTouched();
}
private removeUserFromSelected({ id, username, email }: IdentityUserModel): void { private removeUserFromSelected({ id, username, email }: IdentityUserModel): void {
const indexToRemove = this.selectedUsers.findIndex(user => { const indexToRemove = this.selectedUsers.findIndex(user => {
return user.id === id return user.id === id