Skip to content

Commit c2cd165

Browse files
jcorrea97rafaellmarques
authored andcommitted
feat(datepicker-range): suporte locale russo
Padroniza exibição de data no datepicker-range quando utilizado locale russo fixes DTHFUI-6949
1 parent e350185 commit c2cd165

10 files changed

+143
-76
lines changed

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range-base.component.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,17 @@ describe('PoDatepickerRangeBaseComponent:', () => {
9696
});
9797

9898
it('should be update property p-locale', () => {
99-
// expectPropertiesValues(component, 'locale', '', 'pt'); // @TODO: corrigir teste
10099
expectPropertiesValues(component, 'locale', ['pt', 'x'], 'pt');
101100
expectPropertiesValues(component, 'locale', 'en', 'en');
102101
expectPropertiesValues(component, 'locale', 'es', 'es');
103102
expectPropertiesValues(component, 'locale', 'ru', 'ru');
104103
});
105104

105+
it('should be update property p-locale with the default value ', () => {
106+
component['language'] = 'pt';
107+
expectPropertiesValues(component, 'locale', '', 'pt');
108+
});
109+
106110
it('disabled: should update with true value.', () => {
107111
const booleanValidTrueValues = [true, 'true', 1, ''];
108112

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range-base.component.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ import { poLocaleDefault } from '../../../services/po-language/po-language.const
55
import { PoLanguageService } from '../../../services/po-language/po-language.service';
66
import { requiredFailed } from '../validators';
77
import { PoDateService } from './../../../services/po-date/po-date.service';
8-
import { convertIsoToDate, convertToBoolean, setYearFrom0To100, validateDateRange } from './../../../utils/util';
8+
import {
9+
convertIsoToDate,
10+
convertToBoolean,
11+
setYearFrom0To100,
12+
validateDateRange,
13+
replaceFormatSeparator
14+
} from './../../../utils/util';
915
import { PoDatepickerRangeLiterals } from './interfaces/po-datepicker-range-literals.interface';
1016
import { PoDatepickerRange } from './interfaces/po-datepicker-range.interface';
1117
import { poDatepickerRangeLiteralsDefault } from './po-datepicker-range.literals';
18+
import { PoMask } from '../po-input/po-mask';
1219

1320
/**
1421
* @description
@@ -115,6 +122,7 @@ export abstract class PoDatepickerRangeBaseComponent implements ControlValueAcce
115122
protected isDateRangeInputFormatValid: boolean = true;
116123
protected isStartDateRangeInputValid: boolean = true;
117124
protected onTouchedModel: any;
125+
protected poMaskObject: PoMask;
118126

119127
private _clean?: boolean = false;
120128
private _disabled?;
@@ -384,16 +392,22 @@ export abstract class PoDatepickerRangeBaseComponent implements ControlValueAcce
384392
@Input('p-locale') set locale(value: string) {
385393
if (value) {
386394
this._locale = value.length >= 2 ? value : poLocaleDefault;
395+
this.poMaskObject = this.buildMask(
396+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
397+
);
387398
} else {
388399
this._locale = this.language;
400+
this.poMaskObject = this.buildMask(
401+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
402+
);
389403
}
390404
}
391405

392406
get locale(): string {
393407
return this._locale || this.language;
394408
}
395409

396-
constructor(protected poDateService: PoDateService, languageService: PoLanguageService) {
410+
constructor(protected poDateService: PoDateService, private languageService: PoLanguageService) {
397411
this.language = languageService.getShortLanguage();
398412
}
399413

@@ -507,6 +521,17 @@ export abstract class PoDatepickerRangeBaseComponent implements ControlValueAcce
507521
this.updateScreenByModel(this.dateRange);
508522
}
509523

524+
// Retorna um objeto do tipo PoMask com a mascara configurada.
525+
protected buildMask(format: string = this.format): PoMask {
526+
let mask = format.toUpperCase();
527+
528+
mask = mask.replace(/DD/g, '99');
529+
mask = mask.replace(/MM/g, '99');
530+
mask = mask.replace(/YYYY/g, '9999');
531+
532+
return new PoMask(mask, true);
533+
}
534+
510535
protected dateFormatFailed(value: string): boolean {
511536
return value && !this.poDateService.isValidIso(value);
512537
}

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range.component.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,33 @@ describe('PoDatepickerRangeComponent:', () => {
256256
expect(spy).not.toHaveBeenCalled();
257257
});
258258

259+
it(`ngOnChanges: shouldn't call 'updateScreenByModel' if 'changes' contain locale`, () => {
260+
const changes: any = {
261+
locale: 'pt'
262+
};
263+
264+
const spy = spyOn(component, <any>'buildMask');
265+
266+
component.ngOnChanges(changes);
267+
268+
expect(spy).toHaveBeenCalled();
269+
});
270+
271+
it(`ngOnChanges: shouldn't call 'updateScreenByModel' if 'changes' contain locale and contain 'dateRange'`, () => {
272+
const changes: any = {
273+
locale: 'pt'
274+
};
275+
component.dateRange = { start: '2023-01-25', end: '2023-02-21' };
276+
277+
const spyBuildMask = spyOn(component, <any>'buildMask');
278+
const spyUpdateScreenByModel = spyOn(component, <any>'updateScreenByModel');
279+
280+
component.ngOnChanges(changes);
281+
282+
expect(spyBuildMask).toHaveBeenCalled();
283+
expect(spyUpdateScreenByModel).toHaveBeenCalled();
284+
});
285+
259286
it('clear: should call `updateScreenByModel`, `resetDateRangeInputValidation` and `updateModel`', () => {
260287
spyOn(component, 'updateScreenByModel');
261288
spyOn(component, <any>'resetDateRangeInputValidation');
@@ -604,6 +631,13 @@ describe('PoDatepickerRangeComponent:', () => {
604631
expect(component['formatDate'](format, ...date)).toBe('2018-03-0');
605632
});
606633

634+
it('formatDate: should convert date to `yyyy-mm-` with default format value', () => {
635+
component['format'] = 'yyyy-mm-dd';
636+
const date = [undefined, '3', '2018'];
637+
638+
expect(component['formatDate'](undefined, ...date)).toBe('2018-03-0');
639+
});
640+
607641
it('formatScreenToModel: should return empty string if value is undefined', () => {
608642
const value = undefined;
609643

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range.component.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { PoControlPositionService } from './../../../services/po-control-positio
1919
import { PoDatepickerRange } from './interfaces/po-datepicker-range.interface';
2020
import { PoDatepickerRangeBaseComponent } from './po-datepicker-range-base.component';
2121
import { PoDateService } from './../../../services/po-date/po-date.service';
22-
import { PoMask } from '../po-input/po-mask';
2322
import { PoLanguageService } from '../../../services/po-language/po-language.service';
23+
import { replaceFormatSeparator } from './../../../utils/util';
2424

2525
const arrowLeftKey = 37;
2626
const arrowRightKey = 39;
@@ -93,7 +93,6 @@ export class PoDatepickerRangeComponent
9393
private clickListener;
9494
private eventResizeListener;
9595
private poDatepickerRangeElement: ElementRef<any>;
96-
private poMaskObject: PoMask;
9796

9897
get autocomplete() {
9998
return this.noAutocomplete ? 'off' : 'on';
@@ -139,9 +138,9 @@ export class PoDatepickerRangeComponent
139138
private controlPosition: PoControlPositionService,
140139
private renderer: Renderer2,
141140
private cd: ChangeDetectorRef,
141+
private poLanguageService: PoLanguageService,
142142
poDateService: PoDateService,
143-
poDatepickerRangeElement: ElementRef,
144-
poLanguageService: PoLanguageService
143+
poDatepickerRangeElement: ElementRef
145144
) {
146145
super(poDateService, poLanguageService);
147146
this.poDatepickerRangeElement = poDatepickerRangeElement;
@@ -170,13 +169,23 @@ export class PoDatepickerRangeComponent
170169

171170
ngOnInit() {
172171
// Classe de máscara
173-
this.poMaskObject = this.buildMask();
172+
this.poMaskObject = this.buildMask(
173+
replaceFormatSeparator(this.format, this.poLanguageService.getDateSeparator(this.locale))
174+
);
174175
}
175176

176177
ngOnChanges(changes: SimpleChanges): void {
177178
if (changes.minDate || changes.maxDate) {
178179
this.validateModel(this.dateRange);
179180
}
181+
if (changes.locale) {
182+
if (this.dateRange) {
183+
this.updateScreenByModel(this.dateRange);
184+
}
185+
this.poMaskObject = this.buildMask(
186+
replaceFormatSeparator(this.format, this.poLanguageService.getDateSeparator(this.locale))
187+
);
188+
}
180189
}
181190

182191
ngOnDestroy() {
@@ -309,19 +318,11 @@ export class PoDatepickerRangeComponent
309318
this.dateRangeField.nativeElement.classList.add('po-datepicker-range-field-focused');
310319
}
311320

312-
// Retorna um objeto do tipo PoMask com a mascara configurada.
313-
private buildMask(): PoMask {
314-
let mask = this.format.toUpperCase();
315-
316-
mask = mask.replace(/DD/g, '99');
317-
mask = mask.replace(/MM/g, '99');
318-
mask = mask.replace(/YYYY/g, '9999');
319-
320-
return new PoMask(mask, true);
321-
}
322-
323321
private formatDate(format: string, day: string = '', month: string = '', year: string = ''): string {
324-
let dateFormatted = format;
322+
let dateFormatted = replaceFormatSeparator(
323+
format || this.format,
324+
this.poLanguageService.getDateSeparator(this.locale)
325+
);
325326

326327
day = day && day.includes('T') ? day.slice(0, 2) : day;
327328

@@ -333,7 +334,7 @@ export class PoDatepickerRangeComponent
333334
}
334335

335336
private formatScreenToModel(value: string = ''): string {
336-
const [day, month, year] = value.split('/');
337+
const [day, month, year] = value.split(this.poLanguageService.getDateSeparator(this.locale));
337338

338339
return value ? this.formatDate('yyyy-mm-dd', day, month, year) : '';
339340
}

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker-base.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('PoDatepickerBaseComponent:', () => {
2424
const languageService: PoLanguageService = new PoLanguageService();
2525

2626
beforeEach(() => {
27-
component = new PoDatepickerComponent();
27+
component = new PoDatepickerComponent(languageService);
2828
component['shortLanguage'] = 'pt';
2929
});
3030

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker-base.component.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
formatYear,
1010
isTypeof,
1111
setYearFrom0To100,
12-
validateDateRange
12+
validateDateRange,
13+
replaceFormatSeparator
1314
} from '../../../utils/util';
1415
import { dateFailed, requiredFailed } from './../validators';
1516
import { InputBoolean } from '../../../decorators';
@@ -290,13 +291,17 @@ export abstract class PoDatepickerBaseComponent implements ControlValueAccessor,
290291
value = value.toLowerCase();
291292
if (value.match(/dd/) && value.match(/mm/) && value.match(/yyyy/)) {
292293
this._format = value;
293-
this.objMask = this.buildMask(this.replaceFormatSeparator());
294+
this.objMask = this.buildMask(
295+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
296+
);
294297
this.refreshValue(this.date);
295298
return;
296299
}
297300
}
298301
this._format = poDatepickerFormatDefault;
299-
this.objMask = this.buildMask(this.replaceFormatSeparator());
302+
this.objMask = this.buildMask(
303+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
304+
);
300305
}
301306

302307
get format() {
@@ -337,18 +342,22 @@ export abstract class PoDatepickerBaseComponent implements ControlValueAccessor,
337342
@Input('p-locale') set locale(value: string) {
338343
if (value) {
339344
this._locale = value.length >= 2 ? value : poLocaleDefault;
340-
this.objMask = this.buildMask(this.replaceFormatSeparator());
345+
this.objMask = this.buildMask(
346+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
347+
);
341348
} else {
342349
this._locale = this.shortLanguage;
343-
this.objMask = this.buildMask(this.replaceFormatSeparator());
350+
this.objMask = this.buildMask(
351+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
352+
);
344353
}
345354
this.refreshValue(this.date);
346355
}
347356
get locale() {
348357
return this._locale || this.shortLanguage;
349358
}
350359

351-
constructor() {}
360+
constructor(protected languageService: PoLanguageService) {}
352361

353362
set date(value: any) {
354363
this._date = typeof value === 'string' ? convertIsoToDate(value, false, false) : value;
@@ -360,7 +369,9 @@ export abstract class PoDatepickerBaseComponent implements ControlValueAccessor,
360369

361370
ngOnInit() {
362371
// Classe de máscara
363-
this.objMask = this.buildMask(this.replaceFormatSeparator());
372+
this.objMask = this.buildMask(
373+
replaceFormatSeparator(this.format, this.languageService.getDateSeparator(this.locale))
374+
);
364375
}
365376

366377
// Converte um objeto string em Date
@@ -484,8 +495,6 @@ export abstract class PoDatepickerBaseComponent implements ControlValueAccessor,
484495
return new PoMask(mask, true);
485496
}
486497

487-
protected abstract replaceFormatSeparator(): any;
488-
489498
abstract writeValue(value: any): void;
490499

491500
abstract refreshValue(value: Date): void;

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker.component.spec.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ describe('PoDatepickerComponent:', () => {
699699
const expectedData: any = '28/02/2019';
700700

701701
spyOn(UtilsFunctions, 'formatYear').and.returnValue('2019');
702-
spyOn(component, <any>'replaceFormatSeparator').and.returnValue('28/02/2019');
702+
spyOn(UtilsFunctions, <any>'replaceFormatSeparator').and.returnValue('28/02/2019');
703703

704704
const formattedDate = component.formatToDate(newDate);
705705
expect(formattedDate).toBe(expectedData);
@@ -1146,34 +1146,4 @@ describe('PoDatepickerComponent:', () => {
11461146
expect(fixture.debugElement.nativeElement.querySelector('po-clean')).toBe(null);
11471147
});
11481148
});
1149-
describe('replaceFormatSeparator: ', () => {
1150-
it('should show date separator as . according to russian locale selected', () => {
1151-
component.locale = 'ru';
1152-
component.format = 'dd/mm/yyyy';
1153-
const expectedFormat = 'dd.mm.yyyy';
1154-
const newFormat = component['replaceFormatSeparator']();
1155-
expect(newFormat).toBe(expectedFormat);
1156-
});
1157-
it('should show date separator as / according to portuguese locale selected', () => {
1158-
component.locale = 'pt';
1159-
component.format = 'dd/mm/yyyy';
1160-
const expectedFormat = 'dd/mm/yyyy';
1161-
const newFormat = component['replaceFormatSeparator']();
1162-
expect(newFormat).toBe(expectedFormat);
1163-
});
1164-
it('should show date separator as / according to english locale selected', () => {
1165-
component.locale = 'en';
1166-
component.format = 'dd/mm/yyyy';
1167-
const expectedFormat = 'dd/mm/yyyy';
1168-
const newFormat = component['replaceFormatSeparator']();
1169-
expect(newFormat).toBe(expectedFormat);
1170-
});
1171-
it('should show date separator as / according to spanish locale selected', () => {
1172-
component.locale = 'es';
1173-
component.format = 'dd/mm/yyyy';
1174-
const expectedFormat = 'dd/mm/yyyy';
1175-
const newFormat = component['replaceFormatSeparator']();
1176-
expect(newFormat).toBe(expectedFormat);
1177-
});
1178-
});
11791149
});

0 commit comments

Comments
 (0)