Skip to content

Commit 812b28d

Browse files
Ivan KitanovIvan Kitanov
authored andcommitted
Refactoring some properties and types of the DateRangePicker
1 parent e34a27f commit 812b28d

File tree

6 files changed

+32
-75
lines changed

6 files changed

+32
-75
lines changed

src/components/date-range-picker/date-range-picker.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
escapeKey,
1818
} from '../common/controllers/key-bindings.js';
1919
import { defineComponents } from '../common/definitions/defineComponents.js';
20+
import { equal } from '../common/util.js';
2021
import {
2122
type ValidationContainerTestsParams,
2223
checkDatesEqual,
@@ -2272,7 +2273,7 @@ const checkSelectedRange = (
22722273
IgcCalendarComponent.tagName
22732274
)!;
22742275

2275-
DateTimeUtil.areDateRangesEqual(picker.value, expectedValue);
2276+
equal(picker.value, expectedValue);
22762277

22772278
if (!useTwoInputs) {
22782279
const input = picker.renderRoot.querySelector(IgcInputComponent.tagName)!;

src/components/date-range-picker/date-range-picker.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ import {
4949
defaultDateRangeTransformers,
5050
} from '../common/mixins/forms/form-value.js';
5151
import {
52+
clamp,
5253
createCounter,
54+
equal,
5355
findElementFromEventPath,
5456
last,
5557
} from '../common/util.js';
@@ -60,6 +62,11 @@ import IgcFocusTrapComponent from '../focus-trap/focus-trap.js';
6062
import IgcIconComponent from '../icon/icon.js';
6163
import IgcInputComponent from '../input/input.js';
6264
import IgcPopoverComponent from '../popover/popover.js';
65+
import type {
66+
ContentOrientation,
67+
DateRangePickerInput,
68+
PickerMode,
69+
} from '../types.js';
6370
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
6471
import { styles } from './date-range-picker.base.css.js';
6572
import { dateRangeValidators } from './validators.js';
@@ -226,7 +233,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
226233
private _placeholder?: string;
227234
private _defaultMask!: string;
228235
private _oldValue: DateRangeValue | null = null;
229-
private _visibleMonths: 1 | 2 = 2;
236+
private _visibleMonths = 2;
230237

231238
private predefinedRanges: CustomDateRange[] = [
232239
{
@@ -333,7 +340,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
333340
* @attr mode
334341
*/
335342
@property()
336-
public mode: 'dropdown' | 'dialog' = 'dropdown';
343+
public mode: PickerMode = 'dropdown';
337344

338345
/**
339346
* Use two inputs to display the date range values. Makes the input editable in dropdown mode.
@@ -380,12 +387,12 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
380387
*/
381388
//
382389
@property({ type: Number, attribute: 'visible-months' })
383-
public get visibleMonths(): 1 | 2 {
390+
public get visibleMonths(): number {
384391
return this._visibleMonths;
385392
}
386393

387-
public set visibleMonths(value: 1 | 2) {
388-
this._visibleMonths = [1, 2].includes(value) ? value : 2;
394+
public set visibleMonths(value: number) {
395+
this._visibleMonths = clamp(value, 1, 2);
389396
}
390397

391398
/**
@@ -508,14 +515,14 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
508515
* @attr header-orientation
509516
*/
510517
@property({ attribute: 'header-orientation', reflect: true })
511-
public headerOrientation: 'vertical' | 'horizontal' = 'horizontal';
518+
public headerOrientation: ContentOrientation = 'horizontal';
512519

513520
/**
514521
* The orientation of the multiple months displayed in the calendar's days view.
515522
* @attr
516523
*/
517524
@property()
518-
public orientation: 'vertical' | 'horizontal' = 'horizontal';
525+
public orientation: ContentOrientation = 'horizontal';
519526

520527
/**
521528
* Determines whether the calendar hides its header.
@@ -638,7 +645,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
638645

639646
protected handleDialogClosing(event: Event) {
640647
event.stopPropagation();
641-
if (!DateTimeUtil.areDateRangesEqual(this.value, this._oldValue)) {
648+
if (!equal(this.value, this._oldValue)) {
642649
this.emitEvent('igcChange', { detail: this.value });
643650
}
644651
this._hide(true);
@@ -658,7 +665,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
658665
}
659666

660667
protected dialogDone() {
661-
if (!DateTimeUtil.areDateRangesEqual(this.value, this._oldValue)) {
668+
if (!equal(this.value, this._oldValue)) {
662669
this.emitEvent('igcChange', { detail: this.value });
663670
}
664671
this._hide(true);
@@ -733,10 +740,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
733740
if (!this.contains(relatedTarget as Node)) {
734741
this.checkValidity();
735742

736-
const isSameValue = DateTimeUtil.areDateRangesEqual(
737-
this.value,
738-
this._oldValue
739-
);
743+
const isSameValue = equal(this.value, this._oldValue);
740744
if (!this.useTwoInputs && !this.readOnly && !isSameValue) {
741745
this.emitEvent('igcChange', { detail: this.value });
742746
}
@@ -957,7 +961,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
957961
}
958962
}
959963

960-
private renderClearIcon(picker: 'start' | 'end' = 'start') {
964+
private renderClearIcon(picker: DateRangePickerInput = 'start') {
961965
const clearIcon = !this.useTwoInputs
962966
? 'clear-icon'
963967
: `clear-icon-${picker}`;
@@ -980,7 +984,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
980984
`;
981985
}
982986

983-
private renderCalendarIcon(picker: 'start' | 'end' = 'start') {
987+
private renderCalendarIcon(picker: DateRangePickerInput = 'start') {
984988
const defaultIcon = html`
985989
<igc-icon name="today" collection="default" aria-hidden="true"></igc-icon>
986990
`;
@@ -1118,7 +1122,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
11181122
return IgcValidationContainerComponent.create(this);
11191123
}
11201124

1121-
protected renderInput(id: string, picker: 'start' | 'end' = 'start') {
1125+
protected renderInput(id: string, picker: DateRangePickerInput = 'start') {
11221126
const readOnly = !this.isDropDown || this.readOnly || this.nonEditable;
11231127
const placeholder =
11241128
picker === 'start' ? this.placeholderStart : this.placeholderEnd;

src/components/date-time-input/date-util.spec.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -645,28 +645,4 @@ describe('Date Util', () => {
645645
DateTimeUtil.spinAmPm(currentDate, newDate, 'AM');
646646
expect(currentDate.getHours()).to.equal(4);
647647
});
648-
649-
it('should correctly compare DateRangeValue objects', () => {
650-
const now = new Date();
651-
const later = new Date(now.getTime() + 1000 * 60 * 60); // 1 hour later
652-
653-
const range1 = { start: now, end: later };
654-
const range2 = { start: new Date(now), end: new Date(later) };
655-
const range3 = { start: new Date(now), end: new Date(now) };
656-
657-
expect(DateTimeUtil.areDateRangesEqual(range1, range2)).to.be.true;
658-
659-
expect(DateTimeUtil.areDateRangesEqual(range1, range3)).to.be.false;
660-
661-
expect(DateTimeUtil.areDateRangesEqual(range1, null)).to.be.false;
662-
expect(DateTimeUtil.areDateRangesEqual(null, range2)).to.be.false;
663-
664-
expect(DateTimeUtil.areDateRangesEqual(null, null)).to.be.true;
665-
666-
const partial1 = { start: null, end: later };
667-
const partial2 = { start: null, end: new Date(later) };
668-
const partial3 = { start: now, end: later };
669-
expect(DateTimeUtil.areDateRangesEqual(partial1, partial2)).to.be.true;
670-
expect(DateTimeUtil.areDateRangesEqual(partial1, partial3)).to.be.false;
671-
});
672648
});

src/components/date-time-input/date-util.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { parseISODate } from '../calendar/helpers.js';
2-
import type { DateRangeValue } from '../date-range-picker/date-range-picker.js';
32
import { MaskParser } from '../mask-input/mask-parser.js';
43

54
export enum FormatDesc {
@@ -554,29 +553,6 @@ export abstract class DateTimeUtil {
554553
: format;
555554
}
556555

557-
/**
558-
* Compares two date-range values.
559-
*
560-
* @param first, @param second - the values to compare
561-
*/
562-
public static areDateRangesEqual(
563-
first: DateRangeValue | null,
564-
second: DateRangeValue | null
565-
): boolean {
566-
if (!first && !second) {
567-
return true;
568-
}
569-
570-
if (!first || !second) {
571-
return false;
572-
}
573-
574-
const isStartEqual = first.start?.getTime() === second.start?.getTime();
575-
const isEndEqual = first.end?.getTime() === second.end?.getTime();
576-
577-
return isStartEqual && isEndEqual;
578-
}
579-
580556
private static setDisplayFormatOptions(
581557
value: Date,
582558
format: string,

src/components/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type AbsolutePosition = 'bottom' | 'middle' | 'top';
66
export type ContentOrientation = 'horizontal' | 'vertical';
77
export type HorizontalTransitionAnimation = 'slide' | 'fade' | 'none';
88
export type PickerMode = 'dropdown' | 'dialog';
9+
export type DateRangePickerInput = 'start' | 'end';
910
export type PopoverScrollStrategy = 'scroll' | 'block' | 'close';
1011
export type RangeTextSelectMode = 'preserve' | 'select' | 'start' | 'end';
1112
export type SelectionRangeDirection = 'none' | 'backward' | 'forward';

stories/date-range-picker.stories.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ const metadata: Meta<IgcDateRangePickerComponent> = {
8686
table: { defaultValue: { summary: 'false' } },
8787
},
8888
visibleMonths: {
89-
type: '1 | 2',
89+
type: 'number',
9090
description: 'The number of months displayed in the calendar.',
91-
options: ['1', '2'],
92-
control: { type: 'inline-radio' },
91+
control: 'number',
9392
},
9493
label: {
9594
type: 'string',
@@ -156,17 +155,17 @@ const metadata: Meta<IgcDateRangePickerComponent> = {
156155
table: { defaultValue: { summary: '_' } },
157156
},
158157
headerOrientation: {
159-
type: '"vertical" | "horizontal"',
158+
type: '"horizontal" | "vertical"',
160159
description: 'The orientation of the calendar header.',
161-
options: ['vertical', 'horizontal'],
160+
options: ['horizontal', 'vertical'],
162161
control: { type: 'inline-radio' },
163162
table: { defaultValue: { summary: 'horizontal' } },
164163
},
165164
orientation: {
166-
type: '"vertical" | "horizontal"',
165+
type: '"horizontal" | "vertical"',
167166
description:
168167
"The orientation of the multiple months displayed in the calendar's days view.",
169-
options: ['vertical', 'horizontal'],
168+
options: ['horizontal', 'vertical'],
170169
control: { type: 'inline-radio' },
171170
table: { defaultValue: { summary: 'horizontal' } },
172171
},
@@ -306,7 +305,7 @@ interface IgcDateRangePickerArgs {
306305
/** Whether the control will show chips with predefined ranges. */
307306
usePredefinedRanges: boolean;
308307
/** The number of months displayed in the calendar. */
309-
visibleMonths: 1 | 2;
308+
visibleMonths: number;
310309
/** The label of the control (single input). */
311310
label: string;
312311
/** The label attribute of the start input. */
@@ -336,9 +335,9 @@ interface IgcDateRangePickerArgs {
336335
/** The prompt symbol to use for unfilled parts of the mask. */
337336
prompt: string;
338337
/** The orientation of the calendar header. */
339-
headerOrientation: 'vertical' | 'horizontal';
338+
headerOrientation: 'horizontal' | 'vertical';
340339
/** The orientation of the multiple months displayed in the calendar's days view. */
341-
orientation: 'vertical' | 'horizontal';
340+
orientation: 'horizontal' | 'vertical';
342341
/** Determines whether the calendar hides its header. */
343342
hideHeader: boolean;
344343
/**

0 commit comments

Comments
 (0)