Skip to content

Commit

Permalink
[core] Isolate selectors called without useGridSelector (#3774)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Feb 1, 2022
1 parent 9aea23b commit b249cbd
Show file tree
Hide file tree
Showing 73 changed files with 531 additions and 255 deletions.
347 changes: 283 additions & 64 deletions docs/pages/api-docs/data-grid/selectors.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/scripts/api/buildSelectorsDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface Selector {
category?: string;
deprecated?: string;
description?: string;
supportsApiRef?: boolean;
}

export default function buildSelectorsDocumentation(options: BuildSelectorsDocumentationOptions) {
Expand All @@ -45,6 +46,7 @@ export default function buildSelectorsDocumentation(options: BuildSelectorsDocum
const parameterSymbol = signature.getParameters()[0];

let isSelector = false;
let supportsApiRef = false;

if (
project.checker.getTypeOfSymbolAtLocation(
Expand All @@ -59,6 +61,7 @@ export default function buildSelectorsDocumentation(options: BuildSelectorsDocum
type.symbol.name === 'OutputSelector'
) {
isSelector = true;
supportsApiRef = true;
}

if (!isSelector) {
Expand All @@ -82,6 +85,7 @@ export default function buildSelectorsDocumentation(options: BuildSelectorsDocum
category,
deprecated,
description,
supportsApiRef,
};
})
.filter((el): el is Selector => !!el)
Expand Down
18 changes: 16 additions & 2 deletions docs/src/modules/components/SelectorsDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ export const SelectorExample = styled(HighlightedCode)(({ theme }) => ({
const SELECTOR_NAME_PATTERN = /^grid(.*)Selector/;

const SelectorAccordion = ({ selector }) => {
const signature = `${selector.name}: (apiRef: GridApiRef) => ${selector.returnType}`;
let signature = `${selector.name}: (state: GridState) => ${selector.returnType}`;
if (selector.supportsApiRef) {
signature = [
`${selector.name}: (apiRef: GridApiRef) => ${selector.returnType}`,
'// or',
`${selector.name}: (state: GridState, instanceId?: number) => ${selector.returnType}`,
].join('\n');
}

const match = selector.name.match(SELECTOR_NAME_PATTERN);
const valueName =
match == null ? 'value' : `${match[1][0].toLocaleLowerCase()}${match[1].slice(1)}`;

const example = `const ${valueName} = ${selector.name}(apiRef);`;
let example = `const ${valueName} = ${selector.name}(apiRef.current.state);`;
if (selector.supportsApiRef) {
example = [
`${selector.name}(apiRef)`,
'// or',
`${selector.name}(state, apiRef.current.instanceId)`,
].join('\n');
}

return (
<Accordion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import {
} from '@mui/x-data-grid';

const getRowsFromCurrentPage = ({ apiRef }) =>
gridPaginatedVisibleSortedGridRowIdsSelector(apiRef.current.state);
gridPaginatedVisibleSortedGridRowIdsSelector(apiRef);

const getUnfilteredRows = ({ apiRef }) =>
gridSortedRowIdsSelector(apiRef.current.state);
const getUnfilteredRows = ({ apiRef }) => gridSortedRowIdsSelector(apiRef);

const getFilteredRows = ({ apiRef }) =>
gridVisibleSortedRowIdsSelector(apiRef.current.state);
const getFilteredRows = ({ apiRef }) => gridVisibleSortedRowIdsSelector(apiRef);

const ExportIcon = createSvgIcon(
<path d="M19 12v7H5v-7H3v7c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2v-7h-2zm-6 .67l2.59-2.58L17 11.5l-5 5-5-5 1.41-1.41L11 12.67V3h2z" />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
} from '@mui/x-data-grid';

const getRowsFromCurrentPage = ({ apiRef }: GridCsvGetRowsToExportParams) =>
gridPaginatedVisibleSortedGridRowIdsSelector(apiRef.current.state);
gridPaginatedVisibleSortedGridRowIdsSelector(apiRef);

const getUnfilteredRows = ({ apiRef }: GridCsvGetRowsToExportParams) =>
gridSortedRowIdsSelector(apiRef.current.state);
gridSortedRowIdsSelector(apiRef);

const getFilteredRows = ({ apiRef }: GridCsvGetRowsToExportParams) =>
gridVisibleSortedRowIdsSelector(apiRef.current.state);
gridVisibleSortedRowIdsSelector(apiRef);

const ExportIcon = createSvgIcon(
<path d="M19 12v7H5v-7H3v7c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2v-7h-2zm-6 .67l2.59-2.58L17 11.5l-5 5-5-5 1.41-1.41L11 12.67V3h2z" />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import {
useGridApiContext,
} from '@mui/x-data-grid-pro';

const getRowsWithGroups = ({ apiRef }) =>
gridFilteredSortedRowIdsSelector(apiRef.current.state);
const getRowsWithGroups = ({ apiRef }) => gridFilteredSortedRowIdsSelector(apiRef);

const getRowsWithoutGroups = ({ apiRef }) => {
const rows = gridFilteredSortedRowIdsSelector(apiRef.current.state);
const tree = gridRowTreeSelector(apiRef.current.state);
const rows = gridFilteredSortedRowIdsSelector(apiRef);
const tree = gridRowTreeSelector(apiRef);

return rows.filter((rowId) => !tree[rowId].isAutoGenerated);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
} from '@mui/x-data-grid-pro';

const getRowsWithGroups = ({ apiRef }: GridCsvGetRowsToExportParams) =>
gridFilteredSortedRowIdsSelector(apiRef.current.state);
gridFilteredSortedRowIdsSelector(apiRef);

const getRowsWithoutGroups = ({ apiRef }: GridCsvGetRowsToExportParams) => {
const rows = gridFilteredSortedRowIdsSelector(apiRef.current.state);
const tree = gridRowTreeSelector(apiRef.current.state);
const rows = gridFilteredSortedRowIdsSelector(apiRef);
const tree = gridRowTreeSelector(apiRef);

return rows.filter((rowId) => !tree[rowId].isAutoGenerated);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function RowGroupingSetChildrenExpansion() {
);

const toggleSecondRow = () => {
const rowIds = gridVisibleSortedRowIdsSelector(apiRef.current.state);
const rowIds = gridVisibleSortedRowIdsSelector(apiRef);

if (rowIds.length > 1) {
const rowId = rowIds[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function RowGroupingSetChildrenExpansion() {
);

const toggleSecondRow = () => {
const rowIds = gridVisibleSortedRowIdsSelector(apiRef.current.state);
const rowIds = gridVisibleSortedRowIdsSelector(apiRef);

if (rowIds.length > 1) {
const rowId = rowIds[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useKeepGroupingColumnsHidden = (apiRef, columns, initialModel, leafField)
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};

newModel.forEach((field) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useKeepGroupingColumnsHidden = (
React.useEffect(() => {
apiRef.current.subscribeEvent(GridEvents.rowGroupingModelChange, (newModel) => {
const columnVisibilityModel = {
...gridColumnVisibilityModelSelector(apiRef.current.state),
...gridColumnVisibilityModelSelector(apiRef),
};
newModel.forEach((field) => {
if (!prevModel.current.includes(field)) {
Expand Down
Loading

0 comments on commit b249cbd

Please sign in to comment.