-
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.
Merge branch 'main' into DATAP-1619-ESLint-v9-Upgrade
# Conflicts: # dist/ccdb5.js # dist/ccdb5.js.map # yarn.lock
- Loading branch information
Showing
13 changed files
with
1,544 additions
and
2,424 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 = []; | ||
|
||
const [inputText, setInputText] = useState(''); | ||
|
||
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 [dropdownOptions, setDropdownOptions] = useState(buckets); | ||
|
||
const handleClear = () => { | ||
ref.current.clear(); | ||
setInputText(''); | ||
}; | ||
|
||
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.