Skip to content

Commit 483999f

Browse files
authored
Use new array methods (#3770)
* Use new array methods * revert this
1 parent 6e2b7a4 commit 483999f

8 files changed

+12
-16
lines changed

src/DataGrid.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
672672
function updateRow(column: CalculatedColumn<R, SR>, rowIdx: number, row: R) {
673673
if (typeof onRowsChange !== 'function') return;
674674
if (row === rows[rowIdx]) return;
675-
const updatedRows = [...rows];
676-
updatedRows[rowIdx] = row;
675+
const updatedRows = rows.with(rowIdx, row);
677676
onRowsChange(updatedRows, {
678677
indexes: [rowIdx],
679678
column

src/TreeDataGrid.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function TreeDataGrid<R, SR = unknown, K extends Key = Key>({
7878
const groupIdGetter = rawGroupIdGetter ?? defaultGroupIdGetter;
7979

8080
const { columns, groupBy } = useMemo(() => {
81-
const columns = [...rawColumns].sort(({ key: aKey }, { key: bKey }) => {
81+
const columns = rawColumns.toSorted(({ key: aKey }, { key: bKey }) => {
8282
// Sort select column first:
8383
if (aKey === SELECT_COLUMN_KEY) return -1;
8484
if (bKey === SELECT_COLUMN_KEY) return 1;

website/routes/CommonFeatures.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ function CommonFeatures() {
337337
const sortedRows = useMemo((): readonly Row[] => {
338338
if (sortColumns.length === 0) return rows;
339339

340-
return [...rows].sort((a, b) => {
340+
return rows.toSorted((a, b) => {
341341
for (const sort of sortColumns) {
342342
const comparator = getComparator(sort.columnKey);
343343
const compResult = comparator(a, b);

website/routes/ContextMenu.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface Row {
3030
price: string;
3131
}
3232

33-
function createRows(): Row[] {
33+
function createRows(): readonly Row[] {
3434
const rows: Row[] = [];
3535

3636
for (let i = 1; i < 1000; i++) {
@@ -90,7 +90,7 @@ function ContextMenuDemo() {
9090
price: faker.commerce.price()
9191
};
9292

93-
setRows([...rows.slice(0, insertRowIdx), newRow, ...rows.slice(insertRowIdx)]);
93+
setRows(rows.toSpliced(insertRowIdx, 0, newRow));
9494
setNextId();
9595
}
9696

@@ -127,8 +127,7 @@ function ContextMenuDemo() {
127127
<button
128128
type="button"
129129
onClick={() => {
130-
const { rowIdx } = contextMenuProps;
131-
setRows([...rows.slice(0, rowIdx), ...rows.slice(rowIdx + 1)]);
130+
setRows(rows.toSpliced(contextMenuProps.rowIdx, 1));
132131
setContextMenuProps(null);
133132
}}
134133
>

website/routes/CustomizableRenderers.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function CustomizableRenderers() {
9292
const sortedRows = useMemo((): readonly Row[] => {
9393
if (sortColumns.length === 0) return rows;
9494

95-
return [...rows].sort((a, b) => {
95+
return rows.toSorted((a, b) => {
9696
for (const sort of sortColumns) {
9797
const comparator = getComparator(sort.columnKey);
9898
const compResult = comparator(a, b);

website/routes/RowGrouping.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ function RowGrouping() {
166166
}
167167
} else if (index !== -1) {
168168
setSelectedOptions((options) => {
169-
const newOptions = [...options];
170-
newOptions.splice(index, 1);
171-
return newOptions;
169+
return options.toSpliced(index, 1);
172170
});
173171
}
174172
setExpandedGroupIds(new Set());

website/routes/RowsReordering.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ function RowsReordering() {
6868
const renderRow = useCallback((key: React.Key, props: RenderRowProps<Row>) => {
6969
function onRowReorder(fromIndex: number, toIndex: number) {
7070
setRows((rows) => {
71-
const newRows = [...rows];
72-
newRows.splice(toIndex, 0, newRows.splice(fromIndex, 1)[0]);
71+
const row = rows[fromIndex];
72+
const newRows = rows.toSpliced(fromIndex, 1);
73+
newRows.splice(toIndex, 0, row);
7374
return newRows;
7475
});
7576
}

website/routes/TreeView.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ function toggleSubRow(rows: Row[], id: string): Row[] {
8181
const { children } = row;
8282
if (!children) return rows;
8383

84-
const newRows = [...rows];
85-
newRows[rowIndex] = { ...row, isExpanded: !row.isExpanded };
84+
const newRows = rows.with(rowIndex, { ...row, isExpanded: !row.isExpanded });
8685
if (row.isExpanded) {
8786
newRows.splice(rowIndex + 1, children.length);
8887
} else {

0 commit comments

Comments
 (0)