Skip to content

Commit

Permalink
refactor: change TuiRangeState from enum to type (#6564)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Jan 31, 2024
1 parent 9c00bd2 commit 3b845ca
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ export class TuiPrimitiveCalendarComponent {
}

if (value instanceof TuiDay) {
return value.daySame(item) ? TuiRangeState.Single : null;
return value.daySame(item) ? 'single' : null;
}

if (!(value instanceof TuiDayRange)) {
return value.find(day => day.daySame(item)) ? TuiRangeState.Single : null;
return value.find(day => day.daySame(item)) ? 'single' : null;
}

if (
Expand All @@ -132,7 +132,7 @@ export class TuiPrimitiveCalendarComponent {
hoveredItem.dayBefore(value.from) &&
value.isSingleDay)
) {
return TuiRangeState.Start;
return 'start';
}

if (
Expand All @@ -144,12 +144,10 @@ export class TuiPrimitiveCalendarComponent {
hoveredItem.dayAfter(value.from) &&
value.isSingleDay)
) {
return TuiRangeState.End;
return 'end';
}

return value.isSingleDay && value.from.daySame(item)
? TuiRangeState.Single
: null;
return value.isSingleDay && value.from.daySame(item) ? 'single' : null;
}

itemIsToday(item: TuiDay): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
TuiInteractiveState,
TuiPrimitiveCalendarComponent,
TuiPrimitiveCalendarModule,
TuiRangeState,
} from '@taiga-ui/core';
import {TUI_FIRST_DAY_OF_WEEK} from '@taiga-ui/core/tokens';
import {tuiMockCurrentDate, tuiRestoreRealDate} from '@taiga-ui/testing';
Expand Down Expand Up @@ -156,7 +155,7 @@ describe('PrimitiveCalendar', () => {

component.value = range;

expect(component.getItemRange(day1)).toBe(TuiRangeState.Start);
expect(component.getItemRange(day1)).toBe('start');
});

