Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 5456ea9

Browse files
Add default selections to KeywordSearchWidget
1 parent 1b8245e commit 5456ea9

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
66
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [3.2.0] - 2023-14-02
9+
10+
## Added
11+
12+
- ability to pass default selections to `KeywordSearchWidget`
13+
814
## [3.1.0] - 2023-14-02
915

1016
## [3.0.3-0] (beta) - 2023-13-02

cypress/component/KeywordSearchWidget.cy.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,50 @@ describe('<KeywordSearchWidget />', () => {
315315
'aria-checked',
316316
'false'
317317
)
318+
319+
cy.log('Re-render with new props, providing a default selection.')
320+
cy.findByText('Land (cryosphere)').click()
321+
cy.findByText('Atmosphere (physical)').click()
322+
cy.findByText('Global').click()
323+
324+
rerender(
325+
<KeywordSearchWidget
326+
defaultSelections={{
327+
'Variable domain': ['Atmosphere (composition)', 'Land (cryosphere)']
328+
}}
329+
categories={[
330+
{
331+
category: 'Spatial coverage',
332+
groups: {
333+
Global: 27,
334+
Europe: 12
335+
}
336+
},
337+
{
338+
category: 'Temporal coverage',
339+
groups: {
340+
Past: 18
341+
}
342+
},
343+
{
344+
category: 'Variable domain',
345+
groups: {
346+
'Atmosphere (composition)': 12,
347+
'Atmosphere (physical)': 22,
348+
'Land (cryosphere)': 12
349+
}
350+
}
351+
]}
352+
/>
353+
)
354+
355+
cy.findByLabelText('Atmosphere (physical)').should(
356+
'have.attr',
357+
'aria-checked',
358+
'false'
359+
)
360+
361+
cy.findByLabelText('Global').should('have.attr', 'aria-checked', 'true')
318362
})
319363
})
320364
})

src/widgets/KeywordSearchWidget.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export type KeywordCategory = {
1111
export interface KeywordSearchWidgetProps {
1212
categories: KeywordCategory[]
1313

14+
/**
15+
* Optional default selections. This is used to control checkbox selections.
16+
*/
17+
defaultSelections?: Selections
18+
1419
/**
1520
* Keyword change handlers. Invoked when the user selects/deselects a keyword.
1621
*/
@@ -40,6 +45,7 @@ type Selections = Record<string, string[]>
4045
*/
4146
const KeywordSearchWidget = ({
4247
categories,
48+
defaultSelections,
4349
onKeywordSelection,
4450
keywordQueryParam = 'kw',
4551
keywordQueryParamTransformer,
@@ -60,8 +66,15 @@ const KeywordSearchWidget = ({
6066
(nextState, keywordCategory) => {
6167
nextState[keywordCategory.category] = [
6268
...intersection(
63-
new Set(Object.keys(keywordCategory.groups)),
64-
new Set(selections[keywordCategory.category] || [])
69+
intersection(
70+
new Set(Object.keys(keywordCategory.groups)),
71+
new Set(selections[keywordCategory.category] || [])
72+
),
73+
new Set(
74+
(defaultSelections &&
75+
defaultSelections[keywordCategory.category]) ||
76+
selections[keywordCategory.category]
77+
)
6578
)
6679
]
6780

0 commit comments

Comments
 (0)