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

[DataGrid] Add methods to import and export the state #3593

Merged
merged 39 commits into from Feb 2, 2022

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Jan 11, 2022

Closes #820

https://deploy-preview-3593--material-ui-x.netlify.app/components/data-grid/state/#save-and-restore-the-state

Public API

// Export all the exportable state
const state = apiRef.current.exportState();

// Import all the exportable state
apiRef.current.restoreState(state);

// Or import some parts of the exportable state
apiRef.current.restoreState({ pinning: state.pinning });

// Or pass the exportable state to the `prop.initialState`
<DataGrid initialState={state} />

Internals

Each hook with an initial state must also register two pre-processors:

  • exportState which takes a GridInitialState and add the initial state from this hook to it
  const stateExportPreProcessing = React.useCallback<
    GridPreProcessor<GridPreProcessingGroup.exportState>
  >(
    (prevState) => {
      return {
        ...prevState,
        pagination: {
          ...prevState.pagination,
          pageSize: gridPageSizeSelector(apiRef.current.state),
        },
      };
    },
    [apiRef],
  );
  • restoreState which takes the GridInitialState to restore and updates the state accordingly.
  const stateRestorePreProcessing = React.useCallback<
    GridPreProcessor<GridPreProcessingGroup.restoreState>
  >(
    (params, context) => {
      const pageSize = context.stateToRestore.pagination?.pageSize;
      if (pageSize != null) {
        apiRef.current.setState(setStatePageSize(pageSize));
      }
      return params;
    },
    [apiRef],
  );

This method can also return a callback that will be ran after all the initial states are restored. This can be usefull to apply the sorting / filtering for instance.

I don't use directly setFilterModel, setPageSize, etc... to avoid multiple forceUpdates which causes a lot of re-renders.

@flaviendelangle flaviendelangle marked this pull request as draft January 11, 2022 13:28
@flaviendelangle flaviendelangle added the component: data grid This is the name of the generic UI component, not the React module! label Jan 11, 2022
@flaviendelangle flaviendelangle self-assigned this Jan 11, 2022
@flaviendelangle flaviendelangle changed the title [DataGrid] Add methods to important and export the state [DataGrid] Add methods to import and export the state Jan 12, 2022
@mui-bot
Copy link

mui-bot commented Jan 17, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 1,098.7 1,921.2 1,193.3 1,399.42 334.703
Sort 100k rows ms 699.1 1,068.1 699.1 892.68 161.588
Select 100k rows ms 157.1 218.3 175.7 182.84 24.645
Deselect 100k rows ms 96 213.1 161 148.78 38.385

Generated by 🚫 dangerJS against 02e7534

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 18, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 18, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 19, 2022
@tielushko
Copy link

I support the @benjimac93 notion of having the order of columns saved as the part of initial state as well as the width of the individual column. It would be a very useful feature for our end users.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 28, 2022
@benjimac93
Copy link

Is there a plan to add support for the column orders in a subsequent release? For us, this would be one of the most important parts of state to save/restore

No, we did not start working on it. And as it is independent in our code from the save/restore process itself, we have interest in splitting the work to release a first version of the save/restore as soon as possible to have feedbacks.

Understood it's not in scope for the initial release of this feature. Is there a rough timeline for when it will be in scope in a future release though? Trying to decide if we use MUI Pro or Premium for our product rebuild or if we need to explore other options

@flaviendelangle
Copy link
Member Author

@benjimac93 we could probably try to release this feature by the end of Q1 as it is not a major peace of work.
But can't guarantee anything.

@benjimac93
Copy link

@benjimac93 we could probably try to release this feature by the end of Q1 as it is not a major peace of work. But can't guarantee anything.

I can work with that. Component is awesome as an aside !

Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3593 (comment)

Will this be done in this PR?

onSelect={handleSetActiveView}
/>
))}
<Divider />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is odd with this demo. Having the text input inside the list doesn't look very good. What about putting the input outside the menu, like in a toolbar?

image

Because of the way this variant works it appears misaligned when not fucused.

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not finalize this demo, as I wanted feedbacks about the global idea before spending to much times on the details.
But I agree that it is not 100% clean right now 👍

@flaviendelangle
Copy link
Member Author

Will this be done in this PR?

Yes I think it's better to do it, I will add it today or tomorrow

@flaviendelangle
Copy link
Member Author

@m4theushw I added the default value stripping
And modified the pinned columns state init to handle the control model synchronously and storing the initial model given by the user as provided in state.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 1, 2022
@github-actions
Copy link

github-actions bot commented Feb 1, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 1, 2022
Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The demo still requires some work, but I'm approving for the API which is good.

image

Looking at the feedback received, what users are really asking is to restore the order of the columns. What about keeping the docs commented (demos included) until we add support for this part?

@flaviendelangle
Copy link
Member Author

@m4theushw I can merge this one without the doc visible and start working on the columns size and order yes.

@flaviendelangle flaviendelangle merged commit 850487b into mui:master Feb 2, 2022
@flaviendelangle flaviendelangle deleted the state-import-export branch February 2, 2022 10:16
@flaviendelangle flaviendelangle mentioned this pull request Feb 3, 2022
@gminova
Copy link

gminova commented Feb 10, 2022

@flaviendelangle Great work and so happy this is merged now! I was wondering why is the scrollPosition not part of the state? and is there a way to set an initialScrollPosition prop for the grid just like there is an initialState prop?

@flaviendelangle
Copy link
Member Author

The scroll position is not on our internal state at all because it is too volatile.
Each state update is supposed to cause a re-render and we don't want to re-render on scroll.

What is your use-case exactly ? Would you like to scroll to a position in pixel or to a given row on mount ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Add methods to save and restore the state
7 participants