it('returns end correctly if there is range in value', () => {
Expand All @@ -166,7 +165,7 @@ describe('PrimitiveCalendar', () => {

component.value = range;

expect(component.getItemRange(day2)).toBe(TuiRangeState.End);
expect(component.getItemRange(day2)).toBe('end');
});

it('returns single if value is single day and item equals this', () => {
Expand All @@ -175,7 +174,7 @@ describe('PrimitiveCalendar', () => {

component.value = range;

expect(component.getItemRange(day1)).toBe(TuiRangeState.Single);
expect(component.getItemRange(day1)).toBe('single');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ export class TuiPrimitiveYearPickerComponent {
}

if (value instanceof TuiYear) {
return value.year === item ? TuiRangeState.Single : null;
return value.year === item ? 'single' : null;
}

if (!(value instanceof TuiMonthRange)) {
return value.find(day => day.year === item) ? TuiRangeState.Single : null;
return value.find(day => day.year === item) ? 'single' : null;
}

if (
Expand All @@ -145,7 +145,7 @@ export class TuiPrimitiveYearPickerComponent {
hoveredItem < value.from.year &&
value.from.yearSame(value.to))
) {
return TuiRangeState.Start;
return 'start';
}

if (
Expand All @@ -159,11 +159,11 @@ export class TuiPrimitiveYearPickerComponent {
hoveredItem > value.from.year &&
value.from.yearSame(value.to))
) {
return TuiRangeState.End;
return 'end';
}

return value.from.yearSame(value.to) && value.from.year === item
? TuiRangeState.Single
? 'single'
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
TuiInteractiveState,
TuiPrimitiveYearPickerComponent,
TuiPrimitiveYearPickerModule,
TuiRangeState,
} from '@taiga-ui/core';
import {TuiPageObject} from '@taiga-ui/testing';

Expand Down Expand Up @@ -104,7 +103,7 @@ describe('TuiPrimitiveYearPickerComponent', () => {
new TuiDay(2020, 1, 1),
);

expect(component.getItemRange(item)).toBe(TuiRangeState.Start);
expect(component.getItemRange(item)).toBe('start');
});

it('returns end correctly', () => {
Expand All @@ -115,7 +114,7 @@ describe('TuiPrimitiveYearPickerComponent', () => {
new TuiDay(item, 1, 1),
);

expect(component.getItemRange(item)).toBe(TuiRangeState.End);
expect(component.getItemRange(item)).toBe('end');
});

it('returns single correctly', () => {
Expand All @@ -126,7 +125,7 @@ describe('TuiPrimitiveYearPickerComponent', () => {
new TuiDay(item, 2, 2),
);

expect(component.getItemRange(item)).toBe(TuiRangeState.Single);
expect(component.getItemRange(item)).toBe('single');
});
});

Expand Down
8 changes: 1 addition & 7 deletions projects/core/enums/range-state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/**
* @internal used in calendar and year picker
*/
// TODO: change type in v4.0
// eslint-disable-next-line no-restricted-syntax
export enum TuiRangeState {
End = 'end',
Single = 'single',
Start = 'start',
}
export type TuiRangeState = 'end' | 'single' | 'start';
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class TuiCalendarMonthComponent implements TuiWithOptionalMinMax<TuiMonth
}

if (value instanceof TuiMonth) {
return value.monthSame(item) ? TuiRangeState.Single : null;
return value.monthSame(item) ? 'single' : null;
}

const theFirstOfRange = value.from.monthSame(item) && !value.isSingleMonth;
Expand All @@ -132,7 +132,7 @@ export class TuiCalendarMonthComponent implements TuiWithOptionalMinMax<TuiMonth
value.isSingleMonth;

if (theFirstOfRange || hoveredItemAfterFrom || hoveredItemIsCandidateToBeFrom) {
return TuiRangeState.Start;
return 'start';
}

const theLastOfRange = value.to.monthSame(item) && !value.isSingleMonth;
Expand All @@ -146,12 +146,10 @@ export class TuiCalendarMonthComponent implements TuiWithOptionalMinMax<TuiMonth
value.isSingleMonth;

if (theLastOfRange || hoveredItemBeforeTo || hoveredItemIsCandidateToBeTo) {
return TuiRangeState.End;
return 'end';
}

return value.isSingleMonth && value.from.monthSame(item)
? TuiRangeState.Single
: null;
return value.isSingleMonth && value.from.monthSame(item) ? 'single' : null;
}

getTuiMonth(monthNumber: number, yearNumber: number): TuiMonth {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component, ViewChild} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {TuiDay, TuiMonth, TuiMonthRange, TuiYear} from '@taiga-ui/cdk';
import {TuiInteractiveState, TuiRangeState} from '@taiga-ui/core';
import {TuiInteractiveState} from '@taiga-ui/core';
import {TuiCalendarMonthComponent, TuiCalendarMonthModule} from '@taiga-ui/kit';

const TODAY = TuiDay.currentLocal();
Expand Down Expand Up @@ -165,23 +165,23 @@ describe('CalendarMonth', () => {

component.value = month;

expect(component.getItemRange(month)).toBe(TuiRangeState.Single);
expect(component.getItemRange(month)).toBe('single');
});

it('returns start if item is start of range', () => {
const month = new TuiMonth(TODAY.year, 7);

component.value = new TuiMonthRange(month, month.append({month: 2}));

expect(component.getItemRange(month)).toBe(TuiRangeState.Start);
expect(component.getItemRange(month)).toBe('start');
});

it('returns end if item is start of range', () => {
const month = new TuiMonth(TODAY.year, 7);

component.value = new TuiMonthRange(month.append({month: -2}), month);

expect(component.getItemRange(month)).toBe(TuiRangeState.End);
expect(component.getItemRange(month)).toBe('end');
});

it('returns end if hovered item before item', () => {
Expand All @@ -190,7 +190,7 @@ describe('CalendarMonth', () => {
component.value = new TuiMonthRange(month, month);
component.hoveredItem = new TuiMonth(TODAY.year, 4);

expect(component.getItemRange(month)).toBe(TuiRangeState.End);
expect(component.getItemRange(month)).toBe('end');
});
});

Expand Down

0 comments on commit 3b845ca

Please sign in to comment.