Skip to content

Commit

Permalink
Next.js pages router playground + final bug fixes before review
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Feb 14, 2025
1 parent fc4fa14 commit 2521d77
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 34 deletions.
3 changes: 2 additions & 1 deletion docs/pages/toolpad/core/api/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"dataSource": { "type": { "name": "object" } },
"initialPageSize": { "type": { "name": "number" }, "default": "100" },
"onCreateClick": { "type": { "name": "func" } },
"onDelete": { "type": { "name": "func" } },
"onEditClick": { "type": { "name": "func" } },
"onRowClick": { "type": { "name": "func" } },
"slotProps": {
"type": { "name": "shape", "description": "{ dataGrid?: object }" },
"default": "{}"
},
"slots": {
"type": { "name": "shape", "description": "{ dataGrid?: elementType }" },
"type": { "name": "shape", "description": "{ dataGrid?: func }" },
"default": "{}",
"additionalInfo": { "slotsApi": true }
}
Expand Down
1 change: 1 addition & 0 deletions docs/translations/api-docs/list/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"onCreateClick": {
"description": "Callback fired when the "Create" button is clicked."
},
"onDelete": { "description": "Callback fired when the item is successfully deleted." },
"onEditClick": { "description": "Callback fired when the "Edit" button is clicked." },
"onRowClick": {
"description": "Callback fired when a row is clicked. Not called if the target clicked is an interactive element added by the built-in columns."
Expand Down
23 changes: 17 additions & 6 deletions packages/toolpad-core/src/CRUD/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
}>({
values: {
...Object.fromEntries(
fields.map(({ field, type }) => [
field,
type === 'boolean' ? (initialValues[field] ?? false) : initialValues[field],
]),
fields
.filter(({ field }) => field !== 'id')
.map(({ field, type }) => [
field,
type === 'boolean' ? (initialValues[field] ?? false) : initialValues[field],
]),
),
...initialValues,
},
Expand Down Expand Up @@ -106,6 +108,8 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
setFormValues(initialValues);
}, [initialValues, setFormValues]);

const [hasSubmittedSuccessfully, setHasSubmittedSuccessfully] = React.useState(false);

const [, submitAction, isSubmitting] = React.useActionState<null | Error, FormData>(async () => {
if (validate) {
const errors = await validate(formValues);
Expand All @@ -125,9 +129,11 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {

if (onSubmitSuccess) {
onSubmitSuccess();
} else {
handleFormReset();
}

handleFormReset();
setHasSubmittedSuccessfully(true);
} catch (createError) {
notifications.show(`${localeText.submitErrorMessage}\n${(createError as Error).message}`, {
severity: 'error',
Expand Down Expand Up @@ -339,7 +345,12 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
</Grid>
</FormGroup>
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button type="submit" variant="contained" size="large" loading={isSubmitting}>
<Button
type="submit"
variant="contained"
size="large"
loading={isSubmitting || (hasSubmittedSuccessfully && !!onSubmitSuccess)}
>
{localeText.submitButtonLabel}
</Button>
</Box>
Expand Down
6 changes: 5 additions & 1 deletion packages/toolpad-core/src/CRUD/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ List.propTypes /* remove-proptypes */ = {
* Callback fired when the "Create" button is clicked.
*/
onCreateClick: PropTypes.func,
/**
* Callback fired when the item is successfully deleted.
*/
onDelete: PropTypes.func,
/**
* Callback fired when the "Edit" button is clicked.
*/
Expand All @@ -439,7 +443,7 @@ List.propTypes /* remove-proptypes */ = {
* @default {}
*/
slots: PropTypes.shape({
dataGrid: PropTypes.elementType,
dataGrid: PropTypes.func,
}),
} as any;

Expand Down
23 changes: 17 additions & 6 deletions packages/toolpad-core/src/Crud/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
}>({
values: {
...Object.fromEntries(
fields.map(({ field, type }) => [
field,
type === 'boolean' ? (initialValues[field] ?? false) : initialValues[field],
]),
fields
.filter(({ field }) => field !== 'id')
.map(({ field, type }) => [
field,
type === 'boolean' ? (initialValues[field] ?? false) : initialValues[field],
]),
),
...initialValues,
},
Expand Down Expand Up @@ -106,6 +108,8 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
setFormValues(initialValues);
}, [initialValues, setFormValues]);

const [hasSubmittedSuccessfully, setHasSubmittedSuccessfully] = React.useState(false);

const [, submitAction, isSubmitting] = React.useActionState<null | Error, FormData>(async () => {
if (validate) {
const errors = await validate(formValues);
Expand All @@ -125,9 +129,11 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {

if (onSubmitSuccess) {
onSubmitSuccess();
} else {
handleFormReset();
}

handleFormReset();
setHasSubmittedSuccessfully(true);
} catch (createError) {
notifications.show(`${localeText.submitErrorMessage}\n${(createError as Error).message}`, {
severity: 'error',
Expand Down Expand Up @@ -339,7 +345,12 @@ function FormPage<D extends DataModel>(props: FormPageProps<D>) {
</Grid>
</FormGroup>
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button type="submit" variant="contained" size="large" loading={isSubmitting}>
<Button
type="submit"
variant="contained"
size="large"
loading={isSubmitting || (hasSubmittedSuccessfully && !!onSubmitSuccess)}
>
{localeText.submitButtonLabel}
</Button>
</Box>
Expand Down
6 changes: 5 additions & 1 deletion packages/toolpad-core/src/Crud/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ List.propTypes /* remove-proptypes */ = {
* Callback fired when the "Create" button is clicked.
*/
onCreateClick: PropTypes.func,
/**
* Callback fired when the item is successfully deleted.
*/
onDelete: PropTypes.func,
/**
* Callback fired when the "Edit" button is clicked.
*/
Expand All @@ -439,7 +443,7 @@ List.propTypes /* remove-proptypes */ = {
* @default {}
*/
slots: PropTypes.shape({
dataGrid: PropTypes.elementType,
dataGrid: PropTypes.func,
}),
} as any;

Expand Down
6 changes: 3 additions & 3 deletions packages/toolpad-core/src/nextjs/NextAppProviderPages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AppProviderProps, Navigate, Router } from '../AppProvider';
* @ignore - internal component.
*/
export function NextAppProviderPages(props: AppProviderProps) {
const { push, replace, pathname, query } = useRouter();
const { push, replace, asPath, query } = useRouter();

const search = React.useMemo(() => {
const params = new URLSearchParams();
Expand Down Expand Up @@ -38,11 +38,11 @@ export function NextAppProviderPages(props: AppProviderProps) {

const routerImpl = React.useMemo<Router>(
() => ({
pathname,
pathname: asPath.split('?')[0],
searchParams,
navigate,
}),
[navigate, pathname, searchParams],
[asPath, navigate, searchParams],
);

return <AppProvider router={routerImpl} {...props} />;
Expand Down
4 changes: 3 additions & 1 deletion playground/nextjs-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
"@toolpad/core": "workspace:*",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"cookies-next": "^5.1.0",
"eslint-config-next": "15.1.6",
"next": "^15.1.6",
"next-auth": "5.0.0-beta.25",
"react": "^19.0.0",
"react-dom": "^19.0.0"
"react-dom": "^19.0.0",
"yup": "1.6.1"
}
}
Loading

0 comments on commit 2521d77

Please sign in to comment.