-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from M3nin0/dev
table: generalizing global filter component
- Loading branch information
Showing
5 changed files
with
166 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* This file is part of GEO-Components-React. | ||
* Copyright (C) 2022-2024 GEO Secretariat. | ||
* | ||
* GEO-Components-React is free software; you can redistribute it and/or modify it | ||
* under the terms of the MIT License; see LICENSE file for more details. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { useAsyncDebounce } from 'react-table'; | ||
import { Icon, Input } from 'semantic-ui-react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* Global filter component for the external resources table. | ||
*/ | ||
export const BaseGlobalFilter = ({ | ||
preGlobalFilteredRows, | ||
globalFilter, | ||
setGlobalFilter, | ||
}) => { | ||
const count = preGlobalFilteredRows.length; | ||
const [value, setValue] = React.useState(globalFilter); | ||
const onChange = useAsyncDebounce((value) => { | ||
setGlobalFilter(value || undefined); | ||
}, 200); | ||
|
||
return ( | ||
<Input | ||
fluid | ||
icon | ||
placeholder={'Type to search...'} | ||
value={value || ''} | ||
onChange={(e) => { | ||
setValue(e.target.value); | ||
onChange(e.target.value); | ||
}} | ||
> | ||
<input /> | ||
<Icon name={'search'} /> | ||
</Input> | ||
); | ||
}; | ||
|
||
BaseGlobalFilter.propTypes = { | ||
preGlobalFilteredRows: PropTypes.object.isRequired, | ||
globalFilter: PropTypes.object.isRequired, | ||
setGlobalFilter: PropTypes.object, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* This file is part of GEO-Components-React. | ||
* Copyright (C) 2022 GEO Secretariat. | ||
* | ||
* GEO-Components-React is free software; you can redistribute it and/or modify it | ||
* under the terms of the MIT License; see LICENSE file for more details. | ||
*/ | ||
|
||
import React, { useMemo } from 'react'; | ||
import { useTable } from 'react-table'; | ||
|
||
import { BaseTable } from './BaseTable'; | ||
import { BaseGlobalFilter } from './BaseGlobalFilter'; | ||
|
||
import { render, screen } from '../../../../setupTestRenders'; | ||
|
||
import tabelData from '../../../../mocks/table/table-data.json'; | ||
|
||
const ComponentTemplate = (args) => { | ||
const tableRows = useMemo(() => tabelData); | ||
const tableColumns = useMemo(() => { | ||
return [ | ||
{ | ||
Header: 'Name', | ||
accessor: 'name', | ||
}, | ||
{ | ||
Header: 'Role', | ||
accessor: 'role', | ||
}, | ||
{ | ||
Header: 'Type', | ||
accessor: 'type', | ||
}, | ||
]; | ||
}); | ||
|
||
const { getTableProps, getTableBodyProps, rows, columns, prepareRow } = | ||
useTable({ | ||
columns: tableColumns, | ||
data: tableRows, | ||
}); | ||
|
||
return ( | ||
<BaseTable | ||
columns={columns} | ||
rows={rows} | ||
getTableProps={getTableProps} | ||
getTableBodyProps={getTableBodyProps} | ||
prepareRow={prepareRow} | ||
globalFilter={(globalFilter, preGlobalFilteredRows, setGlobalFilter) => ( | ||
<BaseGlobalFilter | ||
globalFilter={globalFilter} | ||
setGlobalFilter={setGlobalFilter} | ||
preGlobalFilteredRows={preGlobalFilteredRows} | ||
/> | ||
)} | ||
{...args} | ||
/> | ||
); | ||
}; | ||
|
||
describe('BaseGlobalFilter tests', () => { | ||
describe('Render tests', () => { | ||
it('should render with the required props without crashing', () => { | ||
render(<ComponentTemplate />); | ||
|
||
const tableElement = screen.getByRole('table'); | ||
expect(tableElement).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
*/ | ||
|
||
export { BaseTable } from './BaseTable'; | ||
export { BaseGlobalFilter } from './BaseGlobalFilter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters