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 to keep form validation when using custom save buttons #4458

Merged
merged 19 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions packages/ra-core/src/form/FormContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React, { createContext } from 'react';
import { FormFunctions } from '../types';

const defaultFormFunctions: FormFunctions = { setOnSave: () => {} };

const FormContext = createContext<FormFunctions>(defaultFormFunctions);

FormContext.displayName = 'FormContext';

export default FormContext;
6 changes: 3 additions & 3 deletions packages/ra-core/src/form/FormWithRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import arrayMutators from 'final-form-arrays';
import useInitializeFormWithRecord from './useInitializeFormWithRecord';
import sanitizeEmptyValues from './sanitizeEmptyValues';
import getFormInitialValues from './getFormInitialValues';
import OnSaveContext from './OnSaveContext';
import FormContext from './FormContext';

/**
* Wrapper around react-final-form's Form to handle redirection on submit,
Expand Down Expand Up @@ -83,7 +83,7 @@ const FormWithRedirect = ({
};

return (
<OnSaveContext.Provider value={setOnSave}>
<FormContext.Provider value={{ setOnSave }}>
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
<Form
key={version} // support for refresh button
debug={debug}
Expand All @@ -110,7 +110,7 @@ const FormWithRedirect = ({
/>
)}
</Form>
</OnSaveContext.Provider>
</FormContext.Provider>
);
};

Expand Down
10 changes: 0 additions & 10 deletions packages/ra-core/src/form/OnSaveContext.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import addField from './addField';
import FormDataConsumer from './FormDataConsumer';
import OnSaveContext from './OnSaveContext';
import FormContext from './FormContext';
import FormField from './FormField';
import FormWithRedirect from './FormWithRedirect';
import useInput, { InputProps } from './useInput';
Expand Down Expand Up @@ -29,7 +29,7 @@ export {
useInitializeFormWithRecord,
useSuggestions,
ValidationError,
OnSaveContext,
FormContext,
};
export { isRequired } from './FormField';
export * from './validate';
Expand Down
4 changes: 4 additions & 0 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,7 @@ export type Exporter = (
export type SetOnSave = (
onSave?: (values: object, redirect: any) => void
) => void;

export type FormFunctions = {
setOnSave?: SetOnSave;
};
8 changes: 4 additions & 4 deletions packages/ra-ui-materialui/src/button/SaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
useNotify,
RedirectionSideEffect,
Record,
OnSaveContext,
FormContext,
} from 'ra-core';

const SaveButton: FC<SaveButtonProps> = ({
Expand All @@ -38,13 +38,13 @@ const SaveButton: FC<SaveButtonProps> = ({
const classes = useStyles({ classes: classesOverride });
const notify = useNotify();
const translate = useTranslate();
const setOnSave = useContext(OnSaveContext);
const formContext = useContext(FormContext);
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved

const handleClick = event => {
if (typeof onSave === 'function') {
setOnSave(onSave);
formContext.setOnSave(onSave);
} else {
setOnSave();
formContext.setOnSave();
}
if (saving) {
// prevent double submission
Expand Down