mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[APPS-2108] Switch search date range to the material date adapter (#8973)
* cleanup and setup testing * switch to material adapter instead of adf one
This commit is contained in:
@@ -16,11 +16,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { SearchDateRangeComponent } from './search-date-range.component';
|
import { SearchDateRangeComponent } from './search-date-range.component';
|
||||||
import { MomentDateAdapter } from '@alfresco/adf-core';
|
|
||||||
import { DateAdapter } from '@angular/material/core';
|
import { DateAdapter } from '@angular/material/core';
|
||||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { ContentTestingModule } from '../../../testing/content.testing.module';
|
import { ContentTestingModule } from '../../../testing/content.testing.module';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { MomentDateAdapter } from '@angular/material-moment-adapter';
|
||||||
|
|
||||||
declare let moment: any;
|
declare let moment: any;
|
||||||
|
|
||||||
@@ -47,13 +47,6 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
|
|
||||||
afterEach(() => fixture.destroy());
|
afterEach(() => fixture.destroy());
|
||||||
|
|
||||||
it('should use moment adapter', () => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
expect(adapter instanceof MomentDateAdapter).toBe(true);
|
|
||||||
expect(component.datePickerFormat).toBe('DD/MM/YYYY');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should setup form elements on init', () => {
|
it('should setup form elements on init', () => {
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
@@ -62,13 +55,6 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
expect(component.form).toBeDefined();
|
expect(component.form).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should setup the format of the date from configuration', () => {
|
|
||||||
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
expect(adapter.overrideDisplayFormat).toBe(dateFormatFixture);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should setup form control with formatted valid date on change', () => {
|
it('should setup form control with formatted valid date on change', () => {
|
||||||
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
@@ -16,10 +16,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
|
import { FormControl, FormGroup, Validators } from '@angular/forms';
|
||||||
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
|
import { DateAdapter } from '@angular/material/core';
|
||||||
import { MOMENT_DATE_FORMATS, MomentDateAdapter, UserPreferencesService, UserPreferenceValues } from '@alfresco/adf-core';
|
import { UserPreferencesService, UserPreferenceValues } from '@alfresco/adf-core';
|
||||||
|
|
||||||
import { SearchWidget } from '../../models/search-widget.interface';
|
import { SearchWidget } from '../../models/search-widget.interface';
|
||||||
import { SearchWidgetSettings } from '../../models/search-widget-settings.interface';
|
import { SearchWidgetSettings } from '../../models/search-widget-settings.interface';
|
||||||
import { SearchQueryBuilderService } from '../../services/search-query-builder.service';
|
import { SearchQueryBuilderService } from '../../services/search-query-builder.service';
|
||||||
@@ -27,6 +26,7 @@ import { LiveErrorStateMatcher } from '../../forms/live-error-state-matcher';
|
|||||||
import { Moment } from 'moment';
|
import { Moment } from 'moment';
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
import { takeUntil } from 'rxjs/operators';
|
import { takeUntil } from 'rxjs/operators';
|
||||||
|
import { MAT_MOMENT_DATE_ADAPTER_OPTIONS, MomentDateAdapter } from '@angular/material-moment-adapter';
|
||||||
|
|
||||||
export interface DateRangeValue {
|
export interface DateRangeValue {
|
||||||
from: string;
|
from: string;
|
||||||
@@ -42,17 +42,17 @@ const DEFAULT_FORMAT_DATE: string = 'DD/MM/YYYY';
|
|||||||
templateUrl: './search-date-range.component.html',
|
templateUrl: './search-date-range.component.html',
|
||||||
styleUrls: ['./search-date-range.component.scss'],
|
styleUrls: ['./search-date-range.component.scss'],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
|
{ provide: DateAdapter, useClass: MomentDateAdapter },
|
||||||
{ provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }
|
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { strict: true } }
|
||||||
],
|
],
|
||||||
encapsulation: ViewEncapsulation.None,
|
encapsulation: ViewEncapsulation.None,
|
||||||
host: { class: 'adf-search-date-range' }
|
host: { class: 'adf-search-date-range' }
|
||||||
})
|
})
|
||||||
export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy {
|
export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy {
|
||||||
from: UntypedFormControl;
|
from: FormControl;
|
||||||
to: UntypedFormControl;
|
to: FormControl;
|
||||||
|
|
||||||
form: UntypedFormGroup;
|
form: FormGroup;
|
||||||
matcher = new LiveErrorStateMatcher();
|
matcher = new LiveErrorStateMatcher();
|
||||||
|
|
||||||
id: string;
|
id: string;
|
||||||
@@ -95,11 +95,8 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.datePickerFormat = this.settings?.dateFormat ? this.settings.dateFormat : DEFAULT_FORMAT_DATE;
|
this.datePickerFormat = this.settings?.dateFormat ? this.settings.dateFormat : DEFAULT_FORMAT_DATE;
|
||||||
|
|
||||||
const customDateAdapter = this.dateAdapter as MomentDateAdapter;
|
|
||||||
customDateAdapter.overrideDisplayFormat = this.datePickerFormat;
|
|
||||||
|
|
||||||
this.userPreferencesService
|
this.userPreferencesService
|
||||||
.select(UserPreferenceValues.Locale)
|
.select<string>(UserPreferenceValues.Locale)
|
||||||
.pipe(takeUntil(this.onDestroy$))
|
.pipe(takeUntil(this.onDestroy$))
|
||||||
.subscribe((locale) => this.setLocale(locale));
|
.subscribe((locale) => this.setLocale(locale));
|
||||||
|
|
||||||
@@ -117,14 +114,14 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
const splitValue = this.startValue.split('||');
|
const splitValue = this.startValue.split('||');
|
||||||
const fromValue = this.dateAdapter.parse(splitValue[0], this.datePickerFormat);
|
const fromValue = this.dateAdapter.parse(splitValue[0], this.datePickerFormat);
|
||||||
const toValue = this.dateAdapter.parse(splitValue[1], this.datePickerFormat);
|
const toValue = this.dateAdapter.parse(splitValue[1], this.datePickerFormat);
|
||||||
this.from = new UntypedFormControl(fromValue, validators);
|
this.from = new FormControl(fromValue, validators);
|
||||||
this.to = new UntypedFormControl(toValue, validators);
|
this.to = new FormControl(toValue, validators);
|
||||||
} else {
|
} else {
|
||||||
this.from = new UntypedFormControl('', validators);
|
this.from = new FormControl('', validators);
|
||||||
this.to = new UntypedFormControl('', validators);
|
this.to = new FormControl('', validators);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.form = new UntypedFormGroup({
|
this.form = new FormGroup({
|
||||||
from: this.from,
|
from: this.from,
|
||||||
to: this.to
|
to: this.to
|
||||||
});
|
});
|
||||||
@@ -220,7 +217,7 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onChangedHandler(event: any, formControl: UntypedFormControl) {
|
onChangedHandler(event: any, formControl: FormControl) {
|
||||||
const inputValue = event.value;
|
const inputValue = event.value;
|
||||||
const formatDate = this.dateAdapter.parse(inputValue, this.datePickerFormat);
|
const formatDate = this.dateAdapter.parse(inputValue, this.datePickerFormat);
|
||||||
if (formatDate?.isValid()) {
|
if (formatDate?.isValid()) {
|
||||||
@@ -234,12 +231,12 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
this.setFromMaxDate();
|
this.setFromMaxDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
setLocale(locale) {
|
setLocale(locale: string) {
|
||||||
this.dateAdapter.setLocale(locale);
|
this.dateAdapter.setLocale(locale);
|
||||||
moment.locale(locale);
|
moment.locale(locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
hasParseError(formControl): boolean {
|
hasParseError(formControl: FormControl): boolean {
|
||||||
return formControl.hasError('matDatepickerParse') && formControl.getError('matDatepickerParse').text;
|
return formControl.hasError('matDatepickerParse') && formControl.getError('matDatepickerParse').text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -83,7 +83,7 @@ export class UserPreferencesService {
|
|||||||
* @param property The property to watch
|
* @param property The property to watch
|
||||||
* @returns Notification callback
|
* @returns Notification callback
|
||||||
*/
|
*/
|
||||||
select(property: string): Observable<any> {
|
select<T = any>(property: string): Observable<T> {
|
||||||
return this.onChange
|
return this.onChange
|
||||||
.pipe(
|
.pipe(
|
||||||
map((userPreferenceStatus) => userPreferenceStatus[property]),
|
map((userPreferenceStatus) => userPreferenceStatus[property]),
|
||||||
|
Reference in New Issue
Block a user