-
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.
Merge pull request #499 from mlibrary/LIBSEARCH-801-part-4
[LIBSEARCH-801] Implement "clear active filters" designs for advanced search (Part 4)
- Loading branch information
Showing
6 changed files
with
133 additions
and
219 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,134 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { setAdvancedFilter } from '../../../advanced'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
const YearInput = ({ point = 'start', query, setQuery }) => { | ||
return ( | ||
<div> | ||
<label htmlFor={`date-range-${point}-date`}>{point.charAt(0).toUpperCase() + point.slice(1)} date</label> | ||
<input | ||
className='date-range-input-text' | ||
id={`date-range-${point}-date`} | ||
aria-describedby={`date-range-${point}-date-description`} | ||
type='text' | ||
value={query} | ||
onChange={setQuery} | ||
autoComplete='on' | ||
pattern='[0-9]{4}' | ||
/> | ||
<small id={`date-range-${point}-date-description`}>Please enter this format: YYYY</small> | ||
</div> | ||
); | ||
const options = ['before', 'after', 'between', 'in']; | ||
|
||
const extractYears = (dateString) => { | ||
return dateString.match(/\d+/gu) || ['']; | ||
}; | ||
|
||
YearInput.propTypes = { | ||
point: PropTypes.string, | ||
query: PropTypes.string, | ||
setQuery: PropTypes.func | ||
const extractRange = (dateString) => { | ||
const years = extractYears(dateString); | ||
if (!years[0]) { | ||
return 'before'; | ||
} | ||
if (years.length > 1) { | ||
return 'between'; | ||
} | ||
return ['before', 'after'].find((prefix) => { | ||
return dateString.startsWith(prefix); | ||
}) || 'in'; | ||
}; | ||
|
||
const dateRangeOptions = ['Before', 'After', 'Between', 'In']; | ||
const minValue = 1000; | ||
const maxValue = new Date().getFullYear(); | ||
const minValues = [minValue, minValue + 1]; | ||
const maxValues = [maxValue - 1, maxValue]; | ||
|
||
const DateRangeInput = ({ beginQuery, endQuery, selectedRangeOption, handleSelection }) => { | ||
const [beginQueryState, setBeginQuery] = useState(beginQuery || ''); | ||
const [endQueryState, setEndQuery] = useState(endQuery || ''); | ||
const [selectedRangeOptionState, setSelectedRangeOption] = useState(selectedRangeOption || 0); | ||
const DateRangeInput = ({ currentFilter = '', datastoreUid, filterGroupUid }) => { | ||
const dispatch = useDispatch(); | ||
const [range, setRange] = useState(extractRange(currentFilter)); | ||
const [years, setYears] = useState(extractYears(currentFilter)); | ||
const [min, setMin] = useState(minValues); | ||
const [max, setMax] = useState(maxValues); | ||
|
||
const handleStateChange = (beginQueryVal, endQueryVal, selectedRange) => { | ||
handleSelection({ | ||
beginDateQuery: beginQueryVal, | ||
endDateQuery: endQueryVal, | ||
selectedRange | ||
}); | ||
}; | ||
const updateFilter = useCallback((filterValue) => { | ||
dispatch(setAdvancedFilter({ datastoreUid, filterGroupUid, filterValue, onlyOneFilterValue: true })); | ||
}, [dispatch, datastoreUid, filterGroupUid]); | ||
|
||
useEffect(() => { | ||
const selectedRange = dateRangeOptions[selectedRangeOptionState]; | ||
handleStateChange(beginQueryState, endQueryState, selectedRange); | ||
}, [beginQueryState, endQueryState, selectedRangeOptionState]); | ||
|
||
const handleBeginQueryChange = (query) => { | ||
setBeginQuery(query); | ||
}; | ||
updateFilter(currentFilter); | ||
}, [currentFilter, updateFilter]); | ||
|
||
const handleEndQueryChange = (query) => { | ||
setEndQuery(query); | ||
}; | ||
useEffect(() => { | ||
let filterValue = ''; | ||
if (years.some(Boolean)) { | ||
if (range === 'between') { | ||
filterValue = years.filter(Number).join(' to '); | ||
} else { | ||
filterValue = ['before', 'after'].includes(range) ? `${range} ${years[0]}` : years[0]; | ||
} | ||
} | ||
updateFilter(filterValue); | ||
}, [range, years, updateFilter]); | ||
|
||
const rangeOption = dateRangeOptions[selectedRangeOptionState]; | ||
const handleYearChange = useCallback((index, value) => { | ||
if (range === 'between') { | ||
const newYears = [...years]; | ||
newYears[index] = value; | ||
const newMin = [...min]; | ||
const newMax = [...max]; | ||
if (index === 0) { | ||
newYears[1] = value ? newYears[1] : ''; | ||
newMin[1] = value && value < maxValues[1] - 1 ? Math.max(minValues[1], Number(value) + 1) : minValues[1]; | ||
} else { | ||
newMax[0] = value && value > minValues[0] + 1 ? Math.min(maxValues[0], Number(value) - 1) : maxValues[0]; | ||
} | ||
setYears(newYears); | ||
setMin(newMin); | ||
setMax(newMax); | ||
} else { | ||
setYears([value]); | ||
} | ||
}, [range, years, min, max]); | ||
|
||
return ( | ||
<div className='date-range-input'> | ||
<fieldset className='flex__responsive'> | ||
<legend className='visually-hidden'>Select the type of date range to search on</legend> | ||
{dateRangeOptions.map((option, index) => { | ||
{options.map((option, index) => { | ||
return ( | ||
<label key={index}> | ||
<input | ||
type='radio' | ||
name='date-range-input' | ||
value={option} | ||
checked={selectedRangeOptionState === index} | ||
checked={option === range} | ||
onChange={() => { | ||
return setSelectedRangeOption(index); | ||
return setRange(option); | ||
}} | ||
/> | ||
{option} | ||
{option.charAt(0).toUpperCase() + option.slice(1)} | ||
</label> | ||
); | ||
})} | ||
</fieldset> | ||
<div className='date-range-container'> | ||
{ | ||
rangeOption !== 'Before' && ( | ||
<YearInput | ||
query={beginQueryState} | ||
setQuery={(event) => { | ||
return handleBeginQueryChange(event.target.value); | ||
}} | ||
/> | ||
) | ||
} | ||
{ | ||
['Before', 'Between'].includes(rangeOption) && ( | ||
<YearInput | ||
query={endQueryState} | ||
setQuery={(event) => { | ||
return handleEndQueryChange(event.target.value); | ||
}} | ||
point='end' | ||
/> | ||
) | ||
} | ||
<div className='flex__responsive margin-top__xs'> | ||
{Array(range === 'between' ? 2 : 1).fill('').map((input, index) => { | ||
const label = (index === 1 || range === 'before') ? 'End' : 'Start'; | ||
const id = `date-range-${label.toLowerCase()}-date`; | ||
return ( | ||
<div key={index}> | ||
<label htmlFor={id}>{label} date</label> | ||
<input | ||
className='date-range-input-number' | ||
id={id} | ||
aria-describedby={`${id}-description`} | ||
type='number' | ||
value={years[index] || ''} | ||
disabled={index > 0 && !years[index - 1]} | ||
min={range === 'between' ? min[index] : minValue} | ||
max={range === 'between' ? max[index] : maxValue} | ||
onChange={(event) => { | ||
return handleYearChange(index, event.target.value); | ||
}} | ||
autoComplete='on' | ||
/> | ||
<small id={`${id}-description`}>Please enter this format: YYYY</small> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
DateRangeInput.propTypes = { | ||
beginQuery: PropTypes.string, | ||
endQuery: PropTypes.string, | ||
handleSelection: PropTypes.func, | ||
selectedRangeOption: PropTypes.number | ||
currentFilter: PropTypes.string, | ||
datastoreUid: PropTypes.string.isRequired, | ||
filterGroupUid: PropTypes.string.isRequired | ||
}; | ||
|
||
export default DateRangeInput; |
Oops, something went wrong.