@propellerads/table 3.0.0 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.tsx CHANGED
@@ -1,9 +1,158 @@
1
- import React from 'react';
2
- import {Column, TableProps} from 'react-table';
1
+ import React, {FunctionComponent, ReactElement, ReactNode} from 'react';
2
+ import {
3
+ Cell,
4
+ CellPropGetter,
5
+ Column,
6
+ FooterGroupPropGetter,
7
+ FooterPropGetter,
8
+ HeaderGroup,
9
+ HeaderGroupPropGetter,
10
+ Hooks,
11
+ Meta,
12
+ Row,
13
+ RowPropGetter,
14
+ TableBodyPropGetter,
15
+ TablePropGetter, TableState,
16
+ UsePaginationInstanceProps, UsePaginationOptions,
17
+ UsePaginationState,
18
+ UseRowSelectInstanceProps,
19
+ UseRowSelectState, UseSortByColumnProps, UseRowSelectRowProps,
20
+ UseSortByInstanceProps, UseSortByOptions,
21
+ UseTableOptions, HeaderPropGetter,
22
+ } from 'react-table';
23
+
24
+ export type DefaultObject = Record<string, string | number | unknown>;
3
25
 
4
26
  export type LoadingState = {
5
27
  loadingColumns?: Column[],
6
28
  loadingData: any[],
7
29
  }
8
30
 
