Skip to content

Commit 07e307a

Browse files
committed
feat(FilesTableSearch): add to searchText to criteria
1 parent 20646ea commit 07e307a

6 files changed

Lines changed: 120 additions & 7 deletions

File tree

src/files/domain/models/FileCriteria.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export class FileCriteria {
55
public readonly sortBy: FileSortByOption = FileSortByOption.NAME_AZ,
66
public readonly filterByType?: FileType,
77
public readonly filterByAccess?: FileAccessOption,
8-
public readonly filterByTag?: FileTag
8+
public readonly filterByTag?: FileTag,
9+
public readonly searchText?: string
910
) {}
1011

1112
withSortBy(sortBy: FileSortByOption): FileCriteria {
@@ -27,6 +28,16 @@ export class FileCriteria {
2728

2829
return new FileCriteria(this.sortBy, this.filterByType, this.filterByAccess, newFilterByTag)
2930
}
31+
32+
withSearchText(searchText: string | undefined): FileCriteria {
33+
return new FileCriteria(
34+
this.sortBy,
35+
this.filterByType,
36+
this.filterByAccess,
37+
this.filterByTag,
38+
searchText
39+
)
40+
}
3041
}
3142

3243
export enum FileSortByOption {

src/sections/dataset/dataset-files/file-criteria-form/FileCriteriaForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function FileCriteriaForm({
2424
<Form>
2525
<Row>
2626
<Col md={5}>
27-
<FileCriteriaSearchText />
27+
<FileCriteriaSearchText criteria={criteria} onCriteriaChange={onCriteriaChange} />
2828
</Col>
2929
</Row>
3030
<Row className={styles['criteria-section']}>

src/sections/dataset/dataset-files/file-criteria-form/FileCriteriaSearchText.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
import { Button, Form } from 'dataverse-design-system'
22
import { Search } from 'react-bootstrap-icons'
3+
import { FileCriteria } from '../../../../files/domain/models/FileCriteria'
4+
import { ChangeEvent, useState, KeyboardEvent } from 'react'
5+
6+
interface FileCriteriaSearchTextProps {
7+
criteria: FileCriteria
8+
onCriteriaChange: (criteria: FileCriteria) => void
9+
}
10+
export function FileCriteriaSearchText({
11+
criteria,
12+
onCriteriaChange
13+
}: FileCriteriaSearchTextProps) {
14+
const [searchText, setSearchText] = useState<string>(criteria.searchText ?? '')
15+
const handleSearchTextChange = (event: ChangeEvent<HTMLInputElement>) => {
16+
const updatedSearchText = event.target.value
17+
setSearchText(updatedSearchText)
18+
}
19+
const handleSubmitSearch = () => {
20+
onCriteriaChange(criteria.withSearchText(searchText))
21+
}
22+
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
23+
if (event.key === 'Enter') {
24+
event.preventDefault()
25+
handleSubmitSearch()
26+
}
27+
}
328

4-
export function FileCriteriaSearchText() {
529
return (
630
<Form.Group controlId="search-files">
731
<Form.InputGroup>
832
<Form.Group.Input
933
type="text"
1034
placeholder="Search this dataset..."
1135
aria-label="Search this dataset"
36+
defaultValue={searchText}
37+
onChange={handleSearchTextChange}
38+
onKeyDown={handleKeyDown}
39+
/>
40+
<Button
41+
variant="secondary"
42+
icon={<Search />}
43+
aria-label="Submit search"
44+
onClick={handleSubmitSearch}
1245
/>
13-
<Button variant="secondary" icon={<Search />} aria-label="Submit search" />
1446
</Form.InputGroup>
1547
</Form.Group>
1648
)

tests/component/sections/dataset/dataset-files/DatasetFiles.spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ describe('DatasetFiles', () => {
115115
cy.findByRole('button', { name: 'Filter Type: All' }).should('exist')
116116
})
117117

118+
it('calls the useFiles hook with the correct parameters when searchText criteria changes', () => {
119+
cy.customMount(
120+
<DatasetFiles
121+
filesRepository={fileRepository}
122+
datasetPersistentId={datasetPersistentId}
123+
datasetVersion={datasetVersion}
124+
/>
125+
)
126+
127+
cy.findByLabelText('Search this dataset').type('test{enter}')
128+
cy.wrap(fileRepository.getAllByDatasetPersistentId).should(
129+
'be.calledWith',
130+
datasetPersistentId,
131+
datasetVersion,
132+
new FileCriteria().withSearchText('test')
133+
)
134+
})
135+
118136
it("selects all rows when the 'Select all' button is clicked", () => {
119137
cy.customMount(
120138
<DatasetFiles

tests/component/sections/dataset/dataset-files/file-criteria-form/FileCriteriaForm.spec.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('FileCriteriaForm', () => {
5858
cy.findByRole('button', { name: 'Filter Type: All' }).should('not.exist')
5959
cy.findByRole('button', { name: 'Access: All' }).should('not.exist')
6060
cy.findByRole('button', { name: 'Filter Tag: All' }).should('not.exist')
61-
cy.findByAltText('Search this dataset').should('not.exist')
61+
cy.findByLabelText('Search this dataset').should('not.exist')
6262
})
6363

6464
it('renders the SortBy input', () => {
@@ -97,7 +97,7 @@ describe('FileCriteriaForm', () => {
9797
/>
9898
)
9999

100-
cy.findByAltText('Search this dataset').should('exist')
100+
cy.findByLabelText('Search this dataset').should('exist')
101101
})
102102

103103
it('saves global criteria when the sort by option changes', () => {
@@ -187,4 +187,27 @@ describe('FileCriteriaForm', () => {
187187
cy.findByRole('button', { name: 'Access: Public' }).should('exist')
188188
cy.findByRole('button', { name: 'Filter Tag: Data' }).should('exist')
189189
})
190+
191+
it('saves global criteria when the search input changes', () => {
192+
const criteria = new FileCriteria()
193+
.withFilterByTag('document')
194+
.withFilterByAccess(FileAccessOption.PUBLIC)
195+
.withFilterByType('image')
196+
.withSearchText('search')
197+
198+
cy.customMount(
199+
<FileCriteriaForm
200+
criteria={criteria}
201+
onCriteriaChange={onCriteriaChange}
202+
filesCountInfo={filesCountInfo}
203+
/>
204+
)
205+
206+
cy.findByLabelText('Search this dataset').clear().type('new search')
207+
208+
cy.findByRole('button', { name: 'Filter Type: Image' }).should('exist')
209+
cy.findByRole('button', { name: 'Access: Public' }).should('exist')
210+
cy.findByRole('button', { name: 'Filter Tag: Document' }).should('exist')
211+
cy.findByLabelText('Search this dataset').should('have.value', 'new search')
212+
})
190213
})
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
import { FileCriteriaSearchText } from '../../../../../../src/sections/dataset/dataset-files/file-criteria-form/FileCriteriaSearchText'
2+
import { FileCriteria } from '../../../../../../src/files/domain/models/FileCriteria'
3+
4+
const defaultCriteria = new FileCriteria()
25

36
describe('FilesSearch', () => {
47
it('renders the search input', () => {
5-
cy.customMount(<FileCriteriaSearchText />)
8+
const onCriteriaChange = cy.stub().as('onCriteriaChange')
9+
cy.customMount(
10+
<FileCriteriaSearchText criteria={defaultCriteria} onCriteriaChange={onCriteriaChange} />
11+
)
612

713
cy.findByLabelText('Search this dataset').should('exist')
814
cy.findByRole('button', { name: 'Submit search' }).should('exist')
915
})
16+
17+
it('calls onCriteriaChange with the searchText value on enter', () => {
18+
const onCriteriaChange = cy.stub().as('onCriteriaChange')
19+
20+
cy.customMount(
21+
<FileCriteriaSearchText criteria={defaultCriteria} onCriteriaChange={onCriteriaChange} />
22+
)
23+
24+
cy.findByLabelText('Search this dataset').type('test{enter}')
25+
cy.wrap(onCriteriaChange).should('be.calledWith', defaultCriteria.withSearchText('test'))
26+
})
27+
28+
it('calls onCriteriaChange with the searchText value on submit', () => {
29+
const onCriteriaChange = cy.stub().as('onCriteriaChange')
30+
31+
cy.customMount(
32+
<FileCriteriaSearchText criteria={defaultCriteria} onCriteriaChange={onCriteriaChange} />
33+
)
34+
35+
cy.findByLabelText('Search this dataset').type('test')
36+
cy.findByRole('button', { name: 'Submit search' }).click()
37+
cy.wrap(onCriteriaChange).should('be.calledWith', defaultCriteria.withSearchText('test'))
38+
})
1039
})

0 commit comments

Comments
 (0)