Skip to content

Commit

Permalink
Remove @angular/material dependency from cdk library (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimio authored Feb 5, 2024
1 parent d7ab7a7 commit 3ee79b8
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { Injectable } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { DateAdapter } from '@angular/material/core';
import { DateIntervalDescriptor } from '@angular-ru/cdk/typings';

import { endOfDay } from '../../end-of-day';
import { shiftDate } from '../../shift-date/shift-date';
import { startOfDay } from '../../start-of-day';
import { DateSuggestionStrategy } from '../domain/interfaces/date-suggestion-strategy';
import { DayOfWeek, FIRST_DAY_OF_WEEK } from '../tokens/first-day-of-week';

const WEEK_LENGTH: number = 7;

@Injectable()
export class DateSuggestionCalendarWeekStrategy implements DateSuggestionStrategy {
constructor(private readonly dateAdapter: DateAdapter<Date>) {}
constructor(@Inject(FIRST_DAY_OF_WEEK) private readonly firstDayOfWeek: DayOfWeek) {}

public updateIntervalFor(control: AbstractControl, { dateFromKey, dateToKey }: DateIntervalDescriptor): void {
const localizedDayOfWeek: number =
(new Date().getDay() + WEEK_LENGTH - this.dateAdapter.getFirstDayOfWeek()) % WEEK_LENGTH;
const localizedDayOfWeek: number = (new Date().getDay() + WEEK_LENGTH - this.firstDayOfWeek) % WEEK_LENGTH;

control.patchValue({
[dateFromKey]: startOfDay(shiftDate({ days: -localizedDayOfWeek })),
Expand Down
15 changes: 15 additions & 0 deletions libs/cdk/date/src/date-suggestion/tokens/first-day-of-week.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InjectionToken } from '@angular/core';

export const enum DayOfWeek {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}

export const FIRST_DAY_OF_WEEK: InjectionToken<DayOfWeek> = new InjectionToken<DayOfWeek>('First day of week', {
factory: (): DayOfWeek => DayOfWeek.Monday
});
1 change: 1 addition & 0 deletions libs/cdk/date/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { DateSuggestionLastDaysOfIntervalStrategy } from './date-suggestion/stra
export { DateSuggestionLastFewDaysStrategy } from './date-suggestion/strategies/date-suggestion-last-few-days.strategy';
export { DateSuggestionSomeDayAgoStrategy } from './date-suggestion/strategies/date-suggestion-some-day-ago.strategy';
export { DAYS_COUNT } from './date-suggestion/tokens/days-count';
export { DayOfWeek, FIRST_DAY_OF_WEEK } from './date-suggestion/tokens/first-day-of-week';
export { SUGGESTION_STRATEGY_MAP } from './date-suggestion/tokens/suggestion-strategy-map';
export { endOfDay } from './end-of-day';
export { getToday } from './get-today';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AfterViewInit, Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
import { NgControl } from '@angular/forms';
import { MatInput } from '@angular/material/input';
import { toStringValue } from '@angular-ru/cdk/string';

@Directive({ selector: '[convertCase]' })
Expand All @@ -10,7 +9,7 @@ export class ConvertCaseDirective implements AfterViewInit {

constructor(private readonly elementRef: ElementRef, @Optional() private readonly ngControl?: NgControl) {}

private get element(): MatInput {
private get element(): HTMLInputElement {
return this.elementRef.nativeElement;
}

Expand Down
9 changes: 8 additions & 1 deletion libs/cdk/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{
"name": "@angular-ru/cdk",
"version": "14.11.2",
"version": "15.0.0",
"description": "Angular-RU package",
"homepage": "https://github.com/Angular-RU/angular-ru-sdk/tree/master/libs/cdk#readme",
"bugs": "https://github.com/Angular-RU/angular-ru-sdk/issues",
"repository": "https://github.com/Angular-RU/angular-ru-sdk",
"license": "MIT",
"author": "angular-ru@yandex.ru",
"peerDependencies": {
"@angular/cdk": ">=13.0.0",
"@angular/common": ">=13.0.0",
"@angular/core": ">=13.0.0",
"@angular/forms": ">=13.0.0",
"@angular/router": ">=13.0.0"
},
"publishConfig": {
"access": "public"
}
Expand Down
4 changes: 1 addition & 3 deletions libs/cdk/tests/date/date-suggestion-custom.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { FormControl, FormGroup } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS, NativeDateModule } from '@angular/material/core';
import {
DateSuggestionComposer,
DateSuggestionModule,
Expand All @@ -21,8 +20,7 @@ describe('[TEST]: Trim Input', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DateSuggestionModule.forRoot(EXTENDED_STRATEGY_MAP), NativeDateModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }]
imports: [DateSuggestionModule.forRoot(EXTENDED_STRATEGY_MAP)]
}).compileComponents();

composer = TestBed.inject(DateSuggestionComposer);
Expand Down
28 changes: 14 additions & 14 deletions libs/cdk/tests/date/date-suggestion.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { TestBed } from '@angular/core/testing';
import { FormControl, FormGroup } from '@angular/forms';
import { DateAdapter, MATERIAL_SANITY_CHECKS, NativeDateModule } from '@angular/material/core';
import {
DateSuggestionComposer,
DateSuggestionModule,
DayOfWeek,
DEFAULT_SUGGESTION_STRATEGY_MAP,
DefaultDateIntervalSuggestion,
endOfDay,
FIRST_DAY_OF_WEEK,
shiftDate,
startOfDay
} from '@angular-ru/cdk/date';
Expand All @@ -19,19 +20,20 @@ describe('[TEST]: Trim Input', () => {

const mockToday: Date = new Date('28 Apr 2021 12:00:00');
const mockFirstWeekdays = [
{ firstDayOfWeekNumber: 0, firstDayOfWeek: new Date('25 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 1, firstDayOfWeek: new Date('26 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 2, firstDayOfWeek: new Date('27 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 3, firstDayOfWeek: new Date('28 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 4, firstDayOfWeek: new Date('22 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 5, firstDayOfWeek: new Date('23 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: 6, firstDayOfWeek: new Date('24 Apr 2021 12:00:00') }
{ firstDayOfWeekNumber: DayOfWeek.Sunday, firstDayOfWeek: new Date('25 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Monday, firstDayOfWeek: new Date('26 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Tuesday, firstDayOfWeek: new Date('27 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Wednesday, firstDayOfWeek: new Date('28 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Thursday, firstDayOfWeek: new Date('22 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Friday, firstDayOfWeek: new Date('23 Apr 2021 12:00:00') },
{ firstDayOfWeekNumber: DayOfWeek.Saturday, firstDayOfWeek: new Date('24 Apr 2021 12:00:00') }
];
const firstDayOfWeekFactory = jest.fn();

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DateSuggestionModule.forRoot(), NativeDateModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }]
imports: [DateSuggestionModule.forRoot()],
providers: [{ provide: FIRST_DAY_OF_WEEK, useFactory: firstDayOfWeekFactory }]
}).compileComponents();

composer = TestBed.inject(DateSuggestionComposer);
Expand Down Expand Up @@ -106,10 +108,8 @@ describe('[TEST]: Trim Input', () => {
it.each(mockFirstWeekdays)(
'set calendar week as interval (first day of week is %s)',
({ firstDayOfWeekNumber, firstDayOfWeek }) => {
const dateAdapter: DateAdapter<unknown> = TestBed.inject(DateAdapter);

// mocking locale day of week
jest.spyOn(dateAdapter, 'getFirstDayOfWeek').mockImplementation(() => firstDayOfWeekNumber);
firstDayOfWeekFactory.mockImplementation(() => firstDayOfWeekNumber);

// mocking today's week day
jest.useFakeTimers('modern');
Expand All @@ -122,7 +122,7 @@ describe('[TEST]: Trim Input', () => {
dateFrom: startOfDay(firstDayOfWeek),
dateTo: endOfDay(mockToday)
});
expect(form.getRawValue().dateFrom.getDay()).toBe(dateAdapter.getFirstDayOfWeek());
expect(form.getRawValue().dateFrom.getDay()).toBe(firstDayOfWeekNumber);
}
);
});
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/filter/filter-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { InputFilterModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -30,8 +28,7 @@ describe('[TEST]: inputFilter Config', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, MatInputModule, InputFilterModule.forRoot({ default: /\d+/ })],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [ReactiveFormsModule, InputFilterModule.forRoot({ default: /\d+/ })],
declarations: [TestComponent]
}).compileComponents();
});
Expand Down
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/filter/filter-dynamic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AbstractControl, FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { InputFilterModule } from '@angular-ru/cdk/directives';
import { FilterPredicate } from '@angular-ru/cdk/string';
Expand Down Expand Up @@ -37,8 +35,7 @@ describe('[TEST]: inputFilter Dynamic', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ReactiveFormsModule, MatInputModule, InputFilterModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [ReactiveFormsModule, InputFilterModule],
declarations: [DynamicTestComponent]
}).compileComponents();

Expand Down
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/filter/filter-form.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { InputFilterModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -45,8 +43,7 @@ describe('[TEST]: inputFilter Input', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ReactiveFormsModule, MatInputModule, InputFilterModule, FormsModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [ReactiveFormsModule, InputFilterModule, FormsModule],
declarations: [TestComponent]
}).compileComponents();

Expand Down
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/filter/filter-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { InputFilterModule } from '@angular-ru/cdk/directives';
import { FilterPredicate } from '@angular-ru/cdk/string';
Expand Down Expand Up @@ -38,8 +36,7 @@ describe('[TEST]: inputFilter Input', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ReactiveFormsModule, MatInputModule, InputFilterModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [ReactiveFormsModule, InputFilterModule],
declarations: [TestComponent]
}).compileComponents();

Expand Down
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/filter/filter-simple-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement, Input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { InputFilterModule } from '@angular-ru/cdk/directives';
import { REG_EXP_NO_CYRILLIC } from '@angular-ru/cdk/regexp';
Expand Down Expand Up @@ -37,8 +35,7 @@ describe('[TEST]: inputFilter Simple Input', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, MatInputModule, InputFilterModule.forRoot({ default: REG_EXP_NO_CYRILLIC })],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [ReactiveFormsModule, InputFilterModule.forRoot({ default: REG_EXP_NO_CYRILLIC })],
declarations: [TestComponent]
}).compileComponents();

Expand Down
3 changes: 1 addition & 2 deletions libs/cdk/tests/directives/initial-focus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, DebugElement, Input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { BrowserModule, By } from '@angular/platform-browser';
import { InitialFocusModule } from '@angular-ru/cdk/directives';

Expand Down Expand Up @@ -54,7 +53,7 @@ describe('[TEST]: Initial Focus', function () {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [BrowserModule, ReactiveFormsModule, FormsModule, MatInputModule, InitialFocusModule]
imports: [BrowserModule, ReactiveFormsModule, FormsModule, InitialFocusModule]
}).compileComponents();

fixture = TestBed.createComponent(TestComponent);
Expand Down
14 changes: 2 additions & 12 deletions libs/cdk/tests/directives/trim/disabled-trim-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, Component, DebugElement, Input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { BrowserModule, By } from '@angular/platform-browser';
import { TrimInputModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -37,16 +35,8 @@ describe('[TEST]: Disabling trim Input', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
BrowserModule,
ReactiveFormsModule,
FormsModule,
MatInputModule,
TrimInputModule,
NgxMaskModule.forRoot()
],
declarations: [TestComponent],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }]
imports: [BrowserModule, ReactiveFormsModule, FormsModule, TrimInputModule, NgxMaskModule.forRoot()],
declarations: [TestComponent]
}).compileComponents();

