Comments (2)
Hey @filipneculciu,
Warning: value prop on input should not be null. Consider using an empty string to clear the component or undefined for uncontrolled components.
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 null
value to the input
element. A common solution proposed to the same problem would be to use the empty string ''
instead (example). You can have a translation layer between FE-BE.
Frontend Side:
If you receive values like this: const roles = ["Market", "Finance", "Development", null]
as mentioned in the attached example, you can have it translated to ''
like so:
{
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:
Since the backend would expect back the null
values in return, you can handle this translation in processRowUpdate
callback.
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!
from mui-x.
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:
Warning: Encountered two children with the same key, ``. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
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.
from mui-x.
Related Issues (20)
- [data grid] Providing InputComponentProps to valueInputProps in Filter Panel gives console warning due to props applying to all components in value column HOT 3
- [pickers] Date picker bug with disablePast HOT 4
- [data grid] Implementing Visual Feedback for Pasted Data HOT 3
- [charts] Support color scale
- [charts] Support color scale legend HOT 1
- [charts][docs-infra] Document interfaces in dedicated pages
- [data grid] Full-featured CRUD not editing data retrieved from database HOT 3
- [data grid] how to filter based on raw row data HOT 6
- With date-fns adapter, date picker date formatting for days of month < 10 always has zero padding, even when format specifies otherwise. HOT 2
- [data grid] position: absolute for rows to avoid repaint HOT 1
- [data grid] Incorrect class name rendered to GridToolbarQuickFilter when customized with GridToolbarContainer HOT 2
- [data grid] Time spend rendering Emotion/Pigment CSS HOT 2
- [pickers][DateTimePicker] not showing 2 in the hour category, but shows 3 twice and the first isn't selectable HOT 10
- [pickers] Date Picker style lost in prod env when loading `SEEK API` script HOT 1
- [datagrid] Performance: event-based reactivity
- [data grid] Use autocomplete in renderHeaderFilter HOT 1
- [data grid] useResizeContainer Console Error when wrapping <div> is modified HOT 1
- [pickers] How to remove leading 0 from TimePicker? HOT 3
- [data grid] Parameters needed for custom column resize. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mui-x.