-
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.
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
- Loading branch information
Showing
7 changed files
with
155 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,73 @@ | ||
import React, { 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) => { | ||
console.warn('clearSingle ', option); | ||
onFilterUpdate([...validFilters.filter((d) => d !== option)]); | ||
}; | ||
|
||
const onDateChange = (inputDate: string, date: Date) => { | ||
// TODO runtime types different then declared | ||
// date is parsed string | ||
if (DateTime.fromISO(date).isValid) { | ||
const targetDate = DateTime.fromISO(date).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={'Date'} | ||
buttonAriaLabel={'Toggle date picker'} | ||
placeholder={placeholderLabel} | ||
invalidFormatText={'Invalid date format. The correct format is AA-AA-AA'} | ||
/> | ||
</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