как использовать CellMeasurer с таблицей для измерения высоты изображения - PullRequest
0 голосов
/ 17 июня 2019

Я пытаюсь понять, как использовать компонент CellMeasurer в таблице.У меня есть таблица рендеринга в порядке, но все изображения мгновенно извлекаются в зависимости от назначения пакета.

В примере в документации используются список и реквизит deferredMeasurementCache, но, насколько я могу судить, реквизит не существует на Table.

У меня есть коды andbox , иллюстрирующие проблему.

import ReactDOM from "react-dom";
import React from "react";
import { fromJS } from "immutable";
import "./style.css";
import {
  AutoSizer,
  Table,
  Column,
  SortIndicator,
  CellMeasurer,
  CellMeasurerCache
} from "react-virtualized";
import "react-virtualized/styles.css";
const images = [
  "https://images.unsplash.com/photo-1558980394-4c7c9299fe96?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1560681610-68f081d2e7dd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1556229010-6c3f2c9ca5f8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1560675770-ef24e89a6cef?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1556911073-52527ac43761?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1560671440-71f808b4cb50?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1560607985-67046623870e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
  "https://images.unsplash.com/photo-1560527341-725efe8887d9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"
];

const rows = fromJS(images.map((url, i) => ({ url, text: `image ${i + 1}` })));

function headerRenderer({ dataKey, label, sortBy, sortDirection }) {
  // eslint-disable-line
  return (
    <div>
      {label}
      {sortBy === dataKey && <SortIndicator sortDirection={sortDirection} />}
    </div>
  );
}

function Example({ messages }) {
  const cachRef = React.useRef(
    new CellMeasurerCache({
      minHeight: 100,
      defaultHeight: 150,
      defaultWidth: 250,
      fixedWidth: true
    })
  );
  return (
    <AutoSizer>
      {({ width, height }) => (
        <Table
          height={height}
          width={width}
          rowCount={rows.count()}
          rowGetter={({ index }) => rows.get(index)}
          headerHeight={40}
          deferredMeasurementCache={cachRef.current}
          rowHeight={cachRef.current.rowHeight}
        >
          <Column
            label="Image"
            width={250}
            dataKey="url"
            cellDataGetter={({ dataKey, rowData }) => rowData.get(dataKey)}
            cellRenderer={({
              cellData,
              columnIndex,
              key,
              parent,
              rowIndex,
              style
            }) => {
              return (
                <CellMeasurer
                  cache={cachRef.current}
                  columnIndex={columnIndex}
                  key={key}
                  parent={parent}
                  rowIndex={rowIndex}
                >
                  {({ measure }) => (
                    <div style={style}>
                      <img
                        alt="foobar"
                        width={200}
                        onLoad={measure}
                        src={cellData}
                      />
                    </div>
                  )}
                </CellMeasurer>
              );
            }}
            disableSort
          />
          <Column
            label="Text"
            width={200}
            dataKey="text"
            cellDataGetter={({ dataKey, rowData }) => rowData.get(dataKey)}
            flexGrow={1}
            headerRenderer={headerRenderer}
          />
        </Table>
      )}
    </AutoSizer>
  );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...