Skip to content

Commit 1ea0d6b

Browse files
authored
Merge pull request #2308 from broadinstitute/jb-data-type-search-checkbox
Fix default state for options checkbox, text select behavior (SCP-6051)
2 parents 061829d + 70c95c3 commit 1ea0d6b

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

app/javascript/components/search/controls/OptionsControl.jsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@ import React, { useState } from 'react'
22

33
/** checkbox control for adding optional parameters to search query */
44
export default function OptionsControl({ searchContext, searchProp, value, label, multiple = false }) {
5-
const defaultChecked = searchContext.params[searchProp] === value
5+
const defaultChecked = isDefaultChecked()
66
const [isChecked, setIsChecked] = useState(defaultChecked)
77

8+
/** return existing url query params for this option */
9+
function getExistingOpts() {
10+
return searchContext.params[searchProp]?.split(',').filter(o => o !== '') || []
11+
}
12+
13+
/** set the default state for this option checkbox */
14+
function isDefaultChecked() {
15+
if (multiple) {
16+
return getExistingOpts().includes(value)
17+
} else {
18+
return searchContext.params[searchProp] === value
19+
}
20+
}
821

922
/** toggle state of checkbox */
1023
function toggleCheckbox(checked) {
1124
setIsChecked(checked)
1225
if (multiple) {
13-
const existingOpts = searchContext.params[searchProp]?.split(',').filter(o => o !== '') || []
26+
const existingOpts = getExistingOpts()
1427
const newOpts = checked ? existingOpts.concat(value) : existingOpts.filter(v => v !== value)
1528
searchContext.updateSearch({ [searchProp] : newOpts.join(',') })
1629
} else {
@@ -21,8 +34,11 @@ export default function OptionsControl({ searchContext, searchProp, value, label
2134
return (
2235
<li id={`options-control-${searchProp}`} key={`options-control-${searchProp}`}>
2336
<label>
24-
<input type="checkbox" checked={isChecked} onChange={() => {toggleCheckbox(!isChecked)}}/>
25-
<span onClick={() => {toggleCheckbox(!isChecked)}} >{ label }</span>
37+
<input data-testid={`options-checkbox-${searchProp}-${value}`}
38+
type="checkbox"
39+
checked={isChecked}
40+
onChange={() => {toggleCheckbox(!isChecked)}}/>
41+
<span onClick={() => {toggleCheckbox(!isChecked)}} >{ label }</span>
2642
</label>
2743
</li>
2844
)

app/javascript/components/search/results/SearchQueryDisplay.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export const ClearAllButton = () => {
7878
const emptySearchParams = {
7979
terms: '',
8080
genes: '',
81+
external: '',
82+
data_types: '',
8183
facets: emptyFilters
8284
}
8385
selectionContext.updateSelection(emptySearchParams, true)

app/javascript/styles/_search.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ nav.search-links, #search-panel {
6868
width: 100%;
6969
box-sizing: border-box;
7070
display: inline-block;
71+
user-select: none;
7172
}
7273
&.active > a, &.selected > a {
7374
background-color: $action-color;

app/models/hca_azul_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HcaAzulClient
1515
MANIFEST_FORMATS = %w[compact full terra.bdbag terra.pfb curl].freeze
1616

1717
# maximum number of results to return
18-
MAX_RESULTS = 100
18+
MAX_RESULTS = 80
1919

2020
# maximum length of query string (in characters) for requests
2121
MAX_QUERY_LENGTH = 8192

test/js/search/options-control.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import React from 'react'
2-
import { render, fireEvent } from '@testing-library/react'
2+
import { render, fireEvent, screen } from '@testing-library/react'
33
import '@testing-library/jest-dom/extend-expect'
44

55
import OptionsControl from '~/components/search/controls/OptionsControl'
66

77
describe('OptionsControl component', () => {
88
it('renders with default checked state', () => {
99
const searchContext = {
10-
params: { 'external': 'hca' },
10+
params: { 'external': 'hca', 'data_types': 'raw_counts,spatial' },
1111
updateSearch: jest.fn()
1212
}
1313
const { getByText, getByRole } = render(
14-
<OptionsControl searchContext={searchContext} searchProp='external' value='hca' label='Include HCA results' />
14+
<>
15+
<OptionsControl searchContext={searchContext} searchProp='external' value='hca' label='Include HCA results' />
16+
<OptionsControl
17+
searchContext={searchContext} searchProp='data_types' value='raw_counts'
18+
label='Has raw counts' multiple={true}
19+
/>
20+
<OptionsControl
21+
searchContext={searchContext} searchProp='data_types' value='spatial' label='Has spatial' multiple={true}
22+
/>
23+
</>
1524
)
1625

1726
expect(getByText('Include HCA results')).toBeInTheDocument()
18-
expect(getByRole('checkbox')).toBeChecked()
27+
expect(getByText('Has raw counts')).toBeInTheDocument()
28+
expect(getByText('Has spatial')).toBeInTheDocument()
29+
expect(screen.getByTestId('options-checkbox-external-hca')).toBeChecked()
30+
expect(screen.getByTestId('options-checkbox-data_types-raw_counts')).toBeChecked()
31+
expect(screen.getByTestId('options-checkbox-data_types-spatial')).toBeChecked()
1932
})
2033

2134
it('toggles checkbox state on click', () => {

0 commit comments

Comments
 (0)