Skip to content

Commit f6316c8

Browse files
authored
Refactor search context (#384)
1 parent 58d5417 commit f6316c8

19 files changed

+364
-452
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99
### Added
1010
- Add `<CustomRendering/>` to override presentation ([#382](https://github.com/cucumber/react-components/pull/382))
11+
- Add `<ControlledSearchProvider/>`, `<InMemorySearchProvider/>` and `<UrlSearchProvider/>` to provide search state ([#384](https://github.com/cucumber/react-components/pull/384))
1112

1213
### Fixed
1314
- Make keyword spacing look right ([#376](https://github.com/cucumber/react-components/pull/376))
@@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2324
### Removed
2425
- BREAKING CHANGE: Remove `EnvelopesQuery` and its React context ([#374](https://github.com/cucumber/react-components/pull/374))
2526
- BREAKING CHANGE: Remove defunct `<CucumberReact/>` component ([#382](https://github.com/cucumber/react-components/pull/382))
27+
- BREAKING CHANGE: Remove `SearchQueryContext`, `<SearchWrapper/>` and related defunct symbols ([#384](https://github.com/cucumber/react-components/pull/384))
2628

2729
## [22.4.1] - 2025-03-30
2830
### Fixed

src/SearchContext.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TestStepResultStatus as Status } from '@cucumber/messages'
2+
import React from 'react'
3+
4+
export interface SearchState {
5+
readonly query: string
6+
readonly hideStatuses: readonly Status[]
7+
}
8+
9+
export interface SearchContextValue extends SearchState {
10+
update: (changes: Partial<SearchState>) => void
11+
}
12+
13+
export default React.createContext<SearchContextValue>({
14+
query: '',
15+
hideStatuses: [],
16+
update: () => {},
17+
})

src/SearchQueryContext.spec.ts

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/SearchQueryContext.ts

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { FC, PropsWithChildren, useMemo } from 'react'
2+
3+
import SearchQueryContext, { SearchContextValue, SearchState } from '../../SearchContext.js'
4+
5+
interface Props {
6+
value: SearchState
7+
onChange: (newValue: SearchState) => void
8+
}
9+
10+
export const ControlledSearchProvider: FC<PropsWithChildren<Props>> = ({
11+
value,
12+
onChange,
13+
children,
14+
}) => {
15+
const contextValue: SearchContextValue = useMemo(() => {
16+
return {
17+
query: value.query,
18+
hideStatuses: value.hideStatuses,
19+
update: (newValues: Partial<SearchState>) => {
20+
onChange({ ...value, ...newValues })
21+
},
22+
}
23+
}, [value, onChange])
24+
return <SearchQueryContext.Provider value={contextValue}>{children}</SearchQueryContext.Provider>
25+
}

src/components/app/FilteredResults.spec.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@ import { Envelope } from '@cucumber/messages'
22
import { render, waitFor } from '@testing-library/react'
33
import { userEvent } from '@testing-library/user-event'
44
import { expect } from 'chai'
5-
import React, { VoidFunctionComponent } from 'react'
5+
import React, { FC } from 'react'
66

77
import attachments from '../../../acceptance/attachments/attachments.feature.js'
88
import examplesTables from '../../../acceptance/examples-tables/examples-tables.feature.js'
99
import minimal from '../../../acceptance/minimal/minimal.feature.js'
1010
import targetedRun from '../../../samples/targeted-run.js'
11-
import SearchQueryContext, { useSearchQueryCtx } from '../../SearchQueryContext.js'
1211
import { EnvelopesWrapper } from './EnvelopesWrapper.js'
1312
import { FilteredResults } from './FilteredResults.js'
13+
import { InMemorySearchProvider } from './InMemorySearchProvider.js'
1414

1515
describe('FilteredResults', () => {
16-
const TestableFilteredResults: VoidFunctionComponent<{ envelopes: Envelope[] }> = ({
17-
envelopes,
18-
}) => {
16+
const TestableFilteredResults: FC<{ envelopes: Envelope[] }> = ({ envelopes }) => {
1917
return (
2018
<EnvelopesWrapper envelopes={envelopes}>
21-
<SearchQueryContext.Provider value={useSearchQueryCtx({})}>
19+
<InMemorySearchProvider>
2220
<FilteredResults />
23-
</SearchQueryContext.Provider>
21+
</InMemorySearchProvider>
2422
</EnvelopesWrapper>
2523
)
2624
}

src/components/app/FilteredResults.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import testData from '../../../acceptance/examples-tables/examples-tables.featur
66
import targetedRun from '../../../samples/targeted-run.js'
77
import { EnvelopesWrapper } from './EnvelopesWrapper.js'
88
import { FilteredResults } from './FilteredResults.js'
9-
import { SearchWrapper } from './SearchWrapper.js'
9+
import { InMemorySearchProvider } from './InMemorySearchProvider.js'
1010

1111
export default {
1212
title: 'App/FilteredResults',
@@ -19,9 +19,9 @@ type TemplateArgs = {
1919
const Template: Story<TemplateArgs> = ({ envelopes }) => {
2020
return (
2121
<EnvelopesWrapper envelopes={envelopes}>
22-
<SearchWrapper>
22+
<InMemorySearchProvider>
2323
<FilteredResults />
24-
</SearchWrapper>
24+
</InMemorySearchProvider>
2525
</EnvelopesWrapper>
2626
)
2727
}

src/components/app/HighLight.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import highlightWords from 'highlight-words'
33
import React from 'react'
44
import ReactMarkdown from 'react-markdown'
55

6-
import SearchQueryContext from '../../SearchQueryContext.js'
6+
import { useSearch } from '../../hooks/index.js'
77
import rehypePlugins from './rehypePlugins.js'
88
import remarkPlugins from './remarkPlugins.js'
99

@@ -30,7 +30,7 @@ export const HighLight: React.FunctionComponent<IProps> = ({
3030
markdown = false,
3131
className = '',
3232
}) => {
33-
const searchQueryContext = React.useContext(SearchQueryContext)
33+
const searchQueryContext = useSearch()
3434
const query = allQueryWords(
3535
searchQueryContext.query ? searchQueryContext.query.split(' ') : []
3636
).join(' ')

0 commit comments

Comments
 (0)