Skip to content

Commit de086c9

Browse files
Add editorOptions.closeOnExternalRowChange prop (adazzle#3759)
Add `editorOptions.closeOnExternalRowChange` prop Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
1 parent c4f4de8 commit de086c9

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/DataGrid.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
904904
const { idx, row } = selectedPosition;
905905
const column = columns[idx];
906906
const colSpan = getColSpan(column, lastFrozenColumnIndex, { type: 'ROW', row });
907+
const closeOnExternalRowChange = column.editorOptions?.closeOnExternalRowChange ?? true;
907908

908909
const closeEditor = (shouldFocusCell: boolean) => {
909910
setShouldFocusCell(shouldFocusCell);
@@ -925,7 +926,10 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
925926
}
926927
};
927928

928-
if (rows[selectedPosition.rowIdx] !== selectedPosition.originalRow) {
929+
if (
930+
closeOnExternalRowChange &&
931+
rows[selectedPosition.rowIdx] !== selectedPosition.originalRow
932+
) {
929933
// Discard changes if rows are updated from outside
930934
closeEditor(false);
931935
}

src/EditCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function EditCell<R, SR>({
5757
navigate
5858
}: EditCellProps<R, SR>) {
5959
const frameRequestRef = useRef<number>(undefined);
60-
const commitOnOutsideClick = column.editorOptions?.commitOnOutsideClick !== false;
60+
const commitOnOutsideClick = column.editorOptions?.commitOnOutsideClick ?? true;
6161

6262
// We need to prevent the `useEffect` from cleaning up between re-renders,
6363
// as `onWindowCaptureMouseDown` might otherwise miss valid mousedown events.

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export interface Column<TRow, TSummaryRow = unknown> {
6363
readonly displayCellContent?: Maybe<boolean>;
6464
/** @default true */
6565
readonly commitOnOutsideClick?: Maybe<boolean>;
66+
/** @default true */
67+
readonly closeOnExternalRowChange?: Maybe<boolean>;
6668
}>;
6769
}
6870

test/browser/column/renderEditCell.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ describe('Editor', () => {
217217
await userEvent.keyboard('a{arrowleft}b{arrowright}c{arrowdown}'); // should commit changes on arrowdown
218218
expect(getCellsAtRowIndex(0)[1]).toHaveTextContent(/^a1bac$/);
219219
});
220+
221+
it('should close the editor when closeOnExternalRowChange is true or undefined and row is changed from outside', async () => {
222+
page.render(
223+
<EditorTest
224+
editorOptions={{
225+
// needed to prevent editor from closing on update button click
226+
commitOnOutsideClick: false
227+
}}
228+
/>
229+
);
230+
await userEvent.dblClick(getCellsAtRowIndex(0)[1]);
231+
const editor = page.getByRole('textbox', { name: 'col2-editor' });
232+
await expect.element(editor).toBeInTheDocument();
233+
await userEvent.click(page.getByRole('button', { name: 'update' }));
234+
await expect.element(editor).not.toBeInTheDocument();
235+
});
236+
237+
it('should not close the editor when closeOnExternalRowChange is false and row is changed from outside', async () => {
238+
page.render(
239+
<EditorTest
240+
editorOptions={{
241+
commitOnOutsideClick: false,
242+
closeOnExternalRowChange: false
243+
}}
244+
/>
245+
);
246+
await userEvent.dblClick(getCellsAtRowIndex(0)[1]);
247+
const editor = page.getByRole('textbox', { name: 'col2-editor' });
248+
await expect.element(editor).toBeInTheDocument();
249+
await userEvent.click(page.getByRole('button', { name: 'update' }));
250+
await expect.element(editor).toBeInTheDocument();
251+
});
220252
});
221253

222254
describe('editor focus', () => {
@@ -376,6 +408,14 @@ function EditorTest({
376408
<button type="button" onClick={() => onSave?.(rows)}>
377409
save
378410
</button>
411+
<button
412+
type="button"
413+
onClick={() => {
414+
setRows((rows) => rows.map((row) => ({ ...row })));
415+
}}
416+
>
417+
update
418+
</button>
379419
<DataGrid
380420
columns={columns}
381421
rows={rows}

0 commit comments

Comments
 (0)