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

Allow recreating data grid columns from existing data #914

Merged
merged 10 commits into from
Sep 8, 2022
27 changes: 23 additions & 4 deletions packages/toolpad-app/src/toolpad/propertyControls/GridColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as React from 'react';
import AddIcon from '@mui/icons-material/Add';
import DeleteIcon from '@mui/icons-material/Delete';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import RefreshIcon from '@mui/icons-material/Refresh';
import { inferColumns } from '@mui/toolpad-components';
import type { EditorProps } from '../../types';

Expand Down Expand Up @@ -59,11 +60,16 @@ function GridColumnsPropEditor({
const rowsValue = nodeId && bindings[`${nodeId}.props.rows`];
const definedRows: unknown = rowsValue?.value;

const inferredColumns = React.useMemo(
() => inferColumns(Array.isArray(definedRows) ? definedRows : []),
[definedRows],
);

const columnSuggestions = React.useMemo(() => {
const inferred = inferColumns(Array.isArray(definedRows) ? definedRows : []);
const existingFields = new Set(value.map(({ field }) => field));
return inferred.filter((column) => !existingFields.has(column.field));
}, [definedRows, value]);
return inferredColumns.filter((column) => !existingFields.has(column.field));
}, [inferredColumns, value]);
const hasColumnSuggestions = columnSuggestions.length > 0;

const handleCreateColumn = React.useCallback(
(suggestion: GridColDef) => () => {
Expand Down Expand Up @@ -99,6 +105,12 @@ function GridColumnsPropEditor({
[onChange, value],
);

const handleRecreateColumns = React.useCallback(() => {
if (hasColumnSuggestions) {
onChange(inferredColumns);
}
}, [hasColumnSuggestions, inferredColumns, onChange]);

return (
<React.Fragment>
<Button onClick={() => setEditColumnsDialogOpen(true)}>{label}</Button>
Expand Down Expand Up @@ -176,6 +188,13 @@ function GridColumnsPropEditor({
<React.Fragment>
<DialogTitle>Edit columns</DialogTitle>
<DialogContent>
<IconButton
aria-label="Recreate columns"
Copy link
Member

@Janpot Janpot Sep 7, 2022

Choose a reason for hiding this comment

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

We should probably add a tooltip instead.

@prakhargupta1 @gerdadesign nominating this component as well for a redesign. Perhaps we could even put this inline in the inspector, instead of in a dialog?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea, i'll add

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added tooltips to the "Recreate columns" and the "Add column" buttons

onClick={handleRecreateColumns}
disabled={!hasColumnSuggestions}
>
<RefreshIcon />
</IconButton>
<IconButton aria-label="Add column" onClick={handleMenuClick} disabled={disabled}>
<AddIcon />
</IconButton>
Expand All @@ -193,7 +212,7 @@ function GridColumnsPropEditor({
{suggestion.field}
</MenuItem>
))}
<MenuItem onClick={handleCreateColumn({ field: 'new' })}>new column</MenuItem>
<MenuItem onClick={handleCreateColumn({ field: 'new' })}>New column</MenuItem>
</Menu>
<List>
{value.map((colDef, i) => {
Expand Down