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] Allow valueOptions to accept a function #2850

Merged
merged 14 commits into from
Oct 28, 2021

Conversation

alexfauquette
Copy link
Member

@alexfauquette alexfauquette commented Oct 12, 2021

Fixes #2516

For columns with type singleSelect, the valueOptions can now be a function. This function takes an argument row containing the row values and returns an array of options.

valueOptions are also used to filter the column. In that case, the function is called without argument. The user can choose what to return when row === undefenied.

The functionality can be tested here: https://deploy-preview-2850--material-ui-x.netlify.app/components/data-grid/columns/#column-types

@alexfauquette alexfauquette self-assigned this Oct 13, 2021
@@ -83,7 +85,7 @@ export interface GridColDef {
/**
* To be used in combination with `type: 'singleSelect'`. This is an array of the possible cell values and labels.
*/
valueOptions?: Array<string | number | { value: any; label: string }>;
valueOptions?: ValueOptionsArray | ((params?: GridRowModel) => ValueOptionsArray);
Copy link
Member

Choose a reason for hiding this comment

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

I see we are passing the GridRowModel but I think it may be also valuable to pass in the current column, as in the column field. What do you think?

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 do not think it is usefull to pass the column attributes, because valueOptions is defined for a specific function. So it will be used for only one column.

Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily - if I want to pull in the value options from an API for example I would need to know for which column I need to fetch them.

Copy link
Member

Choose a reason for hiding this comment

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

So are we adding the field or proceeding without it?

Copy link
Member

Choose a reason for hiding this comment

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

We could create the GridValueOptionsParams interface with the id, field and row. It's more to keep the same pattern used by the other props. The column we don't need to pass, as it would also include the value options which, if called, could start an infinite loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's done. I'm wondering if we should let id and row undefined in the header (no row profited) or if we should add a boolean property isFilteringOptions which is more explicit than checking if the row is defined.

Copy link
Member

Choose a reason for hiding this comment

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

We can keep id and row undefined when calling from the filter panel. That's how the value formatter works.

https://github.com/mui-org/material-ui-x/blob/6ce542ba7a3e04cb68eb281002630cd97b39795d/packages/grid/_modules_/grid/models/params/gridCellParams.ts#L90-L92

@@ -54,7 +55,9 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps & TextFieldPr
// NativeSelect casts the value to a string.
if (type === 'singleSelect') {
const column = apiRef.current.getColumn(item.columnField);
value = column.valueOptions
const columnValueOptions =
typeof column.valueOptions === 'function' ? column.valueOptions() : column.valueOptions;
Copy link
Member

Choose a reason for hiding this comment

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

If we also pass in the column field we can provide it here so that devs can know which column they are currently working with.

@DanailH
Copy link
Member

DanailH commented Oct 13, 2021

Looks nice! A general rule of thumb - I would wait on the docs a bit. If you need to do changes to the implementation it will be double work to also fix the docs. You can complete the docs once we nail the implementation.

Also, we would need to add tests for the new functionality. I would apply the same for the tests as with the docs 😉

@flaviendelangle
Copy link
Member

Somebody knows why the CI do not work on @alexfauquette PRs ?

@DanailH DanailH changed the title [DataGrid] allows valueOptions to be a function [DataGrid] Allow valueOptions to accept a function Oct 13, 2021
@DanailH
Copy link
Member

DanailH commented Oct 13, 2021

Somebody knows why the CI do not work on @alexfauquette PRs ?

No idea, it's on both of his PRs !!?

@flaviendelangle
Copy link
Member

No idea, it's on both of his PRs !!?

Yeah, so I don't think it will solve on next one :/

@DanailH
Copy link
Member

DanailH commented Oct 19, 2021

@alexdanilowicz if Argos is failing you can check the new screenshot and approve it if you think it is not relevant.

@alexfauquette
Copy link
Member Author

Done, I think you tagged the wrong guy @DanailH

@alexfauquette alexfauquette force-pushed the adapt-optionValues-to-row branch from 1d0d319 to 6f21838 Compare October 20, 2021 09:13
Comment on lines +6 to +19
export interface GridValueOptionsParams {
/**
* The field of the column to which options will be provided
*/
field: string;
/**
* The grid row id.
*/
id?: GridRowId;
/**
* The row model of the row that the current cell belongs to.
*/
row?: GridRowModel;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the parameter pass as an argument to the valueOptions. id and row are not passed if it is on the header.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 20, 2021
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

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.

*/
valueOptions?: Array<string | number | { value: any; label: string }>;
valueOptions?:
| Array<string | number | { value: any; label: string }>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
| Array<string | number | { value: any; label: string }>
| Array<ValueOptions>

Copy link
Member Author

@alexfauquette alexfauquette Oct 22, 2021

Choose a reason for hiding this comment

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

About this, I'm a bit concerned by the types in the doc API. The signature of this props does not seems to be clear.

Before, it was

(string | number | { label: string; value: any })[] 

So ok, I know I can pass a value or a specific type of objects to the prop. Now the type is

ValueOptions[] | ((params: GridValueOptionsParams) => ValueOptions[])

It is not possible to find what ValueOptions and GridValueOptionsParams are in the doc API.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the docs is not great yet. We have been iterating it for a long time. In the future, there will proper docs for these small type definitions. See #1529 (comment). For now, we can keep ValueOptions.

@alexfauquette alexfauquette force-pushed the adapt-optionValues-to-row branch from bc3aeab to a9628be Compare October 22, 2021 15:32
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 22, 2021
@alexfauquette alexfauquette requested a review from DanailH October 27, 2021 08:05
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 to me. I merged with next to fix one the the failing screenshots. You can approve the other one in argos.

@alexfauquette alexfauquette merged commit c7c80bc into mui:next Oct 28, 2021
@alexfauquette alexfauquette deleted the adapt-optionValues-to-row branch October 28, 2021 09:19
@oliviertassinari oliviertassinari added the component: data grid This is the name of the generic UI component, not the React module! label Oct 28, 2021
@oliviertassinari oliviertassinari added the new feature New feature or request label Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Support passing a function to valueOptions
5 participants