-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to filter events at given date. Key points: 1. multiple dates can be choosen 2. ISO date format used (YYYY-MM-DD) regardless of the locale 3. date comparison is done in UTC zone 4. Migration started column (timestamp) added to Plans table Reference-Url: https://github.com/oVirt/ovirt-web-ui/blob/ea9a10e75e7dc965ddc6a1a7f2d36f6301117bbc/src/components/Toolbar/DatePickerFilter.js Signed-off-by: Radoslaw Szwajkowski <[email protected]>
- Loading branch information
Showing
10 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
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,74 @@ | ||
import React, { FormEvent, useState } from 'react'; | ||
import { DateTime } from 'luxon'; | ||
|
||
import { DatePicker, InputGroup, ToolbarFilter } from '@patternfly/react-core'; | ||
|
||
import { FilterTypeProps } from './types'; | ||
|
||
/** | ||
* This Filter type enables selecting a single date (a day). | ||
* | ||
* **FilterTypeProps are interpreted as follows**:<br> | ||
* 1) selectedFilters - dates in YYYY-MM-DD format (ISO date format).<br> | ||
* 2) onFilterUpdate - accepts the list of dates.<br> | ||
* | ||
* [<img src="static/media/src/components-stories/assets/github-logo.svg"><i class="fi fi-brands-github"> | ||
* <font color="green">View component source on GitHub</font>](https://github.com/kubev2v/forklift-console-plugin/blob/main/packages/common/src/components/Filter/DateFilter.tsx) | ||
*/ | ||
export const DateFilter = ({ | ||
selectedFilters = [], | ||
onFilterUpdate, | ||
title, | ||
filterId, | ||
placeholderLabel, | ||
showFilter = true, | ||
}: FilterTypeProps) => { | ||
const validFilters = | ||
selectedFilters | ||
?.map((str) => DateTime.fromISO(str)) | ||
?.filter((dt: DateTime) => dt.isValid) | ||
?.map((dt: DateTime) => dt.toISODate()) ?? []; | ||
|
||
// internal state - stored as ISO date string (no time) | ||
const [date, setDate] = useState(DateTime.now().toISODate()); | ||
|
||
const clearSingleDate = (option) => { | ||
onFilterUpdate([...validFilters.filter((d) => d !== option)]); | ||
}; | ||
|
||
const onDateChange = (even: FormEvent<HTMLInputElement>, value: string) => { | ||
// require full format "YYYY-MM-DD" although partial date is also accepted | ||
// i.e. YYYY-MM gets parsed as YYYY-MM-01 and results in auto completing the date | ||
// unfortunately due to auto-complete user cannot delete the date char after char | ||
if (value?.length === 10 && DateTime.fromISO(value).isValid) { | ||
const targetDate = DateTime.fromISO(value).toISODate(); | ||
setDate(targetDate); | ||
onFilterUpdate([...validFilters.filter((d) => d !== targetDate), targetDate]); | ||
} | ||
}; | ||
|
||
return ( | ||
<ToolbarFilter | ||
key={filterId} | ||
chips={validFilters} | ||
deleteChip={(category, option) => clearSingleDate(option)} | ||
deleteChipGroup={() => onFilterUpdate([])} | ||
categoryName={title} | ||
showToolbarItem={showFilter} | ||
> | ||
<InputGroup> | ||
<DatePicker | ||
value={DateTime.fromISO(date).toISODate()} | ||
dateFormat={(date) => DateTime.fromJSDate(date).toISODate()} | ||
dateParse={(str) => DateTime.fromISO(str).toJSDate()} | ||
onChange={onDateChange} | ||
aria-label={title} | ||
placeholder={placeholderLabel} | ||
invalidFormatText={placeholderLabel} | ||
// default value ("parent") creates collision with sticky table header | ||
appendTo={document.body} | ||
/> | ||
</InputGroup> | ||
</ToolbarFilter> | ||
); | ||
}; |
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
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,12 @@ | ||
import { DateTime } from 'luxon'; | ||
|
||
/** | ||
* Converts a given ISO date string in a known format and timezone to a UTC ISO string. | ||
* | ||
* @param {string} isoDateString - The ISO date string in a known format and timezone. | ||
* @returns {string} The equivalent UTC ISO string if date is valid or undefined otherwise. | ||
*/ | ||
export function convertToUTC(isoDateString: string): string | undefined { | ||
const date = DateTime.fromISO(isoDateString); | ||
return date.isValid ? date.toUTC().toISO() : undefined; | ||
} |
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
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
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