|
1 | 1 | import React, { useState, useContext } from 'react' |
2 | 2 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
3 | | -import { faCogs } from '@fortawesome/free-solid-svg-icons' |
4 | | -import { Popover, OverlayTrigger } from 'react-bootstrap' |
| 3 | +import { faCogs, faTimesCircle } from '@fortawesome/free-solid-svg-icons' |
5 | 4 | import { StudySearchContext } from '~/providers/StudySearchProvider' |
6 | 5 |
|
7 | 6 | import OptionsControl from '~/components/search/controls/OptionsControl' |
| 7 | +import useCloseableModal from '~/hooks/closeableModal' |
8 | 8 |
|
| 9 | +/** list of configured search option entries */ |
| 10 | +export const configuredOptions = [ |
| 11 | + { searchProp: 'external', value: 'hca', label: 'Include HCA results' }, |
| 12 | + { searchProp: 'data_types', value: 'raw_counts', label: 'Has raw counts', multiple: true }, |
| 13 | + { searchProp: 'data_types', value: 'diff_exp', label: 'Has differential expression', multiple: true }, |
| 14 | + { searchProp: 'data_types', value: 'spatial', label: 'Has spatial data', multiple: true } |
| 15 | +] |
| 16 | + |
| 17 | +/** Search options button for filtering results by data types/sources */ |
9 | 18 | export default function OptionsButton() { |
10 | 19 | const searchContext = useContext(StudySearchContext) |
11 | 20 | const [showOptions, setShowOptions] = useState(false) |
12 | | - const configuredOptions = [ |
13 | | - { searchProp: 'external', value: 'hca', label: 'Include HCA results' }, |
14 | | - { searchProp: 'data_types', value: 'raw_counts', label: 'Has raw counts', multiple: true }, |
15 | | - { searchProp: 'data_types', value: 'diff_exp', label: 'Has differential expression', multiple: true }, |
16 | | - { searchProp: 'data_types', value: 'spatial', label: 'Has spatial data', multiple: true } |
17 | | - ] |
18 | | - |
19 | | - const optionsPopover = <Popover data-analytics-name='search-options-menu' id='search-options-menu'> |
20 | | - <ul className="facet-filter-list"> |
| 21 | + |
| 22 | + /** determine if any options have been selected */ |
| 23 | + function searchOptionSelected() { |
| 24 | + const opts = [] |
| 25 | + configuredOptions.map(option => { |
| 26 | + const opt = option.searchProp |
| 27 | + if (searchContext.params[opt] && searchContext.params[opt].length > 0) { |
| 28 | + opts.push(opt) |
| 29 | + } |
| 30 | + }) |
| 31 | + return opts.length > 0 |
| 32 | + } |
| 33 | + |
| 34 | + /** clear all selected options */ |
| 35 | + function clearAllOptions() { |
| 36 | + const existingParams = searchContext.params |
| 37 | + configuredOptions.map(option => {delete existingParams[option.searchProp]}) |
| 38 | + searchContext.updateSearch(existingParams) |
| 39 | + setShowOptions(false) |
| 40 | + } |
| 41 | + |
| 42 | + const { node, clearNode, handleButtonClick } = useCloseableModal(showOptions, setShowOptions) |
| 43 | + |
| 44 | + const optionsMenu = <div data-analytics-name='search-options-menu' id='search-options-menu'> |
| 45 | + <ul> |
21 | 46 | { |
22 | 47 | configuredOptions.map((option, index) => { |
23 | | - return <OptionsControl |
24 | | - key={`${option.searchProp}-${index}`} |
25 | | - searchContext={searchContext} |
26 | | - searchProp={option.searchProp} |
27 | | - value={option.value} |
28 | | - label={option.label} |
29 | | - multiple={option.multiple} |
30 | | - /> |
| 48 | + return <OptionsControl |
| 49 | + key={`${option.searchProp}-${index}`} |
| 50 | + searchContext={searchContext} |
| 51 | + searchProp={option.searchProp} |
| 52 | + value={option.value} |
| 53 | + label={option.label} |
| 54 | + multiple={option.multiple} |
| 55 | + /> |
31 | 56 | }) |
32 | 57 | } |
33 | 58 | </ul> |
34 | | - </Popover> |
| 59 | + { showOptions && searchOptionSelected() && |
| 60 | + <a className='pull-right' onClick={clearAllOptions}> |
| 61 | + Clear |
| 62 | + <span |
| 63 | + ref={clearNode} |
| 64 | + data-testid='clear-search-options' |
| 65 | + onClick={clearAllOptions} |
| 66 | + aria-label='Clear options' |
| 67 | + > |
| 68 | + <FontAwesomeIcon icon={faTimesCircle}/> |
| 69 | + </span> |
| 70 | + </a> |
| 71 | + } |
| 72 | + </div> |
35 | 73 |
|
36 | 74 | return ( |
37 | | - <OverlayTrigger trigger={['click']} placement='bottom' animation={false} overlay={optionsPopover}> |
38 | | - <span id="search-options-button" data-testid="search-options-button" |
39 | | - className={`facet ${showOptions ? 'active' : ''}`}> |
40 | | - <a onClick={() => setShowOptions(!showOptions)}> |
| 75 | + <span |
| 76 | + ref={node} |
| 77 | + id="search-options-button" |
| 78 | + data-testid="search-options-button" |
| 79 | + className={`facet ${showOptions ? 'active' : ''}`} |
| 80 | + > |
| 81 | + <a onClick={handleButtonClick}> |
41 | 82 | <FontAwesomeIcon className="icon-left" icon={faCogs}/>Options |
42 | 83 | </a> |
| 84 | + { showOptions && optionsMenu } |
43 | 85 | </span> |
44 | | - </OverlayTrigger> |
45 | 86 | ) |
46 | 87 | } |
0 commit comments