-
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.
Key points: 1. ISO date format used (YYYY-MM-DD) regardless of the locale 2. date comparison is not time zone aware (string comparison) 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
7 changed files
with
156 additions
and
1 deletion.
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
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