@bsol-oss/react-datatable5 12.0.0-beta.68 → 12.0.0-beta.69

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/dist/index.d.ts CHANGED
@@ -227,7 +227,7 @@ interface UseDataTableReturn {
227
227
  }
228
228
  declare const useDataTable: ({ default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, keyPrefix, }?: UseDataTableProps) => UseDataTableReturn;
229
229
 
230
- interface UseDataTableServerProps extends UseDataTableProps {
230
+ interface UseDataTableServerProps<TData> extends UseDataTableProps {
231
231
  /**
232
232
  * Delay to send the request if the `refreshData` called multiple times
233
233
  *
@@ -240,7 +240,51 @@ interface UseDataTableServerProps extends UseDataTableProps {
240
240
  * default: `1000`
241
241
  */
242
242
  debounceDelay?: number;
243
- url: string;
243
+ /**
244
+ * The url to fetch the data from.
245
+ *
246
+ * Remark:
247
+ * it is the server responsibility to handle the params and return the data.
248
+ * especially the pagination and sorting.
249
+ *
250
+ * The response must be like this:
251
+ * ```ts
252
+ * {
253
+ * data: TData[],
254
+ * count: number,
255
+ * }
256
+ * ```
257
+ *
258
+ * Example:
259
+ * ```ts
260
+ * const url = "https://jsonplaceholder.typicode.com/posts";
261
+ * ```
262
+ *
263
+ * If not provided, the `queryFn` will be used.
264
+ *
265
+ * @default undefined
266
+ */
267
+ url?: string;
268
+ placeholderData?: DataResponse<TData>;
269
+ /**
270
+ * The query function to fetch the data from.
271
+ *
272
+ * Remark:
273
+ * it is the server responsibility to handle the params and return the data.
274
+ * especially the pagination and sorting.
275
+ *
276
+ * Example:
277
+ * ```ts
278
+ * const queryFn = (params: QueryParams) => {
279
+ * return axios.get<DataResponse<TData>>(url, { params });
280
+ * };
281
+ * ```
282
+ *
283
+ * If not provided, the `url` will be used.
284
+ *
285
+ * @default undefined
286
+ */
287
+ queryFn?: (params: QueryParams) => Promise<DataResponse<TData>>;
244
288
  }
245
289
  interface UseDataTableServerReturn<TData> extends UseDataTableReturn {
246
290
  query: UseQueryResult<DataResponse<TData>, Error>;
@@ -251,7 +295,14 @@ interface Result<T = unknown> {
251
295
  interface DataResponse<T = unknown> extends Result<T> {
252
296
  count: number;
253
297
  }
254
- declare const useDataTableServer: <TData>({ url, default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, keyPrefix, }: UseDataTableServerProps) => UseDataTableServerReturn<TData>;
298
+ interface QueryParams {
299
+ offset: number;
300
+ limit: number;
301
+ sorting: SortingState;
302
+ where: ColumnFiltersState;
303
+ searching: string;
304
+ }
305
+ declare const useDataTableServer: <TData>(props: UseDataTableServerProps<TData>) => UseDataTableServerReturn<TData>;
255
306
 
256
307
  interface DataTableServerProps<TData = unknown> {
257
308
  children: ReactNode | ReactNode[];
@@ -283,7 +334,7 @@ interface DataTableServerProps<TData = unknown> {
283
334
  setDensity: OnChangeFn<DensityState>;
284
335
  setColumnVisibility: OnChangeFn<VisibilityState>;
285
336
  query: UseQueryResult<DataResponse<TData>>;
286
- url: string;
337
+ url?: string;
287
338
  translate: UseTranslationResponse<any, any>;
288
339
  tableLabel?: DataTableLabel;
289
340
  }
@@ -738,4 +789,4 @@ declare module "@tanstack/react-table" {
738
789
  }
739
790
  }
740
791
 
741
- export { type CalendarProps, CardHeader, type CardHeaderProps, type CustomJSONSchema7Definition, DataDisplay, type DataDisplayProps, type DataResponse, DataTable, type DataTableDefaultState, type DataTableProps, DataTableServer, type DataTableServerProps, type DatePickerLabels, type DatePickerProps, DefaultCardTitle, DefaultForm, type DefaultFormProps, DefaultTable, type DefaultTableProps, DensityToggleButton, type DensityToggleButtonProps, type EditFilterButtonProps, EditSortingButton, type EditSortingButtonProps, type EditViewButtonProps, EmptyState, type EmptyStateProps, ErrorAlert, type ErrorAlertProps, FilterDialog, FormBody, FormRoot, type FormRootProps, FormTitle, type GetColumnsConfigs, type GetDateColorProps, type GetMultiDatesProps, type GetRangeDatesProps, type GetStyleProps, type GetVariantProps, GlobalFilter, PageSizeControl, type PageSizeControlProps, Pagination, type RangeCalendarProps, type RangeDatePickerProps, RecordDisplay, type RecordDisplayProps, ReloadButton, type ReloadButtonProps, ResetFilteringButton, ResetSelectionButton, ResetSortingButton, type Result, RowCountText, Table, TableBody, type TableBodyProps, TableCardContainer, type TableCardContainerProps, TableCards, type TableCardsProps, TableComponent, TableControls, type TableControlsProps, TableDataDisplay, type TableDataDisplayProps, TableFilter, TableFilterTags, TableFooter, type TableFooterProps, TableHeader, type TableHeaderProps, type TableHeaderTexts, TableLoadingComponent, type TableLoadingComponentProps, type TableProps, type TableRendererProps, type TableRowSelectorProps, TableSelector, TableSorter, TableViewer, TextCell, type TextCellProps, type UseDataTableProps, type UseDataTableReturn, type UseDataTableServerProps, type UseDataTableServerReturn, type UseFormProps, ViewDialog, getColumns, getMultiDates, getRangeDates, idPickerSanityCheck, useDataTable, useDataTableContext, useDataTableServer, useForm, widthSanityCheck };
792
+ export { type CalendarProps, CardHeader, type CardHeaderProps, type CustomJSONSchema7Definition, DataDisplay, type DataDisplayProps, type DataResponse, DataTable, type DataTableDefaultState, type DataTableProps, DataTableServer, type DataTableServerProps, type DatePickerLabels, type DatePickerProps, DefaultCardTitle, DefaultForm, type DefaultFormProps, DefaultTable, type DefaultTableProps, DensityToggleButton, type DensityToggleButtonProps, type EditFilterButtonProps, EditSortingButton, type EditSortingButtonProps, type EditViewButtonProps, EmptyState, type EmptyStateProps, ErrorAlert, type ErrorAlertProps, FilterDialog, FormBody, FormRoot, type FormRootProps, FormTitle, type GetColumnsConfigs, type GetDateColorProps, type GetMultiDatesProps, type GetRangeDatesProps, type GetStyleProps, type GetVariantProps, GlobalFilter, PageSizeControl, type PageSizeControlProps, Pagination, type QueryParams, type RangeCalendarProps, type RangeDatePickerProps, RecordDisplay, type RecordDisplayProps, ReloadButton, type ReloadButtonProps, ResetFilteringButton, ResetSelectionButton, ResetSortingButton, type Result, RowCountText, Table, TableBody, type TableBodyProps, TableCardContainer, type TableCardContainerProps, TableCards, type TableCardsProps, TableComponent, TableControls, type TableControlsProps, TableDataDisplay, type TableDataDisplayProps, TableFilter, TableFilterTags, TableFooter, type TableFooterProps, TableHeader, type TableHeaderProps, type TableHeaderTexts, TableLoadingComponent, type TableLoadingComponentProps, type TableProps, type TableRendererProps, type TableRowSelectorProps, TableSelector, TableSorter, TableViewer, TextCell, type TextCellProps, type UseDataTableProps, type UseDataTableReturn, type UseDataTableServerProps, type UseDataTableServerReturn, type UseFormProps, ViewDialog, getColumns, getMultiDates, getRangeDates, idPickerSanityCheck, useDataTable, useDataTableContext, useDataTableServer, useForm, widthSanityCheck };
package/dist/index.js CHANGED
@@ -3474,50 +3474,43 @@ const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: def
3474
3474
  };
3475
3475
  };
3476
3476
 
3477
- const useDataTableServer = ({ url, default: { sorting: defaultSorting = [], pagination: defaultPagination = {
3478
- pageIndex: 0, //initial page index
3479
- pageSize: 10, //default page size
3480
- }, rowSelection: defaultRowSelection = {}, columnFilters: defaultColumnFilters = [], columnOrder: defaultColumnOrder = [], columnVisibility: defaultColumnVisibility = {}, globalFilter: defaultGlobalFilter = "", density: defaultDensity = "sm", } = {
3481
- sorting: [],
3482
- pagination: {
3477
+ const useDataTableServer = (props) => {
3478
+ const { url, default: defaultProps, keyPrefix, placeholderData, queryFn: customQueryFn, } = props;
3479
+ const { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, } = defaultProps || {};
3480
+ const [sorting, setSorting] = React.useState(defaultSorting || []);
3481
+ const [columnFilters, setColumnFilters] = React.useState(defaultColumnFilters || []); // can set initial column filter state here
3482
+ const [pagination, setPagination] = React.useState(defaultPagination || {
3483
3483
  pageIndex: 0, //initial page index
3484
- pageSize: 10, //age size
3485
- },
3486
- rowSelection: {},
3487
- columnFilters: [],
3488
- columnOrder: [],
3489
- columnVisibility: {},
3490
- globalFilter: "",
3491
- density: "sm",
3492
- }, keyPrefix, }) => {
3493
- const [sorting, setSorting] = React.useState(defaultSorting);
3494
- const [columnFilters, setColumnFilters] = React.useState(defaultColumnFilters); // can set initial column filter state here
3495
- const [pagination, setPagination] = React.useState(defaultPagination);
3496
- const [rowSelection, setRowSelection] = React.useState(defaultRowSelection);
3497
- const [columnOrder, setColumnOrder] = React.useState(defaultColumnOrder);
3498
- const [globalFilter, setGlobalFilter] = React.useState(defaultGlobalFilter);
3499
- const [density, setDensity] = React.useState(defaultDensity);
3500
- const [columnVisibility, setColumnVisibility] = React.useState(defaultColumnVisibility);
3484
+ pageSize: 10, //default page size
3485
+ });
3486
+ const [rowSelection, setRowSelection] = React.useState(defaultRowSelection || {});
3487
+ const [columnOrder, setColumnOrder] = React.useState(defaultColumnOrder || []);
3488
+ const [globalFilter, setGlobalFilter] = React.useState(defaultGlobalFilter || "");
3489
+ const [density, setDensity] = React.useState(defaultDensity || "sm");
3490
+ const [columnVisibility, setColumnVisibility] = React.useState(defaultColumnVisibility || {});
3491
+ const { pageSize, pageIndex } = pagination;
3501
3492
  const params = {
3502
- offset: pagination.pageIndex * pagination.pageSize,
3503
- limit: pagination.pageSize,
3493
+ offset: pageIndex * pageSize,
3494
+ limit: pageSize,
3504
3495
  sorting,
3505
3496
  where: columnFilters,
3506
3497
  searching: globalFilter,
3507
3498
  };
3499
+ const defaultQueryFn = async () => {
3500
+ if (!url) {
3501
+ throw new Error("url is required");
3502
+ }
3503
+ const response = await axios.get(url, {
3504
+ params,
3505
+ });
3506
+ return response.data;
3507
+ };
3508
3508
  const query = reactQuery.useQuery({
3509
3509
  queryKey: [url, JSON.stringify(params)],
3510
- queryFn: () => {
3511
- return axios
3512
- .get(url, {
3513
- params,
3514
- })
3515
- .then((res) => res.data);
3516
- },
3517
- placeholderData: {
3518
- count: 0,
3519
- data: [],
3520
- },
3510
+ queryFn: customQueryFn !== undefined
3511
+ ? () => customQueryFn(params)
3512
+ : defaultQueryFn,
3513
+ placeholderData,
3521
3514
  });
3522
3515
  const translate = reactI18next.useTranslation("", { keyPrefix });
3523
3516
  return {
package/dist/index.mjs CHANGED
@@ -3454,50 +3454,43 @@ const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: def
3454
3454
  };
3455
3455
  };
3456
3456
 
3457
- const useDataTableServer = ({ url, default: { sorting: defaultSorting = [], pagination: defaultPagination = {
3458
- pageIndex: 0, //initial page index
3459
- pageSize: 10, //default page size
3460
- }, rowSelection: defaultRowSelection = {}, columnFilters: defaultColumnFilters = [], columnOrder: defaultColumnOrder = [], columnVisibility: defaultColumnVisibility = {}, globalFilter: defaultGlobalFilter = "", density: defaultDensity = "sm", } = {
3461
- sorting: [],
3462
- pagination: {
3457
+ const useDataTableServer = (props) => {
3458
+ const { url, default: defaultProps, keyPrefix, placeholderData, queryFn: customQueryFn, } = props;
3459
+ const { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, } = defaultProps || {};
3460
+ const [sorting, setSorting] = useState(defaultSorting || []);
3461
+ const [columnFilters, setColumnFilters] = useState(defaultColumnFilters || []); // can set initial column filter state here
3462
+ const [pagination, setPagination] = useState(defaultPagination || {
3463
3463
  pageIndex: 0, //initial page index
3464
- pageSize: 10, //age size
3465
- },
3466
- rowSelection: {},
3467
- columnFilters: [],
3468
- columnOrder: [],
3469
- columnVisibility: {},
3470
- globalFilter: "",
3471
- density: "sm",
3472
- }, keyPrefix, }) => {
3473
- const [sorting, setSorting] = useState(defaultSorting);
3474
- const [columnFilters, setColumnFilters] = useState(defaultColumnFilters); // can set initial column filter state here
3475
- const [pagination, setPagination] = useState(defaultPagination);
3476
- const [rowSelection, setRowSelection] = useState(defaultRowSelection);
3477
- const [columnOrder, setColumnOrder] = useState(defaultColumnOrder);
3478
- const [globalFilter, setGlobalFilter] = useState(defaultGlobalFilter);
3479
- const [density, setDensity] = useState(defaultDensity);
3480
- const [columnVisibility, setColumnVisibility] = useState(defaultColumnVisibility);
3464
+ pageSize: 10, //default page size
3465
+ });
3466
+ const [rowSelection, setRowSelection] = useState(defaultRowSelection || {});
3467
+ const [columnOrder, setColumnOrder] = useState(defaultColumnOrder || []);
3468
+ const [globalFilter, setGlobalFilter] = useState(defaultGlobalFilter || "");
3469
+ const [density, setDensity] = useState(defaultDensity || "sm");
3470
+ const [columnVisibility, setColumnVisibility] = useState(defaultColumnVisibility || {});
3471
+ const { pageSize, pageIndex } = pagination;
3481
3472
  const params = {
3482
- offset: pagination.pageIndex * pagination.pageSize,
3483
- limit: pagination.pageSize,
3473
+ offset: pageIndex * pageSize,
3474
+ limit: pageSize,
3484
3475
  sorting,
3485
3476
  where: columnFilters,
3486
3477
  searching: globalFilter,
3487
3478
  };
3479
+ const defaultQueryFn = async () => {
3480
+ if (!url) {
3481
+ throw new Error("url is required");
3482
+ }
3483
+ const response = await axios.get(url, {
3484
+ params,
3485
+ });
3486
+ return response.data;
3487
+ };
3488
3488
  const query = useQuery({
3489
3489
  queryKey: [url, JSON.stringify(params)],
3490
- queryFn: () => {
3491
- return axios
3492
- .get(url, {
3493
- params,
3494
- })
3495
- .then((res) => res.data);
3496
- },
3497
- placeholderData: {
3498
- count: 0,
3499
- data: [],
3500
- },
3490
+ queryFn: customQueryFn !== undefined
3491
+ ? () => customQueryFn(params)
3492
+ : defaultQueryFn,
3493
+ placeholderData,
3501
3494
  });
3502
3495
  const translate = useTranslation("", { keyPrefix });
3503
3496
  return {
@@ -35,7 +35,7 @@ export interface DataTableServerProps<TData = unknown> {
35
35
  setDensity: OnChangeFn<DensityState>;
36
36
  setColumnVisibility: OnChangeFn<VisibilityState>;
37
37
  query: UseQueryResult<DataResponse<TData>>;
38
- url: string;
38
+ url?: string;
39
39
  translate: UseTranslationResponse<any, any>;
40
40
  tableLabel?: DataTableLabel;
41
41
  }
@@ -1,6 +1,7 @@
1
1
  import { UseQueryResult } from "@tanstack/react-query";
2
+ import { ColumnFiltersState, SortingState } from "@tanstack/react-table";
2
3
  import { UseDataTableProps, UseDataTableReturn } from "./useDataTable";
3
- export interface UseDataTableServerProps extends UseDataTableProps {
4
+ export interface UseDataTableServerProps<TData> extends UseDataTableProps {
4
5
  /**
5
6
  * Delay to send the request if the `refreshData` called multiple times
6
7
  *
@@ -13,7 +14,51 @@ export interface UseDataTableServerProps extends UseDataTableProps {
13
14
  * default: `1000`
14
15
  */
15
16
  debounceDelay?: number;
16
- url: string;
17
+ /**
18
+ * The url to fetch the data from.
19
+ *
20
+ * Remark:
21
+ * it is the server responsibility to handle the params and return the data.
22
+ * especially the pagination and sorting.
23
+ *
24
+ * The response must be like this:
25
+ * ```ts
26
+ * {
27
+ * data: TData[],
28
+ * count: number,
29
+ * }
30
+ * ```
31
+ *
32
+ * Example:
33
+ * ```ts
34
+ * const url = "https://jsonplaceholder.typicode.com/posts";
35
+ * ```
36
+ *
37
+ * If not provided, the `queryFn` will be used.
38
+ *
39
+ * @default undefined
40
+ */
41
+ url?: string;
42
+ placeholderData?: DataResponse<TData>;
43
+ /**
44
+ * The query function to fetch the data from.
45
+ *
46
+ * Remark:
47
+ * it is the server responsibility to handle the params and return the data.
48
+ * especially the pagination and sorting.
49
+ *
50
+ * Example:
51
+ * ```ts
52
+ * const queryFn = (params: QueryParams) => {
53
+ * return axios.get<DataResponse<TData>>(url, { params });
54
+ * };
55
+ * ```
56
+ *
57
+ * If not provided, the `url` will be used.
58
+ *
59
+ * @default undefined
60
+ */
61
+ queryFn?: (params: QueryParams) => Promise<DataResponse<TData>>;
17
62
  }
18
63
  export interface UseDataTableServerReturn<TData> extends UseDataTableReturn {
19
64
  query: UseQueryResult<DataResponse<TData>, Error>;
@@ -24,4 +69,11 @@ export interface Result<T = unknown> {
24
69
  export interface DataResponse<T = unknown> extends Result<T> {
25
70
  count: number;
26
71
  }
27
- export declare const useDataTableServer: <TData>({ url, default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, keyPrefix, }: UseDataTableServerProps) => UseDataTableServerReturn<TData>;
72
+ export interface QueryParams {
73
+ offset: number;
74
+ limit: number;
75
+ sorting: SortingState;
76
+ where: ColumnFiltersState;
77
+ searching: string;
78
+ }
79
+ export declare const useDataTableServer: <TData>(props: UseDataTableServerProps<TData>) => UseDataTableServerReturn<TData>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsol-oss/react-datatable5",
3
- "version": "12.0.0-beta.68",
3
+ "version": "12.0.0-beta.69",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",