Skip to content

Commit

Permalink
fix: Fix date filter not including max day
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Oct 6, 2019
1 parent 805745f commit dd52013
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/FilterPanel/DateFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Props = {
filter?: Filter | null,
min: string,
max: string,
type?: 'date' | 'datetime',
onChangeFilter: (filter: Array<any> | null) => void
}

Expand All @@ -33,6 +34,7 @@ const DateFilter = ({
filter,
min,
max,
type = 'datetime',
onChangeFilter
}: Props) => {
const cx = useStyles()
Expand All @@ -46,8 +48,8 @@ const DateFilter = ({
const handleChange = (minOrMax: 'min' | 'max') => value => {
const newFilter =
minOrMax === 'min'
? compileFilter(fieldKey, value, filterMax)
: compileFilter(fieldKey, filterMin, value)
? compileFilter(fieldKey, createFilterValue(value, 'min'), filterMax)
: compileFilter(fieldKey, filterMin, createFilterValue(value, 'max'))
onChangeFilter(newFilter)
}

Expand Down Expand Up @@ -107,3 +109,21 @@ const useStyles = makeStyles(theme => ({
}
}
}))

const shortDateRegExp = /^(\d{4})-(\d{2})-(\d{2})$/

function createFilterValue(value, minOrMax) {
const match = value.match(shortDateRegExp)
if (!match) return value
return minOrMax === 'min'
? new Date(+match[1], +match[2] - 1, +match[3]).toISOString()
: new Date(
+match[1],
+match[2] - 1,
+match[3],
23,
59,
59,
999
).toISOString()
}

0 comments on commit dd52013

Please sign in to comment.