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

perf(react): 防止不必要的重新渲染 #2250

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React, { useState, useEffect } from 'react';
import { DataCell, S2Event, S2_PREFIX_CLS } from '@antv/s2';
import type { Event as CanvasEvent } from '@antv/g-canvas';
import type { ScrollOffset } from '@antv/s2';
import { DataCell, S2Event, S2_PREFIX_CLS } from '@antv/s2';
import { isEqual, pick } from 'lodash';
import type { Event as CanvasEvent } from '@antv/g-canvas';
import React, { memo, useCallback, useEffect, useState } from 'react';
import { useS2Event } from '../../../../hooks';
import { useSpreadSheetRef } from '../../../../utils/SpreadSheetContext';
import './drag-copy-point.less';
import { DragCopyMask } from './drag-copy-mask';
import './drag-copy-point.less';

export type DragCopyProps = {
onChange?: (val) => void;
};

export function DragCopyPoint(props: DragCopyProps) {
export const DragCopyPoint = memo((props: DragCopyProps) => {

Check warning on line 15 in packages/s2-react/src/components/sheets/editable-sheet/drag-copy/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/drag-copy/index.tsx#L15

Added line #L15 was not covered by tests
const spreadsheet = useSpreadSheetRef();

const [scroll, setScroll] = useState<
Expand Down Expand Up @@ -69,7 +69,7 @@
}
};

const fixPostiton = (event: CanvasEvent) => {
const fixPosition = (event: CanvasEvent) => {
const eventCell = event.target.cfg.parent;
const isEventCellSelected =
spreadsheet.interaction.isSelectedCell(eventCell);
Expand Down Expand Up @@ -123,17 +123,19 @@
}
}, [scroll, cell]);

/** 多选时隐藏拖拽点 */
const batchSelected = () => {
/**
* 多选时隐藏拖拽点
*/
const batchSelected = useCallback(() => {

Check warning on line 129 in packages/s2-react/src/components/sheets/editable-sheet/drag-copy/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/drag-copy/index.tsx#L129

Added line #L129 was not covered by tests
setCell(undefined);
};
}, []);

useS2Event(S2Event.COL_CELL_CLICK, batchSelected, spreadsheet);
useS2Event(S2Event.ROW_CELL_CLICK, batchSelected, spreadsheet);
useS2Event(S2Event.CORNER_CELL_CLICK, batchSelected, spreadsheet);
useS2Event(S2Event.DATA_CELL_BRUSH_SELECTION, batchSelected, spreadsheet);

useS2Event(S2Event.DATA_CELL_CLICK, fixPostiton, spreadsheet);
useS2Event(S2Event.DATA_CELL_CLICK, fixPosition, spreadsheet);

return (
<div
Expand All @@ -149,4 +151,4 @@
<DragCopyMask onCopyFinished={batchSelected} />
</div>
);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Input } from 'antd';
import { merge, pick } from 'lodash';
import React, {
memo,
useCallback,
useEffect,
useMemo,
Expand Down Expand Up @@ -38,7 +39,7 @@
) {
const { params, resolver } = props;
const spreadsheet = useSpreadSheetRef();
const { event, onChange, onDataCellEditEnd, CustomComponent } = params;

Check warning on line 42 in packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx#L42

Added line #L42 was not covered by tests
const cell: BaseCell<ViewMeta> = event.target.cfg.parent;

const { left, top, width, height } = useMemo(() => {
Expand Down Expand Up @@ -96,12 +97,12 @@
spreadsheet.render(true);

onDataCellEditEnd?.(
merge(cell.getMeta(), {
fieldValue: inputVal,

Check warning on line 101 in packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx#L100-L101

Added lines #L100 - L101 were not covered by tests
data: {
[valueField]: inputVal,
},
}),

Check warning on line 105 in packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/edit-cell/index.tsx#L104-L105

Added lines #L104 - L105 were not covered by tests
);

if (onChange) {
Expand Down Expand Up @@ -172,27 +173,25 @@
);
}

function EditCell({
onChange,
onDataCellEditEnd,
CustomComponent,
}: onChangeProps) {
const spreadsheet = useSpreadSheetRef();

const cb = useCallback(
(e: CanvasEvent) => {
invokeComponent(
EditCellComponent,
{ event: e, onChange, onDataCellEditEnd, CustomComponent },
spreadsheet,
);
},
[spreadsheet],
);
const EditCell = memo(
({ onChange, onDataCellEditEnd, CustomComponent }: onChangeProps) => {
const spreadsheet = useSpreadSheetRef();

const cb = useCallback(
(e: CanvasEvent) => {
invokeComponent(
EditCellComponent,
{ event: e, onChange, onDataCellEditEnd, CustomComponent },
spreadsheet,
);
},
[spreadsheet],
);

useS2Event(S2Event.DATA_CELL_DOUBLE_CLICK, cb, spreadsheet);
useS2Event(S2Event.DATA_CELL_DOUBLE_CLICK, cb, spreadsheet);

return null;
}
return null;
},
);

export { EditCell };
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react';
import React, { useCallback } from 'react';
import { BaseSheet } from '../base-sheet';
import type { SheetComponentsProps } from '../interface';
import { EditCell } from './edit-cell';
import { DragCopyPoint } from './drag-copy';
import { EditCell } from './edit-cell';

export const EditableSheet: React.FC<SheetComponentsProps> = React.memo(
(props) => {
const onChange = useCallback(() => {}, []);
return (
<BaseSheet {...props} sheetType={'table'}>
<EditCell
onChange={() => {}}
onChange={onChange}
onDataCellEditEnd={props.onDataCellEditEnd}

Check warning on line 14 in packages/s2-react/src/components/sheets/editable-sheet/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/s2-react/src/components/sheets/editable-sheet/index.tsx#L12-L14

Added lines #L12 - L14 were not covered by tests
/>
<DragCopyPoint />
</BaseSheet>
Expand Down
Loading