-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAP-1621 - adding suggest typeahead and sub-agg search for product …
…and issue (#569) * adding suggest typeahead and sub-agg search for product and issue update test coverage * adding styles * incorporated current Typeahead component (with HighlightingOption) * Reverted to custom typeahead * update dist * Formatting updates, to match expected styling * update dist fix cypress test adjust and make maxResults 5 update dist * took out stringify call for custom Typeahead component * Fixed highlighting option for parent item * cleaned up code * update dist * adding parent to options, updating match to return original casing from string * fixing test * fix test * update dist * restore missing close button * update dist --------- Co-authored-by: Chanel Henley <chanel.henley@cfpb.gov>
- Loading branch information
1 parent
3281a15
commit 2c89b14
Showing
13 changed files
with
2,064 additions
and
2,210 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,150 @@ | ||
import '../../Typeahead/Typeahead.scss'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { Typeahead } from 'react-bootstrap-typeahead'; | ||
import { filterAdded } from '../../../actions'; | ||
import PropTypes from 'prop-types'; | ||
import { useGetAggregations } from '../../../api/hooks/useGetAggregations'; | ||
import { SLUG_SEPARATOR } from '../../../constants'; | ||
import { normalize } from '../../../utils'; | ||
import { ClearButton } from '../../Typeahead/ClearButton/ClearButton'; | ||
import HighlightingOption from '../../Typeahead/HighlightingOption/HighlightingOption'; | ||
import getIcon from '../../Common/Icon/iconMap'; | ||
|
||
export const FilterSearch = ({ fieldName }) => { | ||
const ref = useRef(); | ||
const dispatch = useDispatch(); | ||
|
||
const fieldNameNew = fieldName.replace(/_/g, ' '); | ||
const { data } = useGetAggregations(); | ||
|
||
const aggResults = data[fieldName] || []; | ||
const subaggName = `sub_${fieldName}.raw`.toLowerCase(); | ||
const buckets = []; | ||
|
||
aggResults.forEach((option) => { | ||
if (buckets.findIndex((item) => item.key === option.key) === -1) { | ||
const parentAgg = { ...option }; | ||
parentAgg.isParent = true; | ||
parentAgg.label = option.key; | ||
parentAgg.normalized = normalize(option.key); | ||
parentAgg.position = 0; | ||
parentAgg.top = { | ||
key: option.key, | ||
label: option.key, | ||
normalized: normalize(option.key), | ||
position: 0, | ||
}; | ||
buckets.push(parentAgg); | ||
} | ||
|
||
if (option[subaggName] && option[subaggName].buckets) { | ||
option[subaggName].buckets.forEach((bucket) => { | ||
const item = { | ||
key: option.key + SLUG_SEPARATOR + bucket.key, | ||
label: bucket.key, | ||
normalized: normalize(bucket.key), | ||
position: 0, | ||
top: { | ||
key: option.key, | ||
label: option.key, | ||
normalized: normalize(option.key), | ||
position: 0, | ||
}, | ||
}; | ||
buckets.push(item); | ||
}); | ||
} | ||
}); | ||
|
||
const handleClear = () => { | ||
ref.current.clear(); | ||
setInputText(''); | ||
}; | ||
|
||
const [inputText, setInputText] = useState(''); | ||
|
||
const [dropdownOptions, setDropdownOptions] = useState(buckets); | ||
|
||
const handleInputChange = (value) => { | ||
setInputText(value); | ||
const rawValue = normalize(value); | ||
|
||
if (!rawValue) { | ||
setDropdownOptions(buckets); | ||
} else { | ||
const options = buckets.map((opt) => { | ||
return { | ||
...opt, | ||
position: opt.normalized.indexOf(rawValue), | ||
value, | ||
top: { | ||
...opt.top, | ||
position: opt.top.normalized.indexOf(rawValue), | ||
value, | ||
}, | ||
}; | ||
}); | ||
|
||
setDropdownOptions(options); | ||
} | ||
}; | ||
|
||
const handleSelections = (selected) => { | ||
dispatch(filterAdded(fieldName, selected[0].key)); | ||
handleClear(); | ||
}; | ||
|
||
// give the input focus when the component renders the first time | ||
useEffect(() => { | ||
ref.current.focus(); | ||
}, [ref]); | ||
|
||
return ( | ||
<div className="typeahead"> | ||
<div className="o-search-input"> | ||
<div className="o-search-input__input"> | ||
<label | ||
aria-label={'Search ' + fieldName} | ||
className="o-search-input__input-label" | ||
htmlFor={'filter-search' + fieldName} | ||
> | ||
{getIcon('search')} | ||
</label> | ||
<Typeahead | ||
id={'filter-search' + fieldName} | ||
maxResults={5} | ||
minLength={2} | ||
className="typeahead-selector" | ||
filterBy={['key']} | ||
onChange={(selected) => handleSelections(selected)} | ||
onInputChange={(text) => handleInputChange(text)} | ||
placeholder={'Enter name of ' + fieldNameNew} | ||
labelKey="key" | ||
options={dropdownOptions} | ||
ref={ref} | ||
inputProps={{ | ||
'aria-label': `${fieldNameNew} Filter Menu Input`, | ||
className: 'a-text-input a-text-input--full', | ||
}} | ||
renderMenuItemChildren={(option) => ( | ||
<li className="typeahead-option typeahead-option--multi body-copy"> | ||
<HighlightingOption {...option.top} /> | ||
{!option.isParent ? ( | ||
<div className="typeahead-option__sub"> | ||
{option.value ? <HighlightingOption {...option} /> : null} | ||
</div> | ||
) : null} | ||
</li> | ||
)} | ||
/> | ||
{!!inputText && <ClearButton onClear={handleClear} />} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
FilterSearch.propTypes = { | ||
fieldName: PropTypes.string.isRequired, | ||
}; |
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
Oops, something went wrong.