31
+ export enum FOOTER_PLACEMENT {
32
+ TOP = 'top',
33
+ BOTTOM = 'bottom'
34
+ }
35
+
36
+ export type PaginationProps = {
37
+ paginationAmount?: string | ReactNode | ReactElement,
38
+ parentElementId: string,
39
+ labelPerPage?: string,
40
+ pageSizes?: Array<string | number>,
41
+ pageIndex: number,
42
+ perPage: number,
43
+ totalPages: number,
44
+ totalItems: number,
45
+ canNextPage: boolean,
46
+ canPreviousPage: boolean,
47
+ nextPageHandler: () => void,
48
+ previousPageHandler: () => void,
49
+ setPageSize: (size: number) => void,
50
+ gotoPage: (page: number) => void,
51
+ }
52
+
53
+ export type ControlledPagination = {
54
+ paginationAmount?: string | ReactNode | ReactElement,
55
+ pageSize: number,
56
+ pageIndex: number,
57
+ pageCount: number,
58
+ }
59
+
60
+ export type TableProps = {
61
+ columns: Array<Column<DefaultObject>>,
62
+ data: [],
63
+ totalItems?: number,
64
+ PaginationComponent: React.FunctionComponent<PaginationProps>,
65
+ initialState: Partial<TableState<DefaultObject>>,
66
+ fetchData?: (params: { pageIndex: number, pageSize: number }) => void,
67
+ onSortedChange?: (id: string, isDesc: boolean) => void,
68
+ isLoading?: boolean,
69
+ footerPlacement?: Array<FOOTER_PLACEMENT>,
70
+ hasDefaultPagination?: boolean,
71
+ onSelectRowsChange?: (rows: Array<DefaultObject>) => void,
72
+ controlledPagination?: ControlledPagination,
73
+ loadingMessage?: string,
74
+ labelPerPage?: string,
75
+ parentElementId?: string,
76
+ tableContentId?: string,
77
+ getRowPreProps?: (row: Row) => DefaultObject,
78
+ getTableProps?: TableGetter,
79
+ getHeaderGroupProps?: ElementGetter,
80
+ getHeaderProps?: ElementGetter,
81
+ getRowProps?: ElementGetter,
82
+ getCellProps?: ElementGetter,
83
+ getFooterProps?: ElementGetter,
84
+ getFooterGroupProps?: ElementGetter,
85
+ showLoadingState?: boolean,
86
+ LoadingCellComponent?: FunctionComponent<DefaultObject>,
87
+ noDataMessage?: string | ReactElement
88
+ }
89
+
90
+ export type TableOptions = {
91
+ columns: Array<Column<DefaultObject>>,
92
+ data: Array<DefaultObject>,
93
+ initialState: {
94
+ pageIndex?: number,
95
+ pageSize?: number,
96
+ },
97
+ disableSortRemove: boolean,
98
+ disableMultiSort: boolean,
99
+ disableMultiRemove: boolean,
100
+ manualSortBy?: boolean,
101
+ autoResetPage?: boolean,
102
+ manualPagination?: boolean,
103
+ pageCount?: number,
104
+ }
105
+
106
+ export type MetaProps = {
107
+ column?: HeaderGroup<DefaultObject>,
108
+ cell?: Cell,
109
+ }
110
+
111
+ export type BaseMeta = Meta<DefaultObject, MetaProps>
112
+
113
+ export type TableGetter = (props: DefaultObject) => DefaultObject;
114
+
115
+ export type ElementGetter = (props: DefaultObject, meta: BaseMeta) => DefaultObject;
116
+
117
+ export type ElementCellPropGetter = CellPropGetter<DefaultObject>
118
+
119
+ export type ElementRowPropGetter = RowPropGetter<DefaultObject>
120
+
121
+ export type ElementHeaderFooterPropGetter = FooterGroupPropGetter<DefaultObject> |
122
+ HeaderGroupPropGetter<DefaultObject> |
123
+ FooterPropGetter<DefaultObject>
124
+
125
+ export type GetTableContainerPropsGetter = (userGetter?: TableGetter, getter?: TableGetter) =>
126
+ TablePropGetter<DefaultObject> | TableBodyPropGetter<DefaultObject>
127
+
128
+ export type GetTableElementPropsGetter<T> = (userGetter?: ElementGetter, getter?: ElementGetter) => T
129
+
130
+ export type GetTableElementInternalPropsGetter =
131
+ (internalProps: DefaultObject, userGetter?: ElementGetter, getter?: ElementGetter) =>
132
+ HeaderPropGetter<DefaultObject>
133
+
134
+ export type ColumnWithSort = HeaderGroup<DefaultObject> & Partial<UseSortByColumnProps<DefaultObject>>
135
+
136
+ export type StandardColumn = Column<DefaultObject>
137
+
138
+ export type StandardRow = Row<DefaultObject>
139
+
140
+ export type SelectableRow = Row<{ id: string }> & UseRowSelectRowProps<DefaultObject>
141
+
142
+ export type StandardCell = Cell<DefaultObject>
143
+
144
+ export type SelectableRowInstanceProps = UseRowSelectInstanceProps<DefaultObject>
145
+
146
+ export type StandardHooks = Hooks<DefaultObject>
147
+
148
+ export type TableHooksInstanceProps =
149
+ UseSortByInstanceProps<DefaultObject> &
150
+ UsePaginationInstanceProps<DefaultObject> &
151
+ UseRowSelectInstanceProps<DefaultObject> &
152
+ { state: UseRowSelectState<DefaultObject> & UsePaginationState<DefaultObject>};
153
+
154
+ export type TableOptionsProps =
155
+ UseTableOptions<DefaultObject> & UseSortByOptions<DefaultObject> & UsePaginationOptions<DefaultObject> &
156
+ { initialState: Partial<TableState<DefaultObject>> & Partial<UsePaginationState<DefaultObject>> }
157
+
9
158
  export class Table extends React.Component<Partial<TableProps>> {}
@@ -1,40 +1,37 @@
1
- import React, {useMemo} from 'react';
1
+ import React, {FunctionComponent, useMemo} from 'react';
2
2
  import {Column} from 'react-table';
3
- import {LoadingState} from './types';
4
- import {EmptyStateCell} from './style';
3
+ import {DefaultObject, LoadingState} from './types';
5
4
 
