-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-filters): add SimpleDateRange
- Loading branch information
1 parent
591a394
commit 4c6655e
Showing
10 changed files
with
216 additions
and
2 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
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
65 changes: 65 additions & 0 deletions
65
react-filters/src/components/fields/SimpleDateRangeField.js
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,65 @@ | ||
import React, { useCallback } from 'react'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
|
||
const messages = defineMessages({ | ||
min: { | ||
id: 'ReactFilters.fields.SimpleRangeField.gte', | ||
defaultMessage: 'Min', | ||
}, | ||
max: { | ||
id: 'ReactFilters.fields.SimpleRangeField.lte', | ||
defaultMessage: 'Max', | ||
}, | ||
}); | ||
|
||
// TODO minDate && maxDate from props | ||
|
||
function SimpleDateRangeField({ input }, _ref) { | ||
const intl = useIntl(); | ||
|
||
const { value, onChange } = input; | ||
|
||
const onInputChange = useCallback( | ||
(k, v) => { | ||
if (k === 'gte') { | ||
onChange({ | ||
...value, | ||
gte: v, | ||
}); | ||
} else { | ||
onChange({ | ||
...value, | ||
lte: v, | ||
}); | ||
} | ||
}, | ||
[onChange, value], | ||
); | ||
|
||
return ( | ||
<div className="row"> | ||
<div className="col-xs-6"> | ||
<input | ||
value={value?.gte || ''} | ||
type="date" | ||
className="form-control" | ||
aria-label={intl.formatMessage(messages.min)} | ||
onChange={(e) => onInputChange('gte', e.target.value)} | ||
max={value?.lte || ''} | ||
/> | ||
</div> | ||
<div className="col-xs-6"> | ||
<input | ||
value={value?.lte || ''} | ||
type="date" | ||
className="form-control" | ||
aria-label={intl.formatMessage(messages.max)} | ||
onChange={(e) => onInputChange('lte', e.target.value)} | ||
min={value?.gte || ''} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default React.forwardRef(SimpleDateRangeField); |
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
114 changes: 114 additions & 0 deletions
114
react-filters/src/components/filters/SimpleDateRangeFilter.js
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, { useState } from 'react'; | ||
import { Field } from 'react-final-form'; | ||
import { endOfDay, startOfDay, format } from 'date-fns'; | ||
import { getTimezoneOffset, utcToZonedTime } from 'date-fns-tz'; | ||
// import NumberRangeField from '../fields/NumberRangeField'; | ||
import Panel from '../Panel'; | ||
import Title from '../Title'; | ||
import SimpleDateRangeField from '../fields/SimpleDateRangeField'; | ||
import { Preview } from './DateRangeFilter'; | ||
|
||
const subscription = { value: true }; | ||
|
||
function formatDateValue(value, tz) { | ||
if (!value) return value; | ||
|
||
const currentTz = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
const tzDiff = getTimezoneOffset(tz, value) - getTimezoneOffset(currentTz, value); | ||
|
||
let date = new Date(value); | ||
|
||
if (tzDiff) { | ||
date = utcToZonedTime(date, tz); | ||
} | ||
|
||
return date; | ||
} | ||
|
||
// For display (store -> form) | ||
function formatValue(value) { | ||
if (!value) { | ||
return undefined; | ||
} | ||
|
||
const gte = formatDateValue(value.gte, value.tz); | ||
const lte = formatDateValue(value.lte, value.tz); | ||
|
||
return { | ||
gte: gte ? format(gte, 'yyyy-MM-dd') : null, | ||
lte: lte ? format(lte, 'yyyy-MM-dd') : null, | ||
}; | ||
} | ||
|
||
// For save (form -> store) | ||
function parseValue(value) { | ||
if (!value) { | ||
return value; | ||
} | ||
|
||
const gte = value.gte ? startOfDay(new Date(value.gte)).toISOString() : null; | ||
const lte = value.lte ? endOfDay(new Date(value.lte)).toISOString() : null; | ||
|
||
const result = {}; | ||
if (gte) result.gte = gte; | ||
if (lte) result.lte = lte; | ||
|
||
if (gte || lte) result.tz = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
||
return result; | ||
} | ||
|
||
const SimpleDateRangeFilter = React.forwardRef(function SimpleDateRangeFilter( | ||
{ name }, | ||
ref, | ||
) { | ||
return ( | ||
<Field | ||
ref={ref} | ||
name={name} | ||
subscription={subscription} | ||
format={formatValue} | ||
parse={parseValue} | ||
component={SimpleDateRangeField} | ||
/> | ||
); | ||
}); | ||
|
||
const Collapsable = React.forwardRef(function Collapsable( | ||
{ name, filter, component, disabled, staticRanges, inputRanges, ...rest }, | ||
ref, | ||
) { | ||
const [collapsed, setCollapsed] = useState(true); | ||
|
||
return ( | ||
<Panel | ||
header={( | ||
<Title | ||
name={name} | ||
filter={filter} | ||
component={Preview} | ||
disabled={disabled} | ||
/> | ||
)} | ||
collapsed={collapsed} | ||
setCollapsed={setCollapsed} | ||
> | ||
<SimpleDateRangeFilter | ||
ref={ref} | ||
name={name} | ||
filter={filter} | ||
component={component} | ||
disabled={disabled} | ||
collapsed={collapsed} | ||
{...rest} | ||
/> | ||
</Panel> | ||
); | ||
}); | ||
|
||
const exported = React.memo(SimpleDateRangeFilter); | ||
|
||
exported.Preview = Preview; | ||
exported.Collapsable = Collapsable; | ||
|
||
export default exported; |
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