From 83fef74b85cdacee1f1a4ac42c13fd78c9631652 Mon Sep 17 00:00:00 2001 From: Alexander Melo Date: Thu, 15 Aug 2024 13:44:42 -0400 Subject: [PATCH] feat(datagrid): adds radio filter --- .../src/components/Datagrid/useFiltering.ts | 14 ++++++++++++++ .../components/Datagrid/utils/FilteringUsage.js | 1 + 2 files changed, 15 insertions(+) diff --git a/packages/ibm-products/src/components/Datagrid/useFiltering.ts b/packages/ibm-products/src/components/Datagrid/useFiltering.ts index 5d135628a8..21b7653f65 100644 --- a/packages/ibm-products/src/components/Datagrid/useFiltering.ts +++ b/packages/ibm-products/src/components/Datagrid/useFiltering.ts @@ -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 @@ -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), }), [] ); diff --git a/packages/ibm-products/src/components/Datagrid/utils/FilteringUsage.js b/packages/ibm-products/src/components/Datagrid/utils/FilteringUsage.js index f6f5be6142..8ceecec13e 100644 --- a/packages/ibm-products/src/components/Datagrid/utils/FilteringUsage.js +++ b/packages/ibm-products/src/components/Datagrid/utils/FilteringUsage.js @@ -96,6 +96,7 @@ export const FilteringUsage = ({ defaultGridProps }) => { { Header: 'Role', accessor: 'role', + filter: 'radio', }, ];