Skip to content

Commit b742138

Browse files
fix: add validations to rangeDate filter (#806)
Signed-off-by: Carlos Feria <2582866+carlosthe19916@users.noreply.github.com>
1 parent 0ef318e commit b742138

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

client/src/app/components/FilterPanel/DateRangeFilter.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
DatePicker,
55
Form,
66
FormGroup,
7-
isValidDate as isValidJSDate,
7+
isValidDate,
88
} from "@patternfly/react-core";
99

1010
import {
@@ -72,6 +72,11 @@ export const DateRangeFilter = <TItem,>({
7272
}
7373
};
7474

75+
const toValidator = (date: Date) =>
76+
from && isValidDate(from) && date >= from
77+
? ""
78+
: 'The "to" date must be after the "from" date';
79+
7580
return (
7681
<Form>
7782
<FormGroup role="group" isInline label="From">
@@ -82,8 +87,7 @@ export const DateRangeFilter = <TItem,>({
8287
onChange={onFromDateChange}
8388
aria-label="Interval start"
8489
placeholder="MM/DD/YYYY"
85-
// disable error text (no space in toolbar scenario)
86-
invalidFormatText={""}
90+
invalidFormatText={"Invalid date"}
8791
// default value ("parent") creates collision with sticky table header
8892
appendTo={document.body}
8993
isDisabled={isDisabled}
@@ -93,12 +97,12 @@ export const DateRangeFilter = <TItem,>({
9397
<DatePicker
9498
value={to ? americanDateFormat(to) : ""}
9599
onChange={onToDateChange}
96-
isDisabled={isDisabled || !isValidJSDate(from)}
100+
isDisabled={isDisabled || !isValidDate(from)}
97101
dateFormat={americanDateFormat}
98102
dateParse={parseAmericanDate}
99-
// disable error text (no space in toolbar scenario)
100-
invalidFormatText={""}
103+
invalidFormatText={"Invalid date"}
101104
rangeStart={from}
105+
validators={[toValidator]}
102106
aria-label="Interval end"
103107
placeholder="MM/DD/YYYY"
104108
appendTo={document.body}

client/src/app/components/FilterToolbar/DateRangeFilter.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type ToolbarLabel,
88
type ToolbarLabelGroup,
99
Tooltip,
10-
isValidDate as isValidJSDate,
10+
isValidDate,
1111
} from "@patternfly/react-core";
1212

1313
import type { IFilterControlProps } from "./FilterControl";
@@ -115,6 +115,11 @@ export const DateRangeFilter = <TItem,>({
115115
}
116116
};
117117

118+
const toValidator = (date: Date) =>
119+
from && isValidDate(from) && date >= from
120+
? ""
121+
: 'The "to" date must be after the "from" date';
122+
118123
return (
119124
<ToolbarFilter
120125
key={category.categoryKey}
@@ -132,21 +137,20 @@ export const DateRangeFilter = <TItem,>({
132137
onChange={onFromDateChange}
133138
aria-label="Interval start"
134139
placeholder="MM/DD/YYYY"
135-
// disable error text (no space in toolbar scenario)
136-
invalidFormatText={""}
140+
invalidFormatText={"Invalid date"}
137141
// default value ("parent") creates collision with sticky table header
138142
appendTo={document.body}
139143
isDisabled={isDisabled}
140144
/>
141145
<DatePicker
142146
value={to ? americanDateFormat(to) : ""}
143147
onChange={onToDateChange}
144-
isDisabled={isDisabled || !isValidJSDate(from)}
148+
isDisabled={isDisabled || !isValidDate(from)}
145149
dateFormat={americanDateFormat}
146150
dateParse={parseAmericanDate}
147-
// disable error text (no space in toolbar scenario)
148-
invalidFormatText={""}
151+
invalidFormatText={"Invalid date"}
149152
rangeStart={from}
153+
validators={[toValidator]}
150154
aria-label="Interval end"
151155
placeholder="MM/DD/YYYY"
152156
appendTo={document.body}

0 commit comments

Comments
 (0)