) => void;
+};
+
+export const sorting: TableFeature = {
+ getDefaultOptions: (table) => ({
+ onSortingChange: (updater) =>
+ table.setState((prev) => ({
+ ...prev,
+ sorting: functionalUpdate(updater, prev.sorting),
+ })),
+ }),
+ getInitialState: (state) => ({
+ sorting: {},
+ ...state,
+ }),
+ init: (table) => {
+ table.setSorting = (updater) => table.options.onSortingChange?.(updater);
+ },
+ initColumn: (table, column) => {
+ column.canSort = () =>
+ (column.columnDef.enableSorting ?? true) &&
+ (table.options.enableSorting ?? true);
+ column.getSortingValue = () => table.getState().sorting?.[column.id];
+ column.setSort = (value) => {
+ if (column.canSort())
+ table.setSorting((prevState) => ({
+ ...prevState,
+ [column.id]: value,
+ }));
+ };
+ },
+};
diff --git a/packages/table/src/hooks/index.ts b/packages/table/src/hooks/index.ts
deleted file mode 100644
index f30d0fa..0000000
--- a/packages/table/src/hooks/index.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export * from "./useTable";
diff --git a/packages/table/src/hooks/usePagination.ts b/packages/table/src/hooks/usePagination.ts
deleted file mode 100644
index 02df54a..0000000
--- a/packages/table/src/hooks/usePagination.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { useMemo } from "react";
-
-const range = (start: number, end: number) => {
- let length = end - start + 1;
- return Array.from({ length }, (_, idx) => idx + start);
-};
-
-const usePagination = ({
- totalCount,
- pageSize,
- siblingCount = 3,
- currentPage = 0,
-}: {
- totalCount: number;
- pageSize: number;
- siblingCount?: number;
- currentPage: number;
-}) => {
- return useMemo(() => {
- const totalPageCount = Math.ceil(totalCount / pageSize);
-
- // first page
- if (currentPage < siblingCount) {
- const firstItem = Math.min(0, currentPage);
- const lastItem = Math.min(siblingCount * 2, totalPageCount);
- return { pageCount: totalPageCount, pages: range(firstItem, lastItem) };
- }
-
- // last pages
- if (totalPageCount - currentPage <= siblingCount) {
- const firstItem = Math.min(
- totalPageCount - siblingCount * 2,
- currentPage
- );
- return {
- pageCount: totalPageCount,
- pages: range(firstItem, totalPageCount - 1),
- };
- }
-
- const lastItem = Math.min(currentPage + siblingCount, totalPageCount);
- return {
- pageCount: totalPageCount,
- pages: range(currentPage - siblingCount, lastItem),
- };
- }, [totalCount, pageSize, siblingCount, currentPage]);
-};
-
-export default usePagination;
diff --git a/packages/table/src/hooks/useTable/index.ts b/packages/table/src/hooks/useTable/index.ts
deleted file mode 100644
index 5745c07..0000000
--- a/packages/table/src/hooks/useTable/index.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export { default as useTable } from "./useTable";
-export * from "./useTable.types";
diff --git a/packages/table/src/hooks/useTable/useTable.ts b/packages/table/src/hooks/useTable/useTable.ts
deleted file mode 100644
index 2402588..0000000
--- a/packages/table/src/hooks/useTable/useTable.ts
+++ /dev/null
@@ -1,202 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { useReducer, useState } from "react";
-import usePagination from "../usePagination";
-import type { TableColumn, TableFilter, TableSort } from "../../types";
-import type {
- IPaginationConfig,
- ISortConfig,
- IUseTableConfig,
- IUseTableData,
- TableState,
- UseTableAction,
-} from "./useTable.types";
-import { default as availableFilters } from "../../constants/filters";
-
-function tableReducer(state: TableState, action: UseTableAction) {
- const { type, payload } = action;
-
- switch (type) {
- case "updateCurrentPage":
- return { ...state, currentPage: payload };
- case "updatePageSize":
- return { ...state, pageSize: payload };
- case "toggleVisibleItem":
- return {
- ...state,
- visibleColumns: state.visibleColumns.includes(payload)
- ? state.visibleColumns.filter((c) => c !== payload)
- : [...state.visibleColumns, payload],
- };
- case "toggleMultipleVisibleItems":
- return {
- ...state,
- visibleColumns: payload.filter((c) => !c.hidden).map((c) => c.id),
- };
- case "toggleAllVisibleItems":
- return {
- ...state,
- visibleColumns: payload?.every((c) =>
- state.visibleColumns.includes(c.id)
- )
- ? []
- : payload?.map((c) => c.id),
- };
- case "setFilters":
- return {
- ...state,
- filters: payload,
- // reset expanded rows when filters change
- expandedRows: {},
- };
- case "resetAllFilters":
- return {
- ...state,
- filters: state.initialFilters ?? [],
- };
- case "setSort":
- return {
- ...state,
- sort: payload,
- };
- case "setExpandedRows":
- return {
- ...state,
- expandedRows: {
- ...state.expandedRows,
- [payload]: !state.expandedRows[payload],
- },
- };
-
- case "refresh":
- return { ...state, ...payload };
- default:
- return state;
- }
-}
-
-function useTable<
- P extends IPaginationConfig | undefined,
- S extends ISortConfig | undefined,
- E extends boolean
->(config: IUseTableConfig | null): IUseTableData
{
- const { pagination, columns, initialFilters, sorting, expandable } =
- config ?? {};
- const [prevConfig, setPrevConfig] = useState(config ?? null);
- const [
- { currentPage, pageSize, visibleColumns, filters, sort, expandedRows },
- dispatch,
- ] = useReducer(tableReducer, {
- currentPage: pagination?.initialPage || 0,
- pageSize: pagination?.pageSize || 10,
- visibleColumns: columns?.filter((c) => !c.hidden).map((c) => c.id) ?? [],
- filters: initialFilters ?? [],
- initialFilters: initialFilters ?? [],
- sort: sorting?.default ?? [],
- expandedRows: {},
- });
-
- if (JSON.stringify(config) !== JSON.stringify(prevConfig)) {
- setPrevConfig(config);
- dispatch({
- type: "refresh",
- payload: {
- pageSize: config?.pagination?.pageSize ?? 10,
- currentPage: config?.pagination?.initialPage ?? 0,
- visibleColumns:
- config?.columns?.filter((c) => !c.hidden).map((c) => c.id) ?? [],
- sort: [],
- },
- });
- }
-
- const { pages, pageCount } = usePagination({
- totalCount: pagination?.totalCount ?? 0,
- pageSize,
- currentPage,
- });
-
- let data = {};
-
- if (pagination) {
- data = {
- ...data,
- goToPage: (page: number) => {
- dispatch({ type: "updateCurrentPage", payload: page });
- pagination.onChange?.(page);
- },
- setPageSize: (size: number) =>
- dispatch({ type: "updatePageSize", payload: size }),
- pageCount,
- pages,
- pageSize,
- currentPage,
- paginationType: pagination.type,
- totalCount: pagination?.totalCount ?? 0,
- };
- }
-
- if (columns) {
- data = {
- ...data,
- filters,
- allColumns: columns.map((c) => ({
- ...c,
- toggleHide: () =>
- dispatch({ type: "toggleVisibleItem", payload: c.id }),
- hidden: !visibleColumns.includes(c.id),
- availableFilterOperators:
- c?.availableFilterOperators ??
- (c?.type && availableFilters?.[c.type]),
- })),
- toggleHide: (columns: TableColumn[]) =>
- dispatch({ type: "toggleMultipleVisibleItems", payload: columns }),
- toggleHideAll: () =>
- dispatch({ type: "toggleAllVisibleItems", payload: columns }),
- columns: columns
- .filter((c) => visibleColumns.includes(c.id))
- .map((column) => ({ ...column, sortable: column?.sortable ?? true })),
- setFilters: (filters: TableFilter[]) =>
- dispatch({ type: "setFilters", payload: filters }),
- resetAllFilters: () =>
- dispatch({ type: "resetAllFilters", payload: undefined }),
- };
-
- if (sorting) {
- data = {
- ...data,
- sort,
- sortable: !!config?.sorting?.sortable,
- setSort: (sort: TableSort[]) => dispatch({ type: "setSort", payload: sort }),
- sortType: sorting.type,
- };
- }
-
- if (expandable) {
- data = {
- ...data,
- expandedRows,
- onExpandRow: (id: number) =>
- dispatch({ type: "setExpandedRows", payload: id }),
- };
- }
- }
-
- return { tableProps: data, ...data } as IUseTableData
;
-}
-
-export default useTable;
diff --git a/packages/table/src/hooks/useTable/useTable.types.ts b/packages/table/src/hooks/useTable/useTable.types.ts
deleted file mode 100644
index 8609c6e..0000000
--- a/packages/table/src/hooks/useTable/useTable.types.ts
+++ /dev/null
@@ -1,130 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { TableColumn, TableExpandedState, TableFilter, TableSort } from "../../types";
-
-type AllColumns = Array<
- TableColumn & {
- toggleHide: () => void;
- hidden: boolean;
- }
->;
-
-type IPaginationConfig = {
- initialPage?: number;
- pageSize?: number;
- totalCount: number;
- siblingCount?: number;
- onChange?: (page: number) => void;
- type?: "custom";
-};
-
-type ISortConfig = { default?: TableSort[]; sortable?: boolean; type?: "custom" };
-
-interface IUseTableConfig {
- pagination?: Pagination;
- columns: TableColumn[];
- sorting?: ISortConfig;
- expandable?: Expandable;
- initialFilters?: TableFilter[];
-}
-
-interface IPaginationData {
- goToPage: (page: number) => void;
- setPageSize: (size: number) => void;
- pageSize: number;
- pageCount: number;
- pages: number[];
- currentPage: number;
- paginationType?: "custom";
- totalCount?: number;
-}
-
-type IColumnsData = {
- allColumns: AllColumns;
- toggleHideAll: () => void;
- toggleHide: (columns: TableColumn[]) => void;
- columns: Array>;
- resetAllFilters: () => void;
- setFilters: (filters: TableFilter[]) => void;
- filters: Array;
-};
-
-type ISortData = {
- sort: TableSort[];
- setSort: (sort: TableSort[]) => void;
- sortable: boolean;
- sortType?: "custom";
-};
-
-type IExpandableData = {
- expandedRows: TableExpandedState;
-};
-
-type IUseTableResult =
- (Pagination extends undefined ? undefined : IPaginationData) &
- (TableSort extends undefined ? undefined : ISortData) &
- (Expandable extends true ? IExpandableData : undefined) &
- IColumnsData;
-
-type IUseTableForwardedProps = IUseTableResult<
- Pagination,
- TableSort,
- Expandable
-> & {
- onExpandRow?: (index: number) => void;
-};
-
-type IUseTableData = {
- tableProps: IUseTableForwardedProps;
-} & IUseTableResult;
-
-type TableState = {
- currentPage: number;
- pageSize: number;
- visibleColumns: string[];
- filters: TableFilter[];
- sort: TableSort[];
- expandedRows: TableExpandedState;
- initialFilters: TableFilter[];
-};
-
-type UseTableAction =
- | {
- type: "updateCurrentPage" | "updatePageSize";
- payload: number;
- }
- | { type: "toggleVisibleItem"; payload: string }
- | { type: "toggleMultipleVisibleItems"; payload: TableColumn[] }
- | { type: "toggleAllVisibleItems"; payload: TableColumn[] }
- | { type: "refresh"; payload: Partial }
- | { type: "setFilters"; payload: TableFilter[] }
- | { type: "resetAllFilters"; payload: undefined }
- | { type: "setSort"; payload: TableSort[] }
- | { type: "setExpandedRows"; payload: number };
-
-export {
- IUseTableData,
- IUseTableConfig,
- IPaginationData,
- IPaginationConfig,
- ISortConfig,
- ISortData,
- TableState,
- UseTableAction,
- AllColumns,
- IUseTableForwardedProps,
-};
diff --git a/packages/table/src/index.ts b/packages/table/src/index.ts
index da0ad36..19a42c5 100644
--- a/packages/table/src/index.ts
+++ b/packages/table/src/index.ts
@@ -1,11 +1,11 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
+/*
+ * Copyright 2024 Arkemis S.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,6 +14,12 @@
* limitations under the License.
*/
-export * from "./components";
-export * from "./hooks";
+export * from "./use-table";
+
+export * from "./features/column-filtering";
+export * from "./features/column-visibility";
+export * from "./features/pagination";
+export * from "./features/row-selection";
+export * from "./features/sorting";
+
export * from "./types";
diff --git a/packages/table/src/types.ts b/packages/table/src/types.ts
new file mode 100644
index 0000000..f67f3e8
--- /dev/null
+++ b/packages/table/src/types.ts
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2024 Arkemis S.r.l.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import * as React from "react";
+import {
+ PaginationInstance,
+ PaginationOptions,
+ PaginationTableState,
+} from "./features/pagination";
+import {
+ ColumnVisibilityColumn,
+ ColumnVisibilityInstance,
+ ColumnVisibilityOptions,
+ ColumnVisibilityRow,
+ ColumnVisibilityTableState,
+} from "./features/column-visibility";
+import { BaseRow } from "./core/row";
+import { BaseCell } from "./core/cell";
+import {
+ ColumnFilteringColumn,
+ ColumnFilteringColumnDef,
+ ColumnFilteringInstance,
+ ColumnFilteringOptions,
+ ColumnFilteringTableState,
+} from "./features/column-filtering";
+import {
+ SortingColumn,
+ SortingColumnDef,
+ SortingInstance,
+ SortingOptions,
+ SortTableState,
+} from "./features/sorting";
+import {
+ RowSelectionInstance,
+ RowSelectionOptions,
+ RowSelectionRow,
+ RowSelectionTableState,
+} from "./features/row-selection";
+import {
+ ColumnPinningColumn,
+ ColumnPinningInstance,
+ ColumnPinningOptions,
+ ColumnPinningRow,
+ ColumnPinningTableState,
+} from "./features/column-pinning";
+import { BaseColumn } from "./core/column";
+
+export type TableState = PaginationTableState &
+ ColumnVisibilityTableState &
+ ColumnFilteringTableState &
+ SortTableState &
+ RowSelectionTableState &
+ ColumnPinningTableState;
+
+export type TableBaseOptions = {
+ columns: ColumnDef[];
+ state: Partial;
+ onStateChange: React.Dispatch>;
+ initialState?: Partial;
+ data: TData[];
+ getRowId?: (data: TData) => string;
+};
+
+export type TableResolvedOptions = TableBaseOptions &
+ PaginationOptions &
+ ColumnVisibilityOptions &
+ ColumnFilteringOptions &
+ SortingOptions &
+ RowSelectionOptions &
+ ColumnPinningOptions;
+
+export type TableOptions = Partial<
+ TableResolvedOptions
+>;
+
+export type TableBaseInstance = {
+ features: TableFeature[];
+ setOptions: React.Dispatch>>;
+ options: TableOptions;
+ initialState: TableState;
+ getState: () => TableState;
+ setState: React.Dispatch>;
+ getAllColumns: () => Column[];
+ getRows: () => Row[];
+};
+
+export type Table = TableBaseInstance &
+ PaginationInstance &
+ ColumnVisibilityInstance &
+ ColumnFilteringInstance &
+ SortingInstance &
+ RowSelectionInstance &
+ ColumnPinningInstance;
+
+export type ColumnConfig = Record;
+
+export type ColumnDef = SortingColumnDef &
+ ColumnFilteringColumnDef & {
+ id: string;
+ header?: string;
+ cell?: {
+ renderValue?: (value: TData) => React.ReactNode;
+ };
+ config?: ColumnConfig;
+ };
+
+export type Row = BaseRow &
+ ColumnVisibilityRow &
+ RowSelectionRow &
+ ColumnPinningRow;
+
+export type Cell = BaseCell;
+
+export type Column = BaseColumn &
+ ColumnVisibilityColumn &
+ ColumnFilteringColumn &
+ SortingColumn &
+ ColumnPinningColumn;
+
+export type TableFeature = {
+ getInitialState?: (state?: Partial) => Partial;
+ init: (table: Table) => void;
+ getDefaultOptions?: (
+ table: Table
+ ) => Partial>;
+ initColumn?: (table: Table, column: Column) => void;
+ initRow?: (table: Table, row: Row) => void;
+ initCell?: (
+ table: Table,
+ cell: Cell,
+ column: Column,
+ row: Row
+ ) => void;
+};
diff --git a/packages/table/src/types/action.ts b/packages/table/src/types/action.ts
deleted file mode 100644
index 23db141..0000000
--- a/packages/table/src/types/action.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { ReactNode } from "react";
-
-type Action = {
- content: ((data: Record) => ReactNode) | ReactNode;
- onClick?: (data: Record) => void;
-};
-
-export type { Action };
diff --git a/packages/table/src/types/column.ts b/packages/table/src/types/column.ts
deleted file mode 100644
index 4de0cd2..0000000
--- a/packages/table/src/types/column.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { CSSProperties, ReactNode } from "react";
-import { FilterOperator } from "./filters";
-
-export type TableColumn = {
- id: string;
- label: string;
- renderHeader?: () => string | number | ReactNode;
- render?: (
- data: Record,
- actions: { handleExpandRow: () => void }
- ) => string | number | ReactNode;
- className?: string;
- style?: CSSProperties;
- type?:
- | "boolean"
- | "date"
- | "datetime"
- | "float"
- | "integer"
- | "string"
- | "time";
- availableFilterOperators?: Array;
- sortable?: boolean;
- hidden?: boolean;
-};
diff --git a/packages/table/src/types/components.ts b/packages/table/src/types/components.ts
deleted file mode 100644
index 7575db3..0000000
--- a/packages/table/src/types/components.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { TableColumn } from "./column";
-import * as React from "react";
-
-type TableComponents = Partial<
- Record<
- string,
- (
- value: any,
- rowData: Record,
- column: TableColumn
- ) => React.ReactElement
- >
-> & {
- ExpandedRow?: (rowData: Record) => React.ReactElement;
- Table?: (props: React.HTMLAttributes) => JSX.Element | null;
- TableHeader?: (
- props: React.HTMLAttributes
- ) => JSX.Element | null;
- TableBody?: (
- props: React.HTMLAttributes
- ) => JSX.Element | null;
- TableRow?: (
- props: React.HTMLAttributes
- ) => JSX.Element | null;
- TableHead?: (
- props: React.ThHTMLAttributes
- ) => JSX.Element | null;
- TableCell?: (
- props: React.TdHTMLAttributes
- ) => JSX.Element | null;
- TableFooter?: (
- props: React.HTMLAttributes
- ) => JSX.Element | null;
-};
-
-export type { TableComponents };
diff --git a/packages/table/src/types/expandable.ts b/packages/table/src/types/expandable.ts
deleted file mode 100644
index 6afcefa..0000000
--- a/packages/table/src/types/expandable.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-type TableExpandedState = Record;
-
-export type { TableExpandedState };
diff --git a/packages/table/src/types/filters.ts b/packages/table/src/types/filters.ts
deleted file mode 100644
index 175ec56..0000000
--- a/packages/table/src/types/filters.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export enum FilterOperator {
- EQ = "eq",
- LE = "le",
- LT = "lt",
- GT = "gt",
- GE = "ge",
- NE = "ne",
- CONTAINS = "contains",
- ICONTAINS = "icontains",
- STARTSWITH = "startswith",
- ISTARTSWITH = "istartswith",
- ENDSWITH = "endswith",
- IENDSWITH = "iendswith",
- IN = "in",
- BETWEEN = "between",
-}
-
-export type TableFilter = {
- key: string;
- operator: FilterOperator;
- value: string | number;
-};
diff --git a/packages/table/src/types/index.ts b/packages/table/src/types/index.ts
deleted file mode 100644
index 99f7378..0000000
--- a/packages/table/src/types/index.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export * from "./action";
-export * from "./column";
-export * from "./components";
-export * from "./expandable";
-export * from "./filters";
-export * from "./sort";
-export * from "./table";
diff --git a/packages/table/src/types/sort.ts b/packages/table/src/types/sort.ts
deleted file mode 100644
index aed5c1c..0000000
--- a/packages/table/src/types/sort.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-type TableSort = {
- type: "asc" | "desc";
- key: string;
-};
-
-export { TableSort };
diff --git a/packages/table/src/types/table.ts b/packages/table/src/types/table.ts
deleted file mode 100644
index 7923711..0000000
--- a/packages/table/src/types/table.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { CSSProperties, ReactNode } from "react";
-import { TableColumn } from "./column";
-import { TableComponents } from "./components";
-import { IUseTableForwardedProps } from "../hooks";
-import { Action } from "./action";
-
-type ITableProps = {
- /**
- * Table Columns
- */
- columns: Omit[];
- data: Record[];
- actions?: ActionsConfig;
- noResult?: ReactNode;
- renderHeader?: (column: TableColumn) => ReactNode;
- components?: TableComponents;
-} & Partial>;
-
-type ActionsConfig = {
- label: string | ReactNode;
- position?: "start" | "end";
- className?: string;
- style?: CSSProperties;
- actions: Array;
-};
-
-export { ITableProps };
diff --git a/packages/table/src/use-table.tsx b/packages/table/src/use-table.tsx
new file mode 100644
index 0000000..894ea0a
--- /dev/null
+++ b/packages/table/src/use-table.tsx
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2024 Arkemis S.r.l.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import * as React from "react";
+import { TableOptions } from "./types";
+import { initTable } from "./core/table";
+
+export function useTable(options: TableOptions) {
+ const [table] = React.useState(() =>
+ initTable({
+ state: {},
+ onStateChange: () => {},
+ ...options,
+ })
+ );
+
+ const [state, setState] = React.useState(() => table.initialState);
+
+ table.setOptions((prev) => ({
+ ...prev,
+ ...options,
+ state: {
+ ...state,
+ ...options.state,
+ },
+ onStateChange: (state) => {
+ setState(state);
+ options.onStateChange?.(state);
+ },
+ }));
+
+ return table;
+}
diff --git a/packages/table/src/components/Pagination/Pagination.types.ts b/packages/table/src/utils/functional-update.ts
similarity index 63%
rename from packages/table/src/components/Pagination/Pagination.types.ts
rename to packages/table/src/utils/functional-update.ts
index e3e5064..09708aa 100644
--- a/packages/table/src/components/Pagination/Pagination.types.ts
+++ b/packages/table/src/utils/functional-update.ts
@@ -1,11 +1,11 @@
-/**
- * Copyright 2023 Arkemis S.r.l.
+/*
+ * Copyright 2024 Arkemis S.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,11 +14,12 @@
* limitations under the License.
*/
-interface IPaginationProps {
- pageCount: number;
- pages: number[];
- currentPage: number;
- onChange: (page: number) => void;
+import * as React from "react";
+export function functionalUpdate(
+ updater: React.SetStateAction,
+ input: T
+): T {
+ return typeof updater === "function"
+ ? (updater as (input: T) => T)(input)
+ : updater;
}
-
-export { IPaginationProps };
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b3ece02..35452bc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -25,7 +25,7 @@ importers:
version: 2.26.2
'@turbo/gen':
specifier: ^1.9.7
- version: 1.9.7(@types/node@17.0.12)(typescript@5.1.6)
+ version: 1.9.7(@types/node@18.16.19)(typescript@5.1.6)
eslint:
specifier: ^8.41.0
version: 8.41.0
@@ -141,6 +141,9 @@ importers:
'@arkejs/table':
specifier: workspace:table
version: link:../../packages/table
+ class-variance-authority:
+ specifier: ^0.7.0
+ version: 0.7.0
clsx:
specifier: ^2.1.0
version: 2.1.0
@@ -228,7 +231,7 @@ importers:
version: 5.1.6
vite:
specifier: ^4.4.0
- version: 4.4.4(@types/node@17.0.12)
+ version: 4.4.4(@types/node@18.16.19)
packages/table:
dependencies:
@@ -2743,7 +2746,7 @@ packages:
magic-string: 0.27.0
react-docgen-typescript: 2.2.2(typescript@5.1.6)
typescript: 5.1.6
- vite: 4.4.4(@types/node@17.0.12)
+ vite: 4.4.4(@types/node@18.16.19)
dev: true
/@jridgewell/gen-mapping@0.3.3:
@@ -4139,7 +4142,7 @@ packages:
magic-string: 0.30.1
rollup: 3.26.2
typescript: 5.1.6
- vite: 4.4.4(@types/node@17.0.12)
+ vite: 4.4.4(@types/node@18.16.19)
transitivePeerDependencies:
- encoding
- supports-color
@@ -4616,7 +4619,7 @@ packages:
react: 18.2.0
react-docgen: 7.0.3
react-dom: 18.2.0(react@18.2.0)
- vite: 4.4.4(@types/node@17.0.12)
+ vite: 4.4.4(@types/node@18.16.19)
transitivePeerDependencies:
- '@preact/preset-vite'
- encoding
@@ -4821,7 +4824,7 @@ packages:
resolution: {integrity: sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==}
dev: false
- /@turbo/gen@1.9.7(@types/node@17.0.12)(typescript@5.1.6):
+ /@turbo/gen@1.9.7(@types/node@18.16.19)(typescript@5.1.6):
resolution: {integrity: sha512-mjenROdRNvYxZixNfamJY/XxwKMT+01PuQby8+qdRUUa5ZLBPOv4rw11n+II1O8ZnpFpBJIGGFJcnRNY+lCddw==}
hasBin: true
dependencies:
@@ -4832,7 +4835,7 @@ packages:
minimatch: 9.0.3
node-plop: 0.26.3
semver: 7.5.4
- ts-node: 10.9.1(@types/node@17.0.12)(typescript@5.1.6)
+ ts-node: 10.9.1(@types/node@18.16.19)(typescript@5.1.6)
update-check: 1.5.4
validate-npm-package-name: 5.0.0
transitivePeerDependencies:
@@ -5384,7 +5387,7 @@ packages:
'@babel/plugin-transform-react-jsx-source': 7.22.5(@babel/core@7.22.9)
magic-string: 0.27.0
react-refresh: 0.14.0
- vite: 4.4.4(@types/node@17.0.12)
+ vite: 4.4.4(@types/node@18.16.19)
transitivePeerDependencies:
- supports-color
dev: true
@@ -5399,7 +5402,7 @@ packages:
'@babel/plugin-transform-react-jsx-self': 7.22.5(@babel/core@7.22.9)
'@babel/plugin-transform-react-jsx-source': 7.22.5(@babel/core@7.22.9)
react-refresh: 0.14.0
- vite: 4.4.4(@types/node@17.0.12)
+ vite: 4.4.4(@types/node@18.16.19)
transitivePeerDependencies:
- supports-color
dev: true
@@ -6337,6 +6340,12 @@ packages:
resolution: {integrity: sha512-eXTggHWSooYhq49F2opQhuHWgzucfF2YgODK4e1566GQs5BIfP30B0oenwBJHfWxAs2fyPB1s7Mg949zLf61Yw==}
engines: {node: '>=8'}
+ /class-variance-authority@0.7.0:
+ resolution: {integrity: sha512-jFI8IQw4hczaL4ALINxqLEXQbWcNjoSkloa4IaufXCJr6QawJyw7tuRysRsrE8w2p/4gGaxKIt/hX3qz/IbD1A==}
+ dependencies:
+ clsx: 2.0.0
+ dev: false
+
/clean-stack@2.2.0:
resolution: {integrity: sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==}
engines: {node: '>=6'}
@@ -6407,6 +6416,11 @@ packages:
resolution: {integrity: sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==}
engines: {node: '>=0.8'}
+ /clsx@2.0.0:
+ resolution: {integrity: sha512-rQ1+kcj+ttHG0MKVGBUXwayCCF1oh39BF5COIpRzuCEv8Mwjv0XucrI2ExNTOn9IlLifGClWQcU9BrZORvtw6Q==}
+ engines: {node: '>=6'}
+ dev: false
+
/clsx@2.1.0:
resolution: {integrity: sha512-m3iNNWpd9rl3jvvcBnu70ylMdrXt8Vlq4HYadnU5fwcOtvkSQWPmj7amUcDT2qYI7risszBjI5AUIUox9D16pg==}
engines: {node: '>=6'}
@@ -9266,7 +9280,7 @@ packages:
resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==}
engines: {node: '>= 10.13.0'}
dependencies:
- '@types/node': 17.0.12
+ '@types/node': 18.16.19
merge-stream: 2.0.0
supports-color: 8.1.1
dev: true
@@ -11155,6 +11169,22 @@ packages:
camelcase-css: 2.0.1
postcss: 8.4.35
+ /postcss-load-config@3.1.4:
+ resolution: {integrity: sha512-6DiM4E7v4coTE4uzA8U//WhtPwyhiim3eyjEMFCnUpzbrkK9wJHgKDT2mR+HbtSrd/NubVaYTOpSpjUl8NQeRg==}
+ engines: {node: '>= 10'}
+ peerDependencies:
+ postcss: '>=8.0.9'
+ ts-node: '>=9.0.0'
+ peerDependenciesMeta:
+ postcss:
+ optional: true
+ ts-node:
+ optional: true
+ dependencies:
+ lilconfig: 2.1.0
+ yaml: 1.10.2
+ dev: true
+
/postcss-load-config@3.1.4(postcss@8.4.21):
resolution: {integrity: sha512-6DiM4E7v4coTE4uzA8U//WhtPwyhiim3eyjEMFCnUpzbrkK9wJHgKDT2mR+HbtSrd/NubVaYTOpSpjUl8NQeRg==}
engines: {node: '>= 10'}
@@ -12909,7 +12939,7 @@ packages:
/ts-interface-checker@0.1.13:
resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}
- /ts-node@10.9.1(@types/node@17.0.12)(typescript@5.1.6):
+ /ts-node@10.9.1(@types/node@18.16.19)(typescript@5.1.6):
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
hasBin: true
peerDependencies:
@@ -12928,7 +12958,7 @@ packages:
'@tsconfig/node12': 1.0.11
'@tsconfig/node14': 1.0.3
'@tsconfig/node16': 1.0.4
- '@types/node': 17.0.12
+ '@types/node': 18.16.19
acorn: 8.10.0
acorn-walk: 8.2.0
arg: 4.1.3
@@ -12983,7 +13013,7 @@ packages:
execa: 5.1.1
globby: 11.1.0
joycon: 3.1.1
- postcss-load-config: 3.1.4(postcss@8.4.21)
+ postcss-load-config: 3.1.4
resolve-from: 5.0.0
rollup: 3.26.2
source-map: 0.8.0-beta.0
@@ -13567,7 +13597,7 @@ packages:
vfile-message: 3.1.4
dev: false
- /vite@4.4.4(@types/node@17.0.12):
+ /vite@4.4.4(@types/node@18.16.19):
resolution: {integrity: sha512-4mvsTxjkveWrKDJI70QmelfVqTm+ihFAb6+xf4sjEU2TmUCTlVX87tmg/QooPEMQb/lM9qGHT99ebqPziEd3wg==}
engines: {node: ^14.18.0 || >=16.0.0}
hasBin: true
@@ -13595,7 +13625,7 @@ packages:
terser:
optional: true
dependencies:
- '@types/node': 17.0.12
+ '@types/node': 18.16.19
esbuild: 0.18.11
postcss: 8.4.35
rollup: 3.26.2