-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Table): Support local filters on columns (#314)
* feat: Add filter to Table (#270) * feat: Add filter to Table * feat: Table filtering improvements * fix: Table storybook * feat: Improve SelectColumnFilter styles * feat: Add test for filtering table * fix: Lint table test file * feat: Add disableFilters for table filter story * fix: Add Stack for vertical spacing * fix: New property to control column filter * fix: Update table story * fix: column filter table test * fix: table lint * chore: Minor update on the docs * chore: Use search type input * chore: Fix the issue the column filters are all on when the global filter is used Co-authored-by: Maksim Markelov <[email protected]>
- Loading branch information
1 parent
cbda94f
commit 7d43b42
Showing
9 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/components/Table/components/DefaultColumnFilter/index.tsx
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,39 @@ | ||
/** ******************************************************************************************************************* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. * | ||
******************************************************************************************************************** */ | ||
import React from 'react'; | ||
import Input from '../../../Input'; | ||
import { UseFiltersColumnProps } from 'react-table'; | ||
|
||
interface Props<D extends object> { | ||
column: UseFiltersColumnProps<D>; | ||
} | ||
|
||
export default function DefaultColumnFilter<D extends object>({ | ||
column: { filterValue, preFilteredRows, setFilter }, | ||
}: Props<D>) { | ||
const count = preFilteredRows.length; | ||
|
||
return ( | ||
<Input | ||
value={filterValue || ''} | ||
onChange={(e) => { | ||
setFilter(e || undefined); | ||
}} | ||
type="search" | ||
placeholder={`Search ${count} records...`} | ||
/> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** ******************************************************************************************************************* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. * | ||
******************************************************************************************************************** */ | ||
import React from 'react'; | ||
import { DataType } from './type'; | ||
import { Column } from '../'; | ||
import Select from '../../Select'; | ||
import { Button } from '../../index'; | ||
import { Inline } from '../../../layouts'; | ||
|
||
function SelectColumnFilter({ column: { filterValue, setFilter } }: any) { | ||
const options = [ | ||
{ | ||
label: 'Order 11', | ||
value: 'Order 11', | ||
}, | ||
{ | ||
label: 'Order 12', | ||
value: 'Order 12', | ||
}, | ||
{ | ||
label: 'Order 13', | ||
value: 'Order 13', | ||
}, | ||
]; | ||
|
||
return ( | ||
<Inline spacing="xs"> | ||
<Select | ||
options={options} | ||
selectedOption={options.find(({ value }) => value === filterValue)} | ||
onChange={(e) => { | ||
setFilter(e.target.value || undefined); | ||
}} | ||
/> | ||
<Button onClick={() => setFilter(undefined)} size="small"> | ||
Clear | ||
</Button> | ||
</Inline> | ||
); | ||
} | ||
|
||
const filterColumnDefinition: Column<DataType>[] = [ | ||
{ | ||
id: 'id', | ||
width: 200, | ||
Header: 'Id', | ||
accessor: 'id', | ||
}, | ||
{ | ||
id: 'name', | ||
width: 200, | ||
Header: 'Name', | ||
Filter: SelectColumnFilter, | ||
accessor: 'name', | ||
}, | ||
{ | ||
id: 'createdDate', | ||
width: 200, | ||
Header: 'Created date', | ||
accessor: 'createdDate', | ||
}, | ||
{ | ||
id: 'accounts', | ||
width: 200, | ||
Header: '# Accounts', | ||
disableFilters: true, | ||
disableGlobalFilter: true, | ||
accessor: (row) => row.accounts?.length, | ||
}, | ||
]; | ||
|
||
export default filterColumnDefinition; |
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
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
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
Oops, something went wrong.