6
5
  const useLoadingState = (
7
6
  showLoadingState: boolean,
8
7
  loading: boolean,
9
8
  pageSize: number,
10
- columns: Column[],
11
- LoadingCellComponent = (props: any) => <EmptyStateCell {...props} />,
9
+ columns: Column<DefaultObject>[],
10
+ LoadingCellComponent: FunctionComponent<DefaultObject>,
12
11
  ): LoadingState => {
13
- const loadingColumns: Column[] | undefined = useMemo(() => (showLoadingState && loading ? columns
12
+ const loadingColumns: Column<DefaultObject>[] | undefined = useMemo(() => (showLoadingState && loading ? columns
14
13
  .map(({maxWidth, width, Header}: Column, key: number) => {
15
- const column: Column = {
16
- Cell: LoadingCellComponent,
14
+ const column: Column<DefaultObject> = {
15
+ Cell: () => <LoadingCellComponent />,
17
16
  accessor: `empty_${key}`,
18
- filterable: false,
19
- sortable: false,
20
17
  };
21
18
  if (maxWidth) {
22
19
  column.maxWidth = maxWidth;
23
20
  }
24
21
  if (width) {
25
- column.maxWidth = width;
22
+ column.width = width;
26
23
  }
27
24
  if (Header) {
28
25
  column.Header = Header;
29
26
  }
30
27
 
31
28
  return column;
32
- }) : []), [LoadingCellComponent, columns, loading, showLoadingState]);
29
+ }) : []), [columns, loading, showLoadingState]);
33
30
 
34
31
  const loadingData = useMemo(() => {
35
32
  if (showLoadingState && loading && columns && columns.length > 0) {
36
- const dataObject: any = {};
37
- const dataArray: any[] = [];
33
+ const dataObject: DefaultObject = {};
34
+ const dataArray: DefaultObject[] = [];
38
35
 
39
36
  for (let i = 0; i < columns.length; i += 1) {
40
37
  dataObject[`empty_${i}`] = '';
@@ -0,0 +1,69 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-nocheck
3
+ import {useEffect} from 'react';
4
+
5
+ const TABLE_SHADOW_CLASS_NAME = {
6
+ LEFT: 'shadow_left',
7
+ RIGHT: 'shadow_right',
8
+ };
9
+
10
+ function changeClassName(tableWrapperNode: Element, newClassNameList: string[]) {
11
+ const {classList} = tableWrapperNode;
12
+
13
+ classList.remove(TABLE_SHADOW_CLASS_NAME.LEFT, TABLE_SHADOW_CLASS_NAME.RIGHT);
14
+ classList.add(...newClassNameList);
15
+ }
16
+
17
+ function calculateNewClassNames(tableNode, tableWrapperNode) {
18
+ if (!tableNode || !tableWrapperNode) {
19
+ return;
20
+ }
21
+
22
+ const tableRect = tableNode.getBoundingClientRect();
23
+ const tableWrapperRect = tableWrapperNode.getBoundingClientRect();
24
+ const newClassName = [];
25
+
26
+ const tableRectLeft = Math.floor(tableRect.left);
27
+ const tableRectRight = Math.floor(tableRect.right);
28
+ const tableWrapperRectLeft = Math.floor(tableWrapperRect.left);
29
+ const tableWrapperRectRight = Math.floor(tableWrapperRect.right);
30
+
31
+ if (tableRectLeft < tableWrapperRectLeft) {
32
+ newClassName.push(TABLE_SHADOW_CLASS_NAME.LEFT);
33
+ }
34
+
35
+ if (tableRectRight > tableWrapperRectRight) {
36
+ newClassName.push(TABLE_SHADOW_CLASS_NAME.RIGHT);
37
+ }
38
+
39
+ changeClassName(tableWrapperNode, newClassName);
40
+ }
41
+
42
+ export default (tableRef, tableWrapperRef) => {
43
+ useEffect(() => {
44
+ let ticking = false;
45
+
46
+ function onMousewheel() {
47
+ if (ticking) {
48
+ return;
49
+ }
50
+
51
+ window.requestAnimationFrame(() => {
52
+ calculateNewClassNames(tableRef.current, tableWrapperRef.current);
53
+ ticking = false;
54
+ });
55
+
56
+ ticking = true;
57
+ }
58
+
59
+ calculateNewClassNames(tableRef.current, tableWrapperRef.current);
60
+
61
+ tableRef?.current?.addEventListener('mousewheel', onMousewheel);
62
+ window.addEventListener('resize', onMousewheel);
63
+
64
+ return () => {
65
+ tableRef?.current?.removeEventListener('mousewheel', onMousewheel);
66
+ window.removeEventListener('resize', onMousewheel);
67
+ };
68
+ }, [tableRef.current, tableWrapperRef.current]);
69
+ };
package/src/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import {TableProps} from 'react-table';
2
-
3
- type TableProps = Partial<TableProps> & {
4
- showLoadingState?: boolean
5
- };