Skip to content

Commit 6f4efe8

Browse files
authored
feat: add post validation to cps-autocomplete component (#404)
* fix add post validatio nto cps-autocomplete component * fix onBeforeOptionsHidden function
1 parent 69b18fc commit 6f4efe8

File tree

6 files changed

+475
-29
lines changed

6 files changed

+475
-29
lines changed

projects/composition/src/app/api-data/cps-autocomplete.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@
325325
"default": "500",
326326
"description": "Debounce time for inputChanged event."
327327
},
328+
{
329+
"name": "validating",
330+
"optional": false,
331+
"readonly": false,
332+
"type": "boolean",
333+
"default": "false",
334+
"description": "Determines whether the component is currently validating the selected value asynchronously."
335+
},
328336
{
329337
"name": "_value",
330338
"optional": false,

projects/composition/src/app/pages/autocomplete-page/autocomplete-page.component.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@
142142
prefixIcon="search"
143143
appearance="borderless">
144144
</cps-autocomplete>
145+
146+
<cps-autocomplete
147+
label="Autocomplete with async validation"
148+
[options]="options"
149+
optionLabel="name"
150+
optionInfo="info"
151+
placeholder="Select a city"
152+
[clearable]="true"
153+
[(ngModel)]="selectedOption"
154+
[ngModelOptions]="{ standalone: true }"
155+
[validating]="validating"
156+
(valueChanged)="onOptionSelected($event)"
157+
[externalError]="externalError"
158+
hint="This autocomplete simulates async validation upon selection.">
159+
</cps-autocomplete>
145160
</div>
146161
</form>
147162
</app-component-docs-viewer>

projects/composition/src/app/pages/autocomplete-page/autocomplete-page.component.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@ import { Component, OnInit } from '@angular/core';
22
import {
33
FormsModule,
44
ReactiveFormsModule,
5-
UntypedFormBuilder,
6-
UntypedFormGroup,
5+
FormBuilder,
6+
FormGroup,
77
Validators
88
} from '@angular/forms';
99
import { CpsAutocompleteComponent } from 'cps-ui-kit';
1010
import { ComponentDocsViewerComponent } from '../../components/component-docs-viewer/component-docs-viewer.component';
1111
import ComponentData from '../../api-data/cps-autocomplete.json';
12-
import {
13-
Observable,
14-
Subject,
15-
catchError,
16-
delay,
17-
finalize,
18-
of,
19-
switchMap
20-
} from 'rxjs';
12+
import { Observable, Subject, of, delay } from 'rxjs';
13+
import { switchMap, tap } from 'rxjs/operators';
2114
import { CommonModule } from '@angular/common';
2215

2316
@Component({
@@ -58,25 +51,30 @@ export class AutocompletePageComponent implements OnInit {
5851
{ title: 'Tesla', val: 'TSLA', ticker: 'TSLA' }
5952
];
6053

61-
form!: UntypedFormGroup;
54+
form!: FormGroup;
6255
syncVal: any = [];
6356
componentData = ComponentData;
6457

6558
isSingleLoading = false;
6659
isMultiLoading = false;
60+
externalError = '';
6761

6862
private _singleFilterOptionSubject$ = new Subject<string>();
6963
singleOptionsObservable$?: Observable<any>;
7064

7165
private _multiFilterOptionSubject$ = new Subject<string>();
7266
multiOptionsObservable$?: Observable<any>;
7367

68+
// New properties for the validating example
69+
validating = false;
70+
selectedOption: any = null;
71+
7472
get availableOptionInfo() {
7573
return this.options.map((option) => option.name).join(', ');
7674
}
7775

7876
// eslint-disable-next-line no-useless-constructor
79-
constructor(private _formBuilder: UntypedFormBuilder) {}
77+
constructor(private _formBuilder: FormBuilder) {}
8078

8179
ngOnInit(): void {
8280
this.form = this._formBuilder.group({
@@ -116,13 +114,12 @@ export class AutocompletePageComponent implements OnInit {
116114
if (single) this.isSingleLoading = true;
117115
else this.isMultiLoading = true;
118116
return this._getOptionsFromServer(value).pipe(
119-
catchError((error) => {
120-
console.error('Failed to retrieve options list', error);
121-
return of([]);
122-
}),
123-
finalize(() => {
124-
if (single) this.isSingleLoading = false;
125-
else this.isMultiLoading = false;
117+
// Handle errors and finalize loading state
118+
tap({
119+
complete: () => {
120+
if (single) this.isSingleLoading = false;
121+
else this.isMultiLoading = false;
122+
}
126123
})
127124
);
128125
})
@@ -135,4 +132,25 @@ export class AutocompletePageComponent implements OnInit {
135132
});
136133
return of(filteredRes).pipe(delay(1000));
137134
}
135+
136+
// Method to handle selection changes for async validation
137+
onOptionSelected(option: any) {
138+
this.validating = true;
139+
this.selectedOption = option;
140+
this.externalError = '';
141+
// Simulate async validation with a delay
142+
of(option)
143+
.pipe(
144+
delay(3000) // Simulate a delay of 2 seconds
145+
)
146+
.subscribe({
147+
next: () => {
148+
this.validating = false;
149+
},
150+
error: () => {
151+
// Handle errors and finalize validation state
152+
this.externalError = 'Validation failed';
153+
}
154+
});
155+
}
138156
}

projects/cps-ui-kit/src/lib/components/cps-autocomplete/cps-autocomplete.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
</div>
219219
</cps-menu>
220220
<cps-progress-linear
221-
*ngIf="loading"
221+
*ngIf="loading || validating"
222222
height="3"
223223
radius="4"
224224
opacity="0.3"
@@ -234,7 +234,7 @@
234234
<div
235235
*ngIf="(error || externalError) && !hideDetails"
236236
class="cps-autocomplete-error">
237-
{{ error }}
237+
{{ error || externalError }}
238238
</div>
239239
</div>
240240

0 commit comments

Comments
 (0)