react-table#ensurePluginOrder JavaScript Examples

The following examples show how to use react-table#ensurePluginOrder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useExportData.js    From react-table-plugins with MIT License 5 votes vote down vote up
function useInstance(instance) {
  const {
    rows,
    initialRows = [],
    allColumns,
    disableExport,
    getExportFileName = defaultGetExportFileName,
    getExportFileBlob = defaultGetExportFileBlob,
    plugins,
  } = instance;

  ensurePluginOrder(
    plugins,
    ["useColumnOrder", "useColumnVisibility", "useFilters", "useSortBy"],
    "useExportData"
  );

  // Adding `canExport` & `exportValue` meta data
  allColumns.forEach((column) => {
    const {
      accessor,
      getColumnExportValue = defaultGetColumnExportValue,
    } = column;

    const canExport = accessor
      ? getFirstDefined(
          column.disableExport === true ? false : undefined,
          disableExport === true ? false : undefined,
          true
        )
      : false;

    column.canExport = canExport;
    column.exportValue = getColumnExportValue(column);
  });

  // This method will enable export of data on `instance` object
  const exportData = React.useCallback(
    (fileType, all = false) => {
      // Columns which are exportable
      const exportableColumns = allColumns.filter(
        (col) => col.canExport && (all || col.isVisible)
      );

      if (exportableColumns.length === 0) {
        console.warn("No exportable columns are available");
      }

      // Rows which are exportable
      let exportableRows = (all ? initialRows : rows).map((row) => {
        return exportableColumns.map((col) => {
          const { getCellExportValue = defaultGetCellExportValue } = col;

          return getCellExportValue(row, col);
        });
      });

      // Getting fileName
      const fileName = getExportFileName({ fileType, all });

      // Get `FileBlob` to download
      let fileBlob = getExportFileBlob({
        columns: exportableColumns,
        data: exportableRows,
        fileName,
        fileType,
      });

      // Trigger download in browser
      if (fileBlob) {
        downloadFileViaBlob(fileBlob, fileName, fileType);
      }
    },
    [getExportFileBlob, getExportFileName, initialRows, rows, allColumns]
  );

  Object.assign(instance, {
    exportData,
  });
}