Skip to content

Commit

Permalink
Add eslint-plugin-react-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmahajan7 committed Oct 24, 2024
1 parent 706019a commit 8438db8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tsParser from '@typescript-eslint/parser';
import vitest from '@vitest/eslint-plugin';
import jestDom from 'eslint-plugin-jest-dom';
import react from 'eslint-plugin-react';
import reactCompiler from 'eslint-plugin-react-compiler';
import reactHooks from 'eslint-plugin-react-hooks';
import reactHooksExtra from 'eslint-plugin-react-hooks-extra';
import sonarjs from 'eslint-plugin-sonarjs';
Expand All @@ -18,6 +19,7 @@ export default [

plugins: {
react,
'react-compiler': reactCompiler,
'react-hooks': fixupPluginRules(reactHooks),
'react-hooks-extra': reactHooksExtra,
sonarjs,
Expand Down Expand Up @@ -372,6 +374,10 @@ export default [
'react/style-prop-object': 0,
'react/void-dom-elements-no-children': 1,

// React Compiler
// https://react.dev/learn/react-compiler#installing-eslint-plugin-react-compiler
'react-compiler/react-compiler': 1,

// React Hooks
// https://www.npmjs.com/package/eslint-plugin-react-hooks
'react-hooks/rules-of-hooks': 1,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"eslint": "^9.13.0",
"eslint-plugin-jest-dom": "^5.0.1",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-compiler": "^19.0.0-beta-8a03594-20241020",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-hooks-extra": "^1.15.0",
"eslint-plugin-sonarjs": "^2.0.4",
Expand Down
19 changes: 11 additions & 8 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -917,18 +917,22 @@ function DataGrid<R, SR, K extends Key>(
);
}

function cancelEditing() {
setSelectedPosition(({ idx, rowIdx }) => ({ idx, rowIdx, mode: 'SELECT' }));
}

function closeEditor(shouldFocusCell: boolean) {
shouldFocusCellRef.current = shouldFocusCell;
cancelEditing();
}

function getCellEditor(rowIdx: number) {
if (selectedPosition.rowIdx !== rowIdx || selectedPosition.mode === 'SELECT') return;

const { idx, row } = selectedPosition;
const column = columns[idx];
const colSpan = getColSpan(column, lastFrozenColumnIndex, { type: 'ROW', row });

const closeEditor = (shouldFocusCell: boolean) => {
shouldFocusCellRef.current = shouldFocusCell;
setSelectedPosition(({ idx, rowIdx }) => ({ idx, rowIdx, mode: 'SELECT' }));
};

const onRowChange = (row: R, commitChanges: boolean, shouldFocusCell: boolean) => {
if (commitChanges) {
// Prevents two issues when editor is closed by clicking on a different cell
Expand All @@ -946,7 +950,7 @@ function DataGrid<R, SR, K extends Key>(

if (rows[selectedPosition.rowIdx] !== selectedPosition.originalRow) {
// Discard changes if rows are updated from outside
closeEditor(false);
cancelEditing();
}

return (
Expand Down Expand Up @@ -1061,7 +1065,6 @@ function DataGrid<R, SR, K extends Key>(
// Reset the positions if the current values are no longer valid. This can happen if a column or row is removed
if (selectedPosition.idx > maxColIdx || selectedPosition.rowIdx > maxRowIdx) {
setSelectedPosition({ idx: -1, rowIdx: minRowIdx - 1, mode: 'SELECT' });
setDraggedOverRowIdx(undefined);
}

let templateRows = `repeat(${headerRowsCount}, ${headerRowHeight}px)`;
Expand Down Expand Up @@ -1245,7 +1248,7 @@ function DataGrid<R, SR, K extends Key>(
<ScrollToCell
scrollToPosition={scrollToPosition}
setScrollToCellPosition={setScrollToPosition}
gridElement={gridRef.current!}
gridRef={gridRef}
/>
)}
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/ScrollToCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface PartialPosition {

export default function ScrollToCell({
scrollToPosition: { idx, rowIdx },
gridElement,
gridRef,
setScrollToCellPosition
}: {
scrollToPosition: PartialPosition;
gridElement: HTMLDivElement;
gridRef: React.RefObject<HTMLDivElement | null>;
setScrollToCellPosition: (cell: null) => void;
}) {
const ref = useRef<HTMLDivElement>(null);
Expand All @@ -31,7 +31,7 @@ export default function ScrollToCell({
}

const observer = new IntersectionObserver(removeScrollToCell, {
root: gridElement,
root: gridRef.current!,
threshold: 1.0
});

Expand All @@ -40,7 +40,7 @@ export default function ScrollToCell({
return () => {
observer.disconnect();
};
}, [gridElement, setScrollToCellPosition]);
}, [gridRef, setScrollToCellPosition]);

return (
<div
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useViewportColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function useViewportColumns<R, SR>({

const updateStartIdx = (colIdx: number, colSpan: number | undefined) => {
if (colSpan !== undefined && colIdx + colSpan > colOverscanStartIdx) {
// eslint-disable-next-line react-compiler/react-compiler
startIdx = colIdx;
return true;
}
Expand Down
17 changes: 10 additions & 7 deletions website/routes/CommonFeatures.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,23 @@ function rowKeyGetter(row: Row) {
return row.id;
}

let countries: string[] = [];

function createRows(): readonly Row[] {
const now = Date.now();
const rows: Row[] = [];
const allCountries = [];

for (let i = 0; i < 1000; i++) {
const country = faker.location.country();
allCountries.push(country);

rows.push({
id: i,
title: `Task #${i + 1}`,
client: faker.company.name(),
area: faker.person.jobArea(),
country: faker.location.country(),
country,
contact: faker.internet.exampleEmail(),
assignee: faker.person.fullName(),
progress: Math.random() * 100,
Expand All @@ -270,6 +276,8 @@ function createRows(): readonly Row[] {
});
}

countries = [...new Set(allCountries)].sort(new Intl.Collator().compare);

return rows;
}

Expand Down Expand Up @@ -313,12 +321,7 @@ function CommonFeatures() {
const [selectedRows, setSelectedRows] = useState((): ReadonlySet<number> => new Set());
const [isExporting, setIsExporting] = useState(false);
const gridRef = useRef<DataGridHandle>(null);

const countries = useMemo((): readonly string[] => {
return [...new Set(rows.map((r) => r.country))].sort(new Intl.Collator().compare);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const columns = useMemo(() => getColumns(countries, direction), [countries, direction]);
const columns = useMemo(() => getColumns(countries, direction), [direction]);

const summaryRows = useMemo((): readonly SummaryRow[] => {
return [
Expand Down

0 comments on commit 8438db8

Please sign in to comment.