커스텀 본문 렌더링
커스텀 본문 렌더링 (Custom body rendering)
아주 고급 사용 사례를 위해 renderCustomGridBody prop을 사용하면 그리드 본문 렌더링을 완전히 제어할 수 있어요. 기본 가상화(virtualized) 렌더링이 필요하지 않을 때나, 커스텀 행 레이아웃(아래의 조건부 행 세부사항 셀 등)이 필요한 상황에서 유용해요.
출처: 문서
본문
아주 고급 사용 사례를 위해 renderCustomGridBody prop을 사용하면 그리드 본문 렌더링을 완전히 제어할 수 있어요. 기본 가상화(virtualized) 렌더링이 필요하지 않은 시나리오나, 커스텀 행 레이아웃(아래의 조건부 행 세부사항 셀처럼)이 필요한 시나리오에서 유용해요.
이 prop은 일종의 탈출구(escape hatch) 역할을 하므로, 정확히 무엇을 하는지 알고 있을 때만 사용해야 해요. 커스텀 렌더러를 사용하면 그리드가 데이터 그리드 스펙에서 요구하는 모든 올바른 시맨틱 및 aria 레이블을 갖추고, 키보드 포커스와 탐색이 접근 가능한 방식으로 계속 작동하도록 보장해야 하는 책임이 당신에게 있어요.
커스텀 가상화 (Custom virtualization)
제한된 그리드 높이 (Constrained grid height)
Body 컴포넌트 · Cell 컴포넌트 · Data grid 컴포넌트
import React, { useEffect, useRef, memo } from 'react';
import { css } from '@emotion/react';
import {
EuiDataGridCustomBodyProps,
useEuiTheme,
logicalCSS,
} from '@elastic/eui';
import { raw_data } from './data_columns_cells';
import CustomEuiDataGrid from './data_grid';
export const CustomUnvirtualizedGridBody = memo(
({
Cell,
visibleColumns,
visibleRowData,
setCustomGridBodyProps,
headerRow,
footerRow,
}: EuiDataGridCustomBodyProps) => {
// 올바르게 페이지네이션된 행을 표시하는지 확인
const visibleRows = raw_data.slice(
visibleRowData.startRow,
visibleRowData.endRow
);
// 커스텀 그리드 본문 행에 필요한 스타일 적용
const { euiTheme } = useEuiTheme();
const styles = {
row: css`
${logicalCSS('width', 'fit-content')};
${logicalCSS('border-bottom', euiTheme.border.thin)};
background-color: ${euiTheme.colors.emptyShade};
`,
rowCellsWrapper: css`
display: flex;
`,
rowDetailsWrapper: css`
/* EuiDataGrid의 기본 스타일을 덮어쓰려면 추가 특이성이 필요함 */
&& .euiDataGridRowCell__content {
display: block;
padding: 0;
}
`,
};
// 커스텀 props를 그리드 본문 래퍼에 설정
const bodyRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
setCustomGridBodyProps({
ref: bodyRef,
onScroll: () => console.debug('scrollTop:', bodyRef.current?.scrollTop),
});
}, [setCustomGridBodyProps]);
return (
<>
{headerRow}
{visibleRows.map((row, rowIndex) => (
<div role="row" css={styles.row} key={rowIndex}>
<div css={styles.rowCellsWrapper}>
{visibleColumns.map((column, colIndex) => {
// 행 세부사항 셀은 건너뜀 - flex 래퍼 밖에서 수동으로 렌더링
if (column.id !== 'row-details') {
return (
<Cell
colIndex={colIndex}
visibleRowIndex={rowIndex}
key={`${rowIndex},${colIndex}`}
/>
);
}
})}
</div>
<div css={styles.rowDetailsWrapper}>
<Cell
colIndex={visibleColumns.length - 1} // 행이 표시되면 항상 마지막 인덱스여야 함
visibleRowIndex={rowIndex}
/>
</div>
</div>
))}
{footerRow}
</>
);
}
);
CustomUnvirtualizedGridBody.displayName = 'CustomUnvirtualizedGridBody';
export default ({ autoHeight }: { autoHeight: boolean }) => (
<CustomEuiDataGrid
renderCustomGridBody={CustomUnvirtualizedGridBody}
height={autoHeight ? undefined : 500}
/>
);
Props (속성)
- EuiDataGridCustomBodyProps