New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-table-context

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-table-context

Table Context Manager

latest
Source
npmnpm
Version
0.0.56
Version published
Maintainers
1
Created
Source

React Table Context

You can create your custom table component with the help of this component. This component helps you manage and edit table states.

Let us create and manage your table states. You just create the UI :)

Install

npm install react-table-context

yarn add react-table-context

Usage:

Create your table component:

import {TableProps, TableRecord, injectRouteParamsToValues} from "react-table-context";

const TableHeader: React.FC = () => {
  const {dispatch, state: {columns, sort}} = useTableContext();

  const handleOnClick = () => {
    const order = sort?.order == "asc" ? "desc" : "asc";
    dispatch({
      type: "set-sort",
      payload: {key: column.key as string, order}
    });
  };

  return <thead>
  <tr>
    {columns.map((column, key) => {
      if (column.type == "action") return <th/>;

      return <th onClick={handleOnClick}>{column.title}</th>;
    })}
  </tr>
  </thead>;
}

type Props<T extends TableRecord = TableRecord> = TableProps<T> & {
  data: T[],
  perPage?: number,
  total?: number;
  from?: number;
  to?: number;
};

const Table = <T extends TableRecord = TableRecord>(props: Props<T>) => {
  const {dispatch, state: {page, perPage, initialized}} = useTableContext<Content>();

  useEffect(() => {
    if (initialized) return;

    dispatch({
      type: "initialize",
      payload: {
        columns: props.columns,
        ...injectRouteParamsToValues({
          page,
          perPage: props.perPage || perPage,
        }),
      },
    });
  }, [initialized, dispatch]);

  useEffect(() => {
    if (props.data)
      dispatch({type: "set-data", payload: {data: props.data}});
  }, [props.data]);

  useEffect(() => {
    if (props.total && props.from && props.to) {
      dispatch({
        type: "set-pagination",
        payload: {
          total: props.total,
          from: props.from,
          to: props.to,
        },
      });
    }
  }, [props.total, props.from, props.to]);

  return <table>
    <TableHeader/>

    {/* ... */}
  </table>;
};

export default Table;

Content list table:

import {TableColumnType, useTableContext} from "react-table-context";

const columns: TableColumnType<Content>[] = [
  {title: "Title", key: "title", dataIndex: "title"},
];

const ContentListTable: React.Fc = () => {
  const {state: {sort, filters, initialized}} = useTableContext<Content>();
  const contentQuery = useContentsQuery({sort, filters, enabled: initialized});

  return <Table data={contentQuery.data ?? []} columns={columns}/>;
};

export default ContentListTable;

Use content list table:

<TableContextProvider>
  <ContentListTable/>
</TableContextProvider>

Use Table Context:

// T is your data type
const {state, dispatch} = useTableContext<T>();

Props

NameTypeDescription
columnsArray of type TableColumnType<T extends TableRecord> = { title: string; key: keyof T; dataIndex: keyof T; }key and dataIndex should be keyof T

State

Note: The state includes all props

NameTypeDescription
dataArray of type TableRecord = { id: number } & Record<string, unknown>TableRecord is default type. you should instance of TableRecord
selectednumber[] or undefinedSelected rows ids
isAllSelectedbooleanIf all rows are selected it should be true
sorttype TableSortType<T extends TableRecord> = { key: keyof T; order: TableSortOrder; }Sort state type. TableSortOrder is "asc" or "desc"
filtersArray of type TableFilterType<T extends TableRecord> = { key: keyof T; value: string or Record<string, unknown>; }
pagenumberCurrent page number. Is optional.
perPagenumber or undefinedPage size number. Is optional.
totalnumber or undefinedNumber of all rows. Is optional.
fromnumber or undefined
tonumber or undefined

Actions

TypePayload
set-data{ data: TableRecord[], selectableItemIds?: number[] }
set-selected{ ids: number[] }
toggle-selected{ id: number }
toggle-select-all
set-sortTableSortType
set-filterTableFilterType<T extends TableRecord = TableRecord>
set-filtersTableFilterType<T extends TableRecord = TableRecord>[]
go-to-page{ page: number }
next-page
prev-page
set-pagination{ page?: number; perPage?: number; total?: number, from?: number; to?: number }

Keywords

react

FAQs

Package last updated on 25 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts