-
-
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
[data grid] How to handle nullable values for singleSelect columns? #12383
Comments
Hey @filipneculciu,
The problem mentioned is with the underlying native input event, not even with the underlying material component used (Select). You can observe the same error in this example where no MUI component is used: https://stackblitz.com/edit/vitejs-vite-wbs7mo?file=src%2FApp.tsx,src%2Fmain.tsx&terminal=dev So, by definition, you can not pass Frontend Side: {
field: "role",
headerName: "Department",
width: 220,
editable: true,
type: "singleSelect",
valueOptions: roles.map((value) =>
value !== null
? { value: value, label: value }
: { value: '', label: "None" }
),
} Backend Side: const processRowUpdate = async (newRow: GridRowModel) => {
const updatedRow = { ...newRow, isNew: false } as GridRowModel;
const serverRow = { ...updatedRow, role: updatedRow.role === '' ? null : updatedRow.role};
setLoading(true); // optional optimization with the `loading` prop passed to the grid
await server.post('https://api.url', serverRow); // send the row with `null` value on server
setLoading(false);
return updatedRow; // return the value with `''` value on FE
}; Here's the modified sandbox: https://codesandbox.io/p/sandbox/snowy-cache-pf36j5?file=%2Fsrc%2FDemo.tsx%3A35%2C36 Let me know if it makes sense. Thank you! |
Yes, it makes sense; this is the workaround I was thinking about in the last sentence of my paragraph. I guess this translation layer is unavoidable, I was thinking that maybe there was an option to have the DataGrid do it internally. There is one issue that this mapping causes and it's reproducible in your sandbox (https://codesandbox.io/p/sandbox/snowy-cache-pf36j5?file=%2Fsrc%2FDemo.tsx%3A35%2C36). Open then filtering menu for the "Department" column and you'll get the following warning in the console: I guess this is because the value is '' for both no selection and for "None". Do you have a suggestion to fix this? My only idea was to try something with non-printable characters instead of '', but this solution seems hacky to me. |
@filipneculciu you can just use a different value instead of
and to adjust the parsing in
Would that work for you? |
The issue has been inactive for 7 days and has been automatically closed. |
The problem in depth
Do you have any recommendation/best practices for handling singleSelect columns with nullable values in data grids?
Having null as a possible values leads to warnings in the console log, when switching between null and non-null values; such as:
Warning:
value
prop oninput
should not be null. Consider using an empty string to clear the component orundefined
for uncontrolled components.Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components
I have modified the code example from here: https://mui.com/x/react-data-grid/editing/#full-featured-crud to use DataGridPro and make the Department nullable. You can find the sandbox here: https://codesandbox.io/p/sandbox/elastic-fog-k829hp?file=%2Fsrc%2FDemo.tsx
To see the warning you can try to open the console log before changing to a null value in one of the cells.
In the APIs I use, I get the values as null and need to send them as null. I'd like to know if I can work with them in the DataGrid like this (which would be ideal) or I need to do some workaround (map them to something else for display and map them back when communicating with the API).
Your environment
`npx @mui/envinfo`
Search keywords: singleSelect column nullable
Order ID: 70895
The text was updated successfully, but these errors were encountered: