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

Commit fb43bf8

Browse files
Add ability to override the query params transformer
1 parent 03109ec commit fb43bf8

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
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.0.2-0] (beta) - 2023-13-02
9+
10+
## Added
11+
12+
- ability to override query params transformer to `KeywordSearchWidget` via `keywordQueryParamTransformer`
13+
814
## [3.0.1-0] (beta) - 2023-13-02
915

1016
## Added

cypress/component/KeywordSearchWidget.cy.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,28 @@ describe('<KeywordSearchWidget />', () => {
8787
cy.get('@onKeywordSelection')
8888
.its('firstCall.args.0')
8989
.then(searchParams => {
90-
expect(searchParams.toString()).to.equal('kw=Spatial+coverage%3AGlobal')
90+
expect(searchParams.getAll('kw')).to.deep.equal([
91+
'Spatial%20coverage%3A%20Global'
92+
])
9193
})
9294

9395
cy.get('@onKeywordSelection')
9496
.its('secondCall.args.0')
9597
.then(searchParams => {
96-
expect(searchParams.toString()).to.equal(
97-
'kw=Spatial+coverage%3AGlobal&kw=Variable+domain%3AAtmosphere+%28composition%29'
98-
)
98+
expect(searchParams.getAll('kw')).to.deep.equal([
99+
'Spatial%20coverage%3A%20Global',
100+
'Variable%20domain%3A%20Atmosphere%20(composition)'
101+
])
99102
})
100103

101104
cy.get('@onKeywordSelection')
102105
.its('thirdCall.args.0')
103106
.then(searchParams => {
104-
expect(searchParams.toString()).to.equal(
105-
'kw=Spatial+coverage%3AGlobal&kw=Variable+domain%3AAtmosphere+%28composition%29&kw=Variable+domain%3ALand+%28cryosphere%29'
106-
)
107+
expect(searchParams.getAll('kw')).to.deep.equal([
108+
'Spatial%20coverage%3A%20Global',
109+
'Variable%20domain%3A%20Atmosphere%20(composition)',
110+
'Variable%20domain%3A%20Land%20(cryosphere)'
111+
])
107112
})
108113
})
109114

@@ -210,9 +215,11 @@ describe('<KeywordSearchWidget />', () => {
210215
cy.get('@onKeywordSelection')
211216
.its('thirdCall.args.0')
212217
.then(searchParams => {
213-
expect(searchParams.toString()).to.equal(
214-
'kw=Spatial+coverage%3AGlobal&kw=Variable+domain%3AAtmosphere+%28composition%29&kw=Variable+domain%3ALand+%28cryosphere%29'
215-
)
218+
expect(searchParams.getAll('kw')).to.deep.equal([
219+
'Spatial%20coverage%3A%20Global',
220+
'Variable%20domain%3A%20Atmosphere%20(composition)',
221+
'Variable%20domain%3A%20Land%20(cryosphere)'
222+
])
216223
})
217224

218225
cy.findByLabelText(/Europe/i).should('have.attr', 'aria-checked', 'false')

src/widgets/KeywordSearchWidget.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export interface KeywordSearchWidgetProps {
2020
* The key used as the query parameter for the keyword search. Defaults to "kw".
2121
*/
2222
keywordQueryParam?: string
23+
24+
/**
25+
* A custom transformer for the query parameter. Defaults to using encodeURIComponent.
26+
*/
27+
keywordQueryParamTransformer?: (category: string, keyword: string) => string
2328
}
2429

2530
type Selections = Record<string, string[]>
@@ -31,7 +36,8 @@ type Selections = Record<string, string[]>
3136
const KeywordSearchWidget = ({
3237
categories,
3338
onKeywordSelection,
34-
keywordQueryParam = 'kw'
39+
keywordQueryParam = 'kw',
40+
keywordQueryParamTransformer
3541
}: KeywordSearchWidgetProps) => {
3642
/**
3743
* Keep track of the selected keywords. This is used to preserve selections when accordions are closed and subsequently opened.
@@ -69,7 +75,12 @@ const KeywordSearchWidget = ({
6975
for (const selection in selections) {
7076
if (selections[selection].length) {
7177
for (const keyword of selections[selection]) {
72-
searchParams.append(keywordQueryParam, `${selection}:${keyword}`)
78+
searchParams.append(
79+
keywordQueryParam,
80+
(keywordQueryParamTransformer &&
81+
keywordQueryParamTransformer(selection, keyword)) ||
82+
encodeURIComponent(`${selection}: ${keyword}`)
83+
)
7384
}
7485
}
7586
}

0 commit comments

Comments
 (0)