Skip to content

Commit

Permalink
Fix Form Types
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Mar 8, 2021
1 parent 4a992be commit 233c7d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
37 changes: 22 additions & 15 deletions packages/ra-core/src/form/FormWithRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import * as React from 'react';
import { FC, useRef, useCallback, useEffect, useMemo } from 'react';
import {
Form,
FormProps,
FormRenderProps as FinalFormFormRenderProps,
} from 'react-final-form';
import { Form, FormProps, FormRenderProps } from 'react-final-form';
import arrayMutators from 'final-form-arrays';

import useInitializeFormWithRecord from './useInitializeFormWithRecord';
import useWarnWhenUnsavedChanges from './useWarnWhenUnsavedChanges';
import sanitizeEmptyValues from './sanitizeEmptyValues';
import getFormInitialValues from './getFormInitialValues';
import { FormContextValue, Record, OnSuccess, OnFailure } from '../types';
import {
FormContextValue,
Record as RaRecord,
OnSuccess,
OnFailure,
} from '../types';
import { RedirectionSideEffect } from '../sideEffect';
import { useDispatch } from 'react-redux';
import { setAutomaticRefresh } from '../actions/uiActions';
Expand Down Expand Up @@ -42,7 +43,7 @@ import { FormContextProvider } from './FormContextProvider';
*
* @param {Props} props
*/
const FormWithRedirect: FC<FormWithRedirectProps> = ({
const FormWithRedirect = ({
debug,
decorators,
defaultValue,
Expand All @@ -63,7 +64,7 @@ const FormWithRedirect: FC<FormWithRedirectProps> = ({
warnWhenUnsavedChanges,
sanitizeEmptyValues: shouldSanitizeEmptyValues = true,
...props
}) => {
}: FormWithRedirectProps) => {
const redirect = useRef(props.redirect);
const onSave = useRef(save);
const formGroups = useRef<{ [key: string]: string[] }>({});
Expand Down Expand Up @@ -165,6 +166,7 @@ const FormWithRedirect: FC<FormWithRedirectProps> = ({
validate={validate}
validateOnBlur={validateOnBlur}
render={formProps => (
// @ts-ignore Ignored because of a weird error about the active prop
<FormView
{...props}
{...formProps}
Expand All @@ -182,23 +184,28 @@ const FormWithRedirect: FC<FormWithRedirectProps> = ({
};

export type FormWithRedirectProps = FormWithRedirectOwnProps &
Omit<FormProps, 'onSubmit' | 'active'>;
Omit<FormProps, 'onSubmit'>;

export type FormWithRedirectRenderProps = Omit<
FormViewProps,
'chilren' | 'render' | 'setRedirect'
>;
export type FormWithRedirectRender = (
props: Omit<FormViewProps, 'render' | 'setRedirect'>
props: FormWithRedirectRenderProps
) => React.ReactElement<any, any>;

export type FormWithRedirectSave = (
data: Partial<Record>,
data: Partial<RaRecord>,
redirectTo: RedirectionSideEffect,
options?: {
onSuccess?: OnSuccess;
onFailure?: OnFailure;
}
) => void;

export interface FormWithRedirectOwnProps {
defaultValue?: any;
record?: Record;
record?: RaRecord;
redirect?: RedirectionSideEffect;
render: FormWithRedirectRender;
save?: FormWithRedirectSave;
Expand All @@ -221,18 +228,18 @@ export type HandleSubmitWithRedirect = (
) => void;
interface FormViewProps
extends FormWithRedirectOwnProps,
Omit<FinalFormFormRenderProps, 'render' | 'active'> {
Omit<FormRenderProps, 'render' | 'component'> {
handleSubmitWithRedirect?: HandleSubmitWithRedirect;
setRedirect: SetRedirect;
warnWhenUnsavedChanges?: boolean;
}

const FormView: FC<FormViewProps> = ({
const FormView = ({
render,
warnWhenUnsavedChanges,
setRedirect,
...props
}) => {
}: FormViewProps) => {
// if record changes (after a getOne success or a refresh), the form must be updated
useInitializeFormWithRecord(props.record);
useWarnWhenUnsavedChanges(warnWhenUnsavedChanges);
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FormField from './FormField';
import FormWithRedirect, {
FormWithRedirectProps,
FormWithRedirectRender,
FormWithRedirectRenderProps,
FormWithRedirectSave,
HandleSubmitWithRedirect,
} from './FormWithRedirect';
Expand All @@ -30,6 +31,7 @@ export type {
FormDataConsumerRender,
FormDataConsumerRenderParams,
FormWithRedirectProps,
FormWithRedirectRenderProps,
FormWithRedirectRender,
FormWithRedirectSave,
HandleSubmitWithRedirect,
Expand Down
9 changes: 2 additions & 7 deletions packages/ra-ui-materialui/src/form/SimpleFormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Children, FC, ReactElement } from 'react';
import classnames from 'classnames';
import FormInput from './FormInput';
import PropTypes from 'prop-types';
import { FormRenderProps } from 'react-final-form';
import { MutationMode, Record, RedirectionSideEffect } from 'ra-core';
import { FormWithRedirectRenderProps, MutationMode, Record } from 'ra-core';
import Toolbar from './Toolbar';
import CardContentInner from '../layout/CardContentInner';

Expand Down Expand Up @@ -97,18 +96,14 @@ SimpleFormView.defaultProps = {
component: CardContentInner,
};

export interface SimpleFormViewProps extends FormRenderProps {
export interface SimpleFormViewProps extends FormWithRedirectRenderProps {
basePath?: string;
className?: string;
component?: React.ComponentType<any>;
handleSubmitWithRedirect?: (redirectTo: RedirectionSideEffect) => void;
margin?: 'none' | 'normal' | 'dense';
mutationMode?: MutationMode;
record?: Record;
redirect?: RedirectionSideEffect;
resource?: string;
save?: () => void;
saving?: boolean;
toolbar?: ReactElement;
/** @deprecated use mutationMode: undoable instead */
undoable?: boolean;
Expand Down
11 changes: 3 additions & 8 deletions packages/ra-ui-materialui/src/form/TabbedFormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import classnames from 'classnames';
import { Route, useRouteMatch, useLocation } from 'react-router-dom';
import { Divider } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { FormRenderProps } from 'react-final-form';
import {
escapePath,
FormWithRedirectRenderProps,
MutationMode,
Record,
RedirectionSideEffect,
} from 'ra-core';
import Toolbar from './Toolbar';
import TabbedFormTabs, { getTabFullPath } from './TabbedFormTabs';
Expand All @@ -29,7 +28,6 @@ export const TabbedFormView = (props: TabbedFormViewProps) => {
children,
className,
classes: classesOverride,
form,
handleSubmit,
handleSubmitWithRedirect,
invalid,
Expand Down Expand Up @@ -183,18 +181,14 @@ TabbedFormView.defaultProps = {
toolbar: <Toolbar />,
};

export interface TabbedFormViewProps extends FormRenderProps {
export interface TabbedFormViewProps extends FormWithRedirectRenderProps {
basePath?: string;
classes?: ClassesOverride<typeof useTabbedFormViewStyles>;
className?: string;
margin?: 'none' | 'normal' | 'dense';
mutationMode?: MutationMode;
handleSubmitWithRedirect?: (redirectTo: RedirectionSideEffect) => void;
record?: Record;
redirect?: RedirectionSideEffect;
resource?: string;
save?: () => void;
saving?: boolean;
syncWithLocation?: boolean;
tabs?: ReactElement;
toolbar?: ReactElement;
Expand All @@ -213,6 +207,7 @@ const sanitizeRestProps = ({
dirtySinceLastSubmit,
error,
errors,
form,
hasSubmitErrors,
hasValidationErrors,
initialValues,
Expand Down

0 comments on commit 233c7d2

Please sign in to comment.