Skip to content

Commit

Permalink
feat(datagrid): adds radio filter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMelox committed Aug 15, 2024
1 parent 8cb241b commit 83fef74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/ibm-products/src/components/Datagrid/useFiltering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ import {
DATE,
MULTISELECT,
NUMBER,
RADIO,
} from './Datagrid/addons/Filtering/constants';
import { Hooks, TableInstance } from 'react-table';
import { DataGridState } from './types';

// This function was taken from https://github.com/TanStack/table/blob/v7/src/filterTypes.js
export const exactText = (rows, ids, filterValue) => {
return rows.filter((row) => {
return ids.some((id) => {
const rowValue = row.values[id];
return rowValue !== undefined
? String(rowValue).toLowerCase() === String(filterValue).toLowerCase()
: true;
});
});
};

const handleMultiFilter = (rows, id, value) => {
// gets all the items that are selected and returns their value
const selectedItems = value
Expand Down Expand Up @@ -72,6 +85,7 @@ const useFiltering = (hooks: Hooks) => {
},
[CHECKBOX]: (rows, id, value) => handleMultiFilter(rows, id, value),
[MULTISELECT]: (rows, id, value) => handleMultiFilter(rows, id, value),
[RADIO]: (rows, id, value) => exactText(rows, id, value),
}),
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const FilteringUsage = ({ defaultGridProps }) => {
{
Header: 'Role',
accessor: 'role',
filter: 'radio',
},
];

Expand Down

0 comments on commit 83fef74

Please sign in to comment.