Skip to content
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

[DataGrid] Fix filter with object as value and value getter #1665

Merged
merged 8 commits into from
May 17, 2021

Conversation

dtassone
Copy link
Member

fix #1615

@@ -115,6 +115,31 @@ describe('<DataGrid /> - Filter', () => {
});
expect(getColumnValues()).to.deep.equal(['Nike']);
});

it('should allow object as value and work with valueGetter', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Could you replicate this test case for the other operators? Only "contains" is covered. The new test cases can be dynamically generated iterating over a [operator, value, expected] array to not flood this file with duplicated code.

Copy link
Member

@oliviertassinari oliviertassinari May 14, 2021

Choose a reason for hiding this comment

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

Would it run fast enough? We might have many combinations to test. Would it make sense to test the comparator unitarily?

Copy link
Member

Choose a reason for hiding this comment

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

It will add some ms to run everything, but there're not too many combinations. We have 3 string operators, so 3 test cases. With valueGetter tests it will be just more 3 test cases. It's kinda related to #1665 (comment). The idea to cover everything is to prevent us from accidentally using it inside a filter operator.

For another PR: We should add some tests for numeric and date operators. We just have for string and boolean operators.

const rowValue = column.valueGetter ? column.valueGetter(params) : params.value;
return Boolean(rowValue) === valueAsBoolean;
return ({ value }): boolean => {
return Boolean(value) === valueAsBoolean;
Copy link
Member

Choose a reason for hiding this comment

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

Should we change the type of params to not allow it to be passed to valueGetter? This is just to prevent us and users from making the same mistake in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so, as the issue was within the filters and not in the user's demo.

Copy link
Member

@oliviertassinari oliviertassinari May 14, 2021

Choose a reason for hiding this comment

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

@dtassone but can't developers write custom operators and fall into this trap?

Copy link
Member Author

Choose a reason for hiding this comment

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

What trap? I'm not sure I understand.
there is a type

export interface GridFilterOperator {
  label?: string;
  value: string;
  getApplyFilterFn: (
    filterItem: GridFilterItem,
    column: any,
  ) => null | ((params: GridCellParams) => boolean);
  InputComponent: React.JSXElementConstructor<GridFilterInputValueProps>;
  InputComponentProps?: Record<string, any>;
}

Copy link
Member

@m4theushw m4theushw May 14, 2021

Choose a reason for hiding this comment

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

{
  field: "country", // field exists in data
  headerName: "Country",
  valueGetter: (cellParams) => cellParams.value.name,
  flex: 0.5
}
{
  field: "country2", // field doesn't exist in data
  headerName: "Country",
  valueGetter: (cellParams) => cellParams.row.country.name,
  flex: 0.5
}

The trap is that if the valueGetter relays on value (first example), it can't be called inside the filter operator because cellParams.value will be already the resolved value (a string). If valueGetter relays on row (second example) there's no trap, but we don't know a priori what is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

we just have to know that cellParams consider valueGetter, which should be expected as the value wouldn't be correct otherwise.

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! labels May 14, 2021
Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

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

Looks good. I'll open another PR to try to remove the skip on Edge.

@dtassone dtassone merged commit cf8d8e4 into mui:master May 17, 2021
before(function before() {
if (isEdge) {
// We need to skip edge as it does not handle the date the same way as other browsers.
this.skip();
Copy link
Member

Choose a reason for hiding this comment

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

This problem is not with Edge but with BrowserStack. All Edge versions on it are with locale set to en_GB instead of en_US. Unfortunately, the karma launcher doesn't have an option to change the locale. Related to karma-runner/karma-browserstack-launcher#187.

Copy link
Member

@oliviertassinari oliviertassinari May 17, 2021

Choose a reason for hiding this comment

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

The logic should be timezone invariant, we don't control the timezone of the end users.

So it sounds very much like a bug with the filtering logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Filtering row doesn't work when use on columns with properties "field" and "valueGetter"
3 participants