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

Fix/default editable grid #3526

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/toolpad-studio-components/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ export function parseColumns(columns: SerializableGridColumns): GridColDef[] {
return columns.map(({ type: colType, ...column }) => {
const isIdColumn = column.field === 'id';

let baseColumn: Omit<GridColDef, 'field'> = { editable: true };
let baseColumn: Omit<GridColDef, 'field'> = {};

if (isIdColumn) {
baseColumn = {
Expand Down Expand Up @@ -1134,6 +1134,9 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
setActionResult,
);

const useDataProvider = useNonNullableContext(UseDataProviderContext);
const { dataProvider } = useDataProvider(dataProviderId || null);

const nodeRuntime = useNode<ToolpadDataGridProps>();

const handleResize = React.useMemo(
Expand Down Expand Up @@ -1235,6 +1238,11 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
[selection?.id],
);

const isRowUpdateModelAvailable = React.useMemo(
() => !!dataProvider?.updateRecord,
[dataProviderProps],
);

const columns: GridColDef[] = React.useMemo(
() => (columnsProp ? parseColumns(columnsProp) : []),
[columnsProp],
Expand Down Expand Up @@ -1262,7 +1270,18 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
}, [nodeRuntime, rows]);

const renderedColumns = React.useMemo<GridColDef[]>(() => {
const result = [...columns];
const mapEditableToColumns = columns.map((column) => {
// Don't touch the existing columns with `editable` value, example: `id` column
if (column.editable || column.editable === false) {
return column;
}

column.editable = isRowUpdateModelAvailable;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should avoid mutating items in .map. Perhaps something like the following would work?

return column.editable === undefined ? { ...column, editable: isRowUpdateModelAvailable } : column;


return column;
});

const result = [...mapEditableToColumns];

if (getProviderActions) {
result.push({
Expand All @@ -1276,7 +1295,7 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
}

return result;
}, [columns, getProviderActions]);
}, [columns, getProviderActions, isRowUpdateModelAvailable]);

const appHost = useAppHost();
const isProPlan = appHost.plan === 'pro';
Expand Down
Loading