-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Allow valueOptions
to accept a function
#2850
Conversation
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -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; |
There was a problem hiding this comment.
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.
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 😉 |
Somebody knows why the CI do not work on @alexfauquette PRs ? |
valueOptions
to accept a function
No idea, it's on both of his PRs !!? |
Yeah, so I don't think it will solve on next one :/ |
@alexdanilowicz if Argos is failing you can check the new screenshot and approve it if you think it is not relevant. |
Done, I think you tagged the wrong guy @DanailH |
1d0d319
to
6f21838
Compare
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; | ||
} |
There was a problem hiding this comment.
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.
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks nice. We only miss a couple of tests for the edit cell and the filter:
*/ | ||
valueOptions?: Array<string | number | { value: any; label: string }>; | ||
valueOptions?: | ||
| Array<string | number | { value: any; label: string }> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Array<string | number | { value: any; label: string }> | |
| Array<ValueOptions> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
docs/src/pages/components/data-grid/columns/ColumnTypesGrid.tsx
Outdated
Show resolved
Hide resolved
docs/src/pages/components/data-grid/columns/ColumnTypesGrid.tsx
Outdated
Show resolved
Hide resolved
bc3aeab
to
a9628be
Compare
There was a problem hiding this 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.
Fixes #2516
For columns with type
singleSelect
, thevalueOptions
can now be a function. This function takes an argumentrow
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