gregnb / gregnb/mui-datatables
Using mui-datatables in a type-safe way by having access to the original object
- Dominant language
- JavaScript
- Stars
- 2.7k
- Forks
- 906
- PR merge metrics
- No merged PRs in 30d
Description
(largely based on my similar reply to issue #109, but decided it was more appropriate to start an issue as well to get the discussion rolling)
Hi @gregnb, thank you for your great work on this.
I wonder if it's possible use `mui-datatables` in a more "type-safe" way. Maybe it's already available or maybe it's something that can be easily implemented.
Currently we specify the columns as such:
const columns = [
{
name: 'object_field',
label: 'Object field label'
}
}
This will take the `object_field` field from each object in the array sent to `data`. However, specifying the field name as a string isn't type-safe. TypeScript has no way of knowing whether objects in `data` contain or don't contain the field `object_field`.
### Question:
Is it possible perhaps, instead of having `name` as a required field, another option could be added? (let's call it `value`, required when `name` isn't specified), that would be used as such:
const columns = [
{
value: (obj) => obj.object_field,
label: 'Object field'
}
}
This way, we could use it with TypeScript:
value: (obj: KnownType) => obj.object_field,
...and TypeScript would check that `object_field` does indeed belong to `KnownType`. Since it's an extra option, it wouldn't interfere with the current way of doing things or JS-only users.
Would also be nice to get the original data object in `customBodyRender`. Use case for this is: if we want to render one column based on a value from another.
Right now we *do* have access to the values array in `tableMeta.rowData`, The caveat is: the order matters. Changing the column order requires a change in how we access `tableMeta.rowData`. But would be nice to be able to keep the type information if using TypeScript.
So maybe we could have the original object available in the parameters sent to `customBodyRender`?
What are your thoughts? Thanks!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.