Skip to content

Commit e175acd

Browse files
committed
feat: add includes validator
1 parent 3239437 commit e175acd

3 files changed

Lines changed: 36 additions & 19 deletions

File tree

projects/design-angular-kit/src/lib/components/form/autocomplete/autocomplete.component.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,43 @@ export class ItAutocompleteComponent extends ItAbstractFormComponent<string | nu
5050
/**
5151
* Function to set assistive hint label. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
5252
*/
53-
@Input() assistiveHintLabel: () => string = undefined;
53+
@Input() assistiveHintLabel: () => string = () =>
54+
'Quando i risultati del completamento automatico sono disponibili, usa le frecce su e giù per rivedere e Invio per selezionare. Utenti di dispositivi touch, esplora tramite tocco o con gesti di scorrimento';
5455

5556
/**
5657
* Function to set label in case of no result. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
5758
*/
58-
@Input() noResultsLabel: () => string = undefined;
59+
@Input() noResultsLabel: () => string = () => 'Nessun risultato trovato';
5960

6061
/**
6162
* Function to set label that alerts you that query's too short. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
6263
*/
63-
@Input() statusQueryTooShortLabel: (minQueryLength: number) => string = undefined;
64+
@Input() statusQueryTooShortLabel: (minQueryLength: number) => string = minQueryLength =>
65+
`Digita ${minQueryLength} o più caratteri per mostrare le opzioni di ricerca`;
6466

6567
/**
6668
* Function to set no results label. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
6769
*/
68-
@Input() statusNoResultsLabel: () => string = undefined;
70+
@Input() statusNoResultsLabel: () => string = () => 'Nessun risultato di ricerca';
6971

7072
/**
7173
* Function to set selected option label. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
7274
*/
73-
@Input() statusSelectedOptionLabel: (selectedOption: string, length: number, index: number) => string = undefined;
75+
@Input() statusSelectedOptionLabel: (selectedOption: string, length: number, index: number) => string = (selectedOption, length, index) =>
76+
`${selectedOption} ${index + 1} di ${length} è sottolineato`;
7477

7578
/**
7679
* Function to set status results label. For more information https://github.com/alphagov/accessible-autocomplete?tab=readme-ov-file#internationalization
7780
*/
78-
@Input() statusResultsLabel: (length: number, contentSelectedOption: string) => string = undefined;
81+
@Input() statusResultsLabel: (length: number, contentSelectedOption: string) => string = (length, contentSelectedOption) => {
82+
const words = {
83+
result: length === 1 ? 'risultato' : 'risultati',
84+
is: length === 1 ? 'è' : 'sono',
85+
available: length === 1 ? 'disponibile' : 'disponibili',
86+
};
87+
88+
return `${length} ${words.result} ${words.is} ${words.available}. ${contentSelectedOption}`;
89+
};
7990

8091
/**
8192
* Fired when value changes

projects/design-angular-kit/src/lib/validators/it-validators.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ export class ItValidators {
133133
return ItValidators.customPattern(PHONE_NUMBER_REGEX, { invalidTel: true });
134134
}
135135

136+
public static includes(possibleValues: string[]): ValidatorFn {
137+
return formControl => {
138+
return possibleValues?.includes(formControl.value) ? null : { invalidSelection: true };
139+
};
140+
}
141+
136142
/**
137143
* URL validator
138144
*/

src/app/form-input/model-driven-validation-example/model-driven-validation-example.component.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,9 @@ import { ItValidators } from 'design-angular-kit/validators/it-validators';
99
})
1010
export class ModelDrivenValidationExampleComponent {
1111
myForm: FormGroup;
12-
regions: string[];
12+
regions: string[] = [];
1313

1414
constructor(private _fb: FormBuilder) {
15-
const validators = [Validators.required, Validators.minLength(3), Validators.maxLength(10), Validators.pattern('[ab]+')];
16-
this.myForm = this._fb.group({
17-
taxCode: [null, ItValidators.taxCode],
18-
vat: [null, ItValidators.vatNumber],
19-
cap: [null, ItValidators.cap],
20-
email: [null],
21-
url: [null],
22-
phone: [null],
23-
region: [null],
24-
iban: [null, ItValidators.iban],
25-
myInput: ['', validators],
26-
});
2715
this.regions = [
2816
'Abruzzo',
2917
'Basilicata',
@@ -46,6 +34,18 @@ export class ModelDrivenValidationExampleComponent {
4634
"Valle d'Aosta",
4735
'Veneto',
4836
];
37+
const validators = [Validators.required, Validators.minLength(3), Validators.maxLength(10), Validators.pattern('[ab]+')];
38+
this.myForm = this._fb.group({
39+
taxCode: [null, ItValidators.taxCode],
40+
vat: [null, ItValidators.vatNumber],
41+
cap: [null, ItValidators.cap],
42+
email: [null],
43+
url: [null],
44+
phone: [null],
45+
region: [null, ItValidators.includes(this.regions)],
46+
iban: [null, ItValidators.iban],
47+
myInput: ['', validators],
48+
});
4949
}
5050

5151
value = '';

0 commit comments

Comments
 (0)