Skip to content

Commit

Permalink
Merge pull request #3577 from marmelab/form-perf
Browse files Browse the repository at this point in the history
[RFR] Improve form performance
  • Loading branch information
fzaninotto authored Aug 23, 2019
2 parents bcc8772 + 96aa93b commit 13d3027
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
14 changes: 7 additions & 7 deletions examples/simple/src/posts/PostCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
useRedirect,
useNotify,
} from 'react-admin'; // eslint-disable-line import/no-unresolved
import { useFormState } from 'react-final-form';
import { useFormState, FormSpy } from 'react-final-form';

const SaveWithNoteButton = props => {
const [create] = useCreate('posts');
Expand Down Expand Up @@ -128,17 +128,17 @@ const PostCreate = ({ permissions, ...props }) => {
<TextInput autoFocus source="title" />
<TextInput source="teaser" fullWidth={true} multiline={true} />
<RichTextInput source="body" validate={required()} />
<FormDataConsumer>
{({ formData, ...rest }) =>
formData.title && (
<FormSpy subscription={{ values: true }}>
{({ values }) =>
values.title ? (
<NumberInput
source="average_note"
defaultValue={5}
{...rest}
/>
)
) : null
}
</FormDataConsumer>
</FormSpy>

<DateInput
source="published_at"
defaultValue={dateDefaultValue}
Expand Down
8 changes: 8 additions & 0 deletions packages/ra-ui-materialui/src/form/SimpleForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SimpleForm = ({ initialValues, ...props }) => {
mutators={{ ...arrayMutators }}
keepDirtyOnReinitialize
destroyOnUnregister
subscription={defaultSubscription}
{...props}
render={({ submitting, ...formProps }) => (
<SimpleFormView
Expand All @@ -55,6 +56,13 @@ const SimpleForm = ({ initialValues, ...props }) => {
);
};

const defaultSubscription = {
submitting: true,
pristine: true,
valid: true,
invalid: true,
};

export default SimpleForm;

const SimpleFormView = ({
Expand Down
8 changes: 8 additions & 0 deletions packages/ra-ui-materialui/src/form/TabbedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const TabbedForm = ({ initialValues, ...props }) => {
setRedirect={setRedirect}
keepDirtyOnReinitialize
destroyOnUnregister
subscription={defaultSubscription}
{...props}
render={({ submitting, ...formProps }) => (
<TabbedFormView
Expand All @@ -64,6 +65,13 @@ const TabbedForm = ({ initialValues, ...props }) => {
);
};

const defaultSubscription = {
submitting: true,
pristine: true,
valid: true,
invalid: true,
};

export default withRouter(TabbedForm);

export const TabbedFormView = ({
Expand Down

0 comments on commit 13d3027

Please sign in to comment.