-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into package-entity
- Loading branch information
Showing
15 changed files
with
644 additions
and
201 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
client/src/app/components/FilterPanel/DateRangeFilter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, { FormEvent, useState } from "react"; | ||
|
||
import { | ||
DatePicker, | ||
Form, | ||
FormGroup, | ||
isValidDate as isValidJSDate, | ||
} from "@patternfly/react-core"; | ||
|
||
import { | ||
americanDateFormat, | ||
isValidAmericanShortDate, | ||
isValidInterval, | ||
parseAmericanDate, | ||
parseInterval, | ||
toISODateInterval, | ||
} from "../FilterToolbar/dateUtils"; | ||
import { IFilterControlProps } from "./FilterControl"; | ||
|
||
/** | ||
* This Filter type enables selecting an closed date range. | ||
* Precisely given range [A,B] a date X in the range if A <= X <= B. | ||
* | ||
* **Props are interpreted as follows**:<br> | ||
* 1) filterValue - date range encoded as ISO 8601 time interval string ("dateFrom/dateTo"). Only date part is used (no time).<br> | ||
* 2) setFilterValue - accepts the list of ranges.<br> | ||
* | ||
*/ | ||
|
||
export const DateRangeFilter = <TItem,>({ | ||
category, | ||
filterValue, | ||
setFilterValue, | ||
isDisabled = false, | ||
}: React.PropsWithChildren< | ||
IFilterControlProps<TItem, string> | ||
>): JSX.Element | null => { | ||
const selectedFilters = filterValue ?? []; | ||
|
||
const validFilters = | ||
selectedFilters?.filter((interval) => | ||
isValidInterval(parseInterval(interval)) | ||
) ?? []; | ||
const [from, setFrom] = useState<Date>(); | ||
const [to, setTo] = useState<Date>(); | ||
|
||
// Update it if it changes externally | ||
React.useEffect(() => { | ||
if (filterValue?.[0]) { | ||
const [from, to] = parseInterval(filterValue?.[0]); | ||
setFrom(from.toDate()); | ||
setTo(to.toDate()); | ||
} else { | ||
setFrom(undefined); | ||
setTo(undefined); | ||
} | ||
}, [filterValue]); | ||
|
||
const onFromDateChange = ( | ||
event: FormEvent<HTMLInputElement>, | ||
value: string | ||
) => { | ||
if (isValidAmericanShortDate(value)) { | ||
setFrom(parseAmericanDate(value)); | ||
setTo(undefined); | ||
} | ||
}; | ||
|
||
const onToDateChange = (even: FormEvent<HTMLInputElement>, value: string) => { | ||
if (isValidAmericanShortDate(value)) { | ||
const newTo = parseAmericanDate(value); | ||
setTo(newTo); | ||
const target = toISODateInterval(from, newTo); | ||
if (target) { | ||
setFilterValue([target]); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<Form> | ||
<FormGroup role="group" isInline label="From"> | ||
<DatePicker | ||
value={from ? americanDateFormat(from) : ""} | ||
dateFormat={americanDateFormat} | ||
dateParse={parseAmericanDate} | ||
onChange={onFromDateChange} | ||
aria-label="Interval start" | ||
placeholder="MM/DD/YYYY" | ||
// disable error text (no space in toolbar scenario) | ||
invalidFormatText={""} | ||
// default value ("parent") creates collision with sticky table header | ||
appendTo={document.body} | ||
isDisabled={isDisabled} | ||
/> | ||
</FormGroup> | ||
<FormGroup role="group" isInline label="To"> | ||
<DatePicker | ||
value={to ? americanDateFormat(to) : ""} | ||
onChange={onToDateChange} | ||
isDisabled={isDisabled || !isValidJSDate(from)} | ||
dateFormat={americanDateFormat} | ||
dateParse={parseAmericanDate} | ||
// disable error text (no space in toolbar scenario) | ||
invalidFormatText={""} | ||
rangeStart={from} | ||
aria-label="Interval end" | ||
placeholder="MM/DD/YYYY" | ||
appendTo={document.body} | ||
/> | ||
</FormGroup> | ||
</Form> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.