Skip to content

Introduce useColumnFilter #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ImmortalRabbit
Copy link
Contributor

Hi!

Introduced useColumnFilter to support filtering similarly as useColumnSort is implemented.

I have also created a Storybook example demonstrating how to use useColumnFilter.

Currently, I am not 100% sure how to implement a filter menu. I can only see menu props being available which simple icon that can triggered. Ideally, I would like to add a UI component to work alongside useColumnFilter.

Any suggestions on how I could implement this?

Also, if the hook itself is valuable to merge without the ui component, I can work on the filter UI component in a separate PR.

@ImmortalRabbit
Copy link
Contributor Author

ImmortalRabbit commented Jul 28, 2025

Hi @jassmith,

I'm still not sure—do you guys accept PRs? Sorry for just dropping it here.

#815 (comment)

@lukasmasuch lukasmasuch requested a review from Copilot August 8, 2025 23:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new useColumnFilter hook to support filtering data grid columns, similar to the existing useColumnSort implementation. The hook provides a way to filter rows based on custom filter callbacks applied to specific columns.

  • Adds useColumnFilter hook with support for single or multiple column filters
  • Includes comprehensive test coverage for multi-column filtering scenarios
  • Provides a Storybook example demonstrating the hook's usage with a simple number input filter

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
packages/source/src/use-column-filter.ts Core hook implementation with filtering logic and row mapping
packages/source/test/use-column-filter.test.tsx Test coverage for multi-column filtering functionality
packages/source/src/stories/use-data-source.stories.tsx Storybook example with filter input and integration
packages/source/src/index.ts Export statement for the new hook


export type ColumnFilter = {
column: GridColumn;
filterCallback: (cellValue: any) => boolean;
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cellValue parameter should be typed as GridCell instead of any since the hook passes the entire cell object (line 53) to the filter callback, not just the cell's data value.

Suggested change
filterCallback: (cellValue: any) => boolean;
filterCallback: (cellValue: GridCell) => boolean;

Copilot uses AI. Check for mistakes.

for (let sIndex = 0; sIndex < activeFilters.length; sIndex++) {
const { filter: colFilter } = activeFilters[sIndex];
const v = values[sIndex][r];
if (!colFilter.filterCallback(v)) {
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter callback receives the entire GridCell object v, but the type definition suggests it should receive cellValue: any. This inconsistency could cause runtime errors if consumers expect just the cell data rather than the full cell object.

Suggested change
if (!colFilter.filterCallback(v)) {
if (!colFilter.filterCallback(v.data)) {

Copilot uses AI. Check for mistakes.

const [filterValue, setFilterValue] = React.useState<number | undefined>(undefined);

const filterHandler = (value: number) => {
return filterValue > value;
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter callback expects a GridCell object but treats the parameter as a number. This will cause a runtime error since filterValue > value will be comparing a number to a GridCell object. Should be return filterValue > Number(value.data).

Suggested change
return filterValue > value;
const filterHandler = (value) => {
return filterValue > Number(value.data);

Copilot uses AI. Check for mistakes.

getCellContent,
filter: [
{ column: columns[0], filterCallback: (v) => v !== "2" },
{ column: columns[1], filterCallback: (v) => v !== "b" },
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test filter callback expects a string but will receive a GridCell object. This should be (v) => v.data !== "2" to access the cell's data property.

Suggested change
{ column: columns[1], filterCallback: (v) => v !== "b" },
{ column: columns[0], filterCallback: (v) => v.data !== "2" },
{ column: columns[1], filterCallback: (v) => v.data !== "b" },

Copilot uses AI. Check for mistakes.

getCellContent,
filter: [
{ column: columns[0], filterCallback: (v) => v !== "2" },
{ column: columns[1], filterCallback: (v) => v !== "b" },
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test filter callback expects a string but will receive a GridCell object. This should be (v) => v.data !== "b" to access the cell's data property.

Suggested change
{ column: columns[1], filterCallback: (v) => v !== "b" },
{ column: columns[1], filterCallback: (v) => v.data !== "b" },

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant