Skip to content

Commit

Permalink
fix: use DateTime instance method isValid (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Oct 30, 2024
1 parent f72a463 commit 4dba903
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/components/DateField/hooks/useDateFieldState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';

import {isValid} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
import {useControlledState} from '@gravity-ui/uikit';

Expand Down Expand Up @@ -84,7 +83,7 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
}

const displayValue =
value && isValid(value) && isAllSegmentsValid(allSegments, validSegments)
value && value.isValid() && isAllSegmentsValid(allSegments, validSegments)
? value.timeZone(timeZone)
: placeholderDate.timeZone(timeZone);
const sectionsState = useSectionsState(sections, displayValue, validSegments);
Expand Down Expand Up @@ -201,7 +200,7 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState

function setValueFromString(str: string) {
const date = parseDateFromString(str, format, timeZone);
if (isValid(date)) {
if (date.isValid()) {
handleUpdateDate(date);
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/DateField/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {dateTime, expandFormat, isValid} from '@gravity-ui/date-utils';
import {dateTime, expandFormat} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';

import {mergeDateTime} from '../utils/dates';
Expand Down Expand Up @@ -612,7 +612,7 @@ export function formatSections(sections: DateFieldSection[]): string {

function parseDate(options: {input: string; format: string; timeZone?: string}) {
let date = dateTime(options);
if (!isValid(date)) {
if (!date.isValid()) {
date = dateTime({...options, format: undefined});
}
return date;
Expand All @@ -624,7 +624,7 @@ function isDateStringWithTimeZone(str: string) {

export function parseDateFromString(str: string, format: string, timeZone?: string): DateTime {
let date = parseDate({input: str, format, timeZone});
if (isValid(date)) {
if (date.isValid()) {
if (timeZone && !isDateStringWithTimeZone(str)) {
const time = parseDate({input: str, format});
date = mergeDateTime(date, time);
Expand Down
7 changes: 3 additions & 4 deletions src/components/RangeDateField/hooks/useRangeDateFieldState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable complexity */
import React from 'react';

import {isValid} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
import {useControlledState} from '@gravity-ui/uikit';

Expand Down Expand Up @@ -98,8 +97,8 @@ export function useRangeDateFieldState(props: RangeDateFieldStateOptions): Range

const displayValue =
value &&
isValid(value.start) &&
isValid(value.end) &&
value.start.isValid() &&
value.end.isValid() &&
Object.keys(validSegments.start).length >= Object.keys(allSegments).length &&
Object.keys(validSegments.end).length >= Object.keys(allSegments).length
? {start: value.start.timeZone(timeZone), end: value.end.timeZone(timeZone)}
Expand Down Expand Up @@ -236,7 +235,7 @@ export function useRangeDateFieldState(props: RangeDateFieldStateOptions): Range
const start = parseDateFromString(list?.[0]?.trim(), format, timeZone);
const end = parseDateFromString(list?.[1]?.trim(), format, timeZone);
const range = {start, end};
if (isValid(range.start) && isValid(range.end)) {
if (range.start.isValid() && range.end.isValid()) {
handleUpdateRange(range);
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/RangeDateField/utils/isValidRange.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {isValid} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';

import type {RangeValue} from '../../types';

export function isValidRange({start, end}: RangeValue<DateTime>): boolean {
return isValid(start) && isValid(end) && (start.isSame(end) || start.isBefore(end));
return start.isValid() && end.isValid() && (start.isSame(end) || start.isBefore(end));
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {dateTimeParse, isLikeRelative, isValid} from '@gravity-ui/date-utils';
import {dateTimeParse, isLikeRelative} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
import {useControlledState} from '@gravity-ui/uikit';

Expand Down Expand Up @@ -55,7 +55,7 @@ export function useRelativeDateFieldState(props: RelativeDateFieldOptions): Rela
setText(t);
if (isLikeRelative(t)) {
const date = dateTimeParse(t);
if (date && isValid(date)) {
if (date && date.isValid()) {
setValue(t);
} else {
setValue(null);
Expand Down

0 comments on commit 4dba903

Please sign in to comment.