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 3 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
16 changes: 12 additions & 4 deletions packages/toolpad-studio-components/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ function getNarrowedColType(type?: string): GridColType | undefined {
return (type && type in DEFAULT_COLUMN_TYPES ? type : undefined) as GridColType | undefined;
}

export function parseColumns(columns: SerializableGridColumns): GridColDef[] {
export function parseColumns(columns: SerializableGridColumns, isEditable?: boolean): GridColDef[] {
Copy link
Member

@Janpot Janpot May 6, 2024

Choose a reason for hiding this comment

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

parseColumns acts as a conversion between "serializable columns" and "X grid columns". It's being reused across the codebase for this purpose. I prefer to keep this function single purpose.
How do you feel about solving this downstream instead? Where we iterate a second time to create the rendered columns:
https://github.com/mui/mui-toolpad/blob/02cc9a97b76098d51651eb48cf10c12ccdc7c8fe/packages/toolpad-studio-components/src/DataGrid.tsx#L1264-L1279

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Janpot , say we're having id column it shouldn't be editable at all right, should I move the isIdColumn logic to this function?

If we update the editable here id column may probably get override right?

Copy link
Member

@Janpot Janpot May 6, 2024

Choose a reason for hiding this comment

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

How about we only set isEditable if it isn't already defined? And remove isEditable: true from the baseColumn in parseColumns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, that's a good approach. Will update accordingly.

return columns.map(({ type: colType, ...column }) => {
const isIdColumn = column.field === 'id';

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

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,9 +1238,14 @@ const DataGridComponent = React.forwardRef(function DataGridComponent(
[selection?.id],
);

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

const columns: GridColDef[] = React.useMemo(
() => (columnsProp ? parseColumns(columnsProp) : []),
[columnsProp],
() => (columnsProp ? parseColumns(columnsProp, isRowUpdateModelAvailable) : []),
[columnsProp, isRowUpdateModelAvailable],
);

React.useEffect(() => {
Expand Down
Loading