fixture = TestBed.createComponent(TestComponent);
Expand Down
12 changes: 1 addition & 11 deletions libs/cdk/tests/directives/trim/dynamic-trim-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { BrowserModule, By } from '@angular/platform-browser';
import { TrimInputModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -38,15 +36,7 @@ describe('[TEST]: Trim Input', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
BrowserModule,
ReactiveFormsModule,
FormsModule,
MatInputModule,
TrimInputModule,
NgxMaskModule.forRoot()
],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [BrowserModule, ReactiveFormsModule, FormsModule, TrimInputModule, NgxMaskModule.forRoot()],
declarations: [DynamicTestComponent]
}).compileComponents();

Expand Down
5 changes: 1 addition & 4 deletions libs/cdk/tests/directives/trim/ng-model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { BrowserModule, By } from '@angular/platform-browser';
import { TrimInputModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -33,8 +31,7 @@ describe('[TEST]: Trim Input', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BrowserModule, FormsModule, MatInputModule, TrimInputModule, NgxMaskModule.forRoot()],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }],
imports: [BrowserModule, FormsModule, TrimInputModule, NgxMaskModule.forRoot()],
declarations: [NgModelTestComponent]
}).compileComponents();

Expand Down
14 changes: 2 additions & 12 deletions libs/cdk/tests/directives/trim/trim-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ChangeDetectionStrategy, Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { MatInputModule } from '@angular/material/input';
import { BrowserModule, By } from '@angular/platform-browser';
import { TrimInputModule } from '@angular-ru/cdk/directives';
import { Nullable } from '@angular-ru/cdk/typings';
Expand Down Expand Up @@ -36,16 +34,8 @@ describe('[TEST]: Trim Input', function () {

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
BrowserModule,
ReactiveFormsModule,
FormsModule,
MatInputModule,
TrimInputModule,
NgxMaskModule.forRoot()
],
declarations: [TestComponent],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }]
imports: [BrowserModule, ReactiveFormsModule, FormsModule, TrimInputModule, NgxMaskModule.forRoot()],
declarations: [TestComponent]
}).compileComponents();

fixture = TestBed.createComponent(TestComponent);
Expand Down
4 changes: 1 addition & 3 deletions libs/cdk/tests/pipes/mutable-pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TestBed } from '@angular/core/testing';
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
import { DeepPathPipe, MutableTypePipe, MutableTypePipeModule } from '@angular-ru/cdk/pipes';
import { Immutable, Nullable, PlainObject } from '@angular-ru/cdk/typings';

Expand All @@ -8,8 +7,7 @@ describe('mutable', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [MutableTypePipeModule],
providers: [{ provide: MATERIAL_SANITY_CHECKS, useValue: false }]
imports: [MutableTypePipeModule]
}).compileComponents();
pipe = TestBed.inject(MutableTypePipe);
});
Expand Down
Loading

0 comments on commit 3ee79b8

Please sign in to comment.