Skip to content

Commit

Permalink
Create component - saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Jan 16, 2025
1 parent 0bb5aa1 commit dccbb8e
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 25 deletions.
42 changes: 42 additions & 0 deletions packages/toolpad-core/src/CRUD/Create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client';
import * as React from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Grid from '@mui/material/Grid2';
import TextField from '@mui/material/TextField';
import { CRUDFields, DataModel } from './shared';

export interface CreateProps<D extends DataModel> {
/**
* Fields to show.
*/
fields: CRUDFields;
/**
* Methods to interact with server-side data.
*/
methods: {
createOne: (data: Omit<D, 'id'>) => Promise<D>;
};
}

function Create<D extends DataModel>(props: CreateProps<D>) {
const { fields, methods } = props;
const { createOne } = methods;

return (
<Box component="form" noValidate autoComplete="off">
<Grid container spacing={2} sx={{ mb: 2 }}>
{fields.map((field) => (
<Grid key={field.field} size={6}>
<TextField id={field.field} label={field.headerName} sx={{ width: '100%' }} />
</Grid>
))}
</Grid>
<Button type="submit" variant="contained">
Contained
</Button>
</Box>
);
}

export { Create };
1 change: 1 addition & 0 deletions packages/toolpad-core/src/CRUD/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './List';
export * from './Show';
export * from './Create';
5 changes: 5 additions & 0 deletions playground/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const NAVIGATION: Navigation = [
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders/new',
title: 'New Order',
icon: <ShoppingCartIcon />,
},
{
segment: 'orders',
title: 'Orders',
Expand Down
5 changes: 5 additions & 0 deletions playground/vite/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Layout from './layouts/dashboard';
import DashboardPage from './pages';
import OrdersPage from './pages/orders';
import OrderPage from './pages/order';
import NewOrderPage from './pages/new-order';

const router = createBrowserRouter([
{
Expand All @@ -27,6 +28,10 @@ const router = createBrowserRouter([
path: 'orders/:orderId',
Component: OrderPage,
},
{
path: 'orders/new',
Component: NewOrderPage,
},
],
},
],
Expand Down
35 changes: 35 additions & 0 deletions playground/vite/src/pages/new-order.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react';
import { Create } from '@toolpad/core/CRUD';

interface Order extends Record<string, unknown> {
id: number;
name: string;
status: string;
}

const orderFields = [
{ field: 'id', headerName: 'ID' },
{ field: 'name', headerName: 'Name' },
{ field: 'status', headerName: 'Status' },
];

export default function OrderPage() {
return (
<Create<Order>
fields={orderFields}
methods={{
createOne: () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
id: 69,
name: 'Order 69',
status: 'pending',
});
}, 1500);
});
},
}}
/>
);
}
6 changes: 5 additions & 1 deletion playground/vite/src/pages/orders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default function OrdersPage() {
[navigate],
);

const handleCreateClick = React.useCallback(() => {
navigate('/orders/new');
}, [navigate]);

return (
<List<Order>
fields={orderFields}
Expand Down Expand Up @@ -52,7 +56,7 @@ export default function OrdersPage() {
}}
initialPageSize={25}
onRowClick={handleRowClick}
onCreateClick={() => {}}
onCreateClick={handleCreateClick}
onEditClick={() => {}}
/>
);
Expand Down
Loading

0 comments on commit dccbb8e

Please sign in to comment.