Skip to content

Commit

Permalink
fix(components/input-date-range): added corrector if from is more tha…
Browse files Browse the repository at this point in the history
…n to #1628
  • Loading branch information
ZurabDev committed Jun 7, 2024
1 parent f6e8708 commit a46224c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
8 changes: 8 additions & 0 deletions libs/components/src/lib/@core/date-time/day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ export class PrizmDay extends PrizmMonth {
return new Date(this.year, this.month, this.day);
}

public getTime(): number {
return this.toLocalNativeDate().getTime();
}

public copy(): PrizmDay {
return PrizmDay.fromLocalNativeDate(this.toLocalNativeDate());
}

/**
* Returns native {@link Date} based on UTC
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,29 +222,58 @@ export class PrizmInputLayoutDateRangeComponent extends PrizmInputNgControl<Priz
}

public onValueFromChange(value: string, isFromValue: boolean): void {
// clear from mask
// Clear from mask
value = value.replace(/[_]/g, '');

if (isFromValue && value === this.fromValue) return;
if (!isFromValue && value === this.toValue) return;
this.nativeValue$$.next(
isFromValue ? [value, this.nativeValue$$.value[1]] : [this.nativeValue$$.value[0], value]
);
const currentValue = isFromValue ? this.fromValue : this.toValue;
if (value === currentValue) return;

const otherValue = isFromValue ? this.nativeValue$$.value[1] : this.nativeValue$$.value[0];
this.nativeValue$$.next(isFromValue ? [value, otherValue] : [otherValue, value]);

if (value == null) {
this.onOpenChange(true);
}

if (!value || value.length !== this.computedSingleMask.length) {
if (!value && isFromValue && !this.value?.to && !isFromValue && !this.value?.from)
if (!value && isFromValue && !this.value?.to && !isFromValue && !this.value?.from) {
this.updateValue(null);
}
return;
}

const parsedValue = PrizmDay.normalizeParse(value, this.dateFormat);
this.updateWithCorrectDateAndTime(
isFromValue ? parsedValue : (this.value?.from as any),
isFromValue ? this.value?.to : (parsedValue as any)
const [parsedFromValue, parsedToValue] = this.ensureCorrectDateOrder(
isFromValue,
isFromValue ? parsedValue : this.value?.from,
isFromValue ? this.value?.to : parsedValue
);

this.updateWithCorrectDateAndTime(parsedFromValue ?? null, parsedToValue ?? null);
}

/**
* Ensures that the date range is valid by adjusting the fromValue and toValue if necessary.
* If the fromValue is later than the toValue, it corrects the order based on the isFromValue flag.
*
* @param isFromValue - A boolean indicating if the change was made to the fromValue.
* @param fromValue - The starting date of the range.
* @param toValue - The ending date of the range.
* @returns A tuple containing the corrected fromValue and toValue.
*/
private ensureCorrectDateOrder(
isFromValue: boolean,
fromValue?: PrizmDay,
toValue?: PrizmDay
): [typeof fromValue, typeof toValue] {
if (fromValue && toValue && fromValue.getTime() > toValue.getTime()) {
if (isFromValue) {
fromValue = toValue.copy();
} else {
toValue = fromValue.copy();
}
}
return [fromValue, toValue];
}

public onRangeChange(range: PrizmDayRange | null): void {
Expand Down

0 comments on commit a46224c

Please sign in to comment.