listpage-next 0.0.45 → 0.0.47

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.
@@ -0,0 +1,18 @@
1
+ import { JSX } from "react";
2
+ import { DataTableContext } from "../typings";
3
+ export interface RenderProps<T, Record> {
4
+ ctx: DataTableContext;
5
+ defaultValue: T;
6
+ record: Record;
7
+ index: number;
8
+ render: (props: UserRenderComponentProps<T, Record>) => JSX.Element;
9
+ }
10
+ export type UserRenderComponentProps<T, Record> = {
11
+ ctx: DataTableContext;
12
+ value: T;
13
+ onChange: (v: T) => void;
14
+ record: Record;
15
+ index: number;
16
+ };
17
+ export type ColumnRender<R> = <T = any>(props: UserRenderComponentProps<T, R>) => JSX.Element;
18
+ export declare const Render: <T = any, Record = any>(props: RenderProps<T, Record>) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,14 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useControllableValue } from "ahooks";
3
+ const Render = (props)=>{
4
+ const { render: Component, record, index, ctx } = props;
5
+ const [value, onChange] = useControllableValue(props);
6
+ return /*#__PURE__*/ jsx(Component, {
7
+ value: value,
8
+ onChange: onChange,
9
+ record: record,
10
+ index: index,
11
+ ctx: ctx
12
+ });
13
+ };
14
+ export { Render };
@@ -0,0 +1,3 @@
1
+ import { ColumnType } from "antd/es/table";
2
+ import { DataTableContext, DataTableProps } from "../typings";
3
+ export declare function useColumns<Record>(props: DataTableProps<Record>, ctx: DataTableContext): ColumnType<Record>[];
@@ -0,0 +1,25 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { Render } from "../components/Render.js";
3
+ function useColumns(props, ctx) {
4
+ const { columns = [] } = props;
5
+ return columns.map((column)=>{
6
+ const getRender = ()=>{
7
+ if ('function' != typeof column.render) return column.render;
8
+ if ('object' == typeof column.render) return column.render;
9
+ if ('function' == typeof column.render) return function(value, record, index) {
10
+ return /*#__PURE__*/ jsx(Render, {
11
+ render: column.render,
12
+ defaultValue: value,
13
+ record: record,
14
+ index: index,
15
+ ctx: ctx
16
+ });
17
+ };
18
+ };
19
+ return {
20
+ ...column,
21
+ render: getRender()
22
+ };
23
+ });
24
+ }
25
+ export { useColumns };
@@ -1,13 +1,10 @@
1
1
  import { DataTableProps } from "../typings";
2
+ import { BaseQueryParams } from "../../../http-client";
2
3
  export declare function useData<T = any>(props: DataTableProps<T>): {
3
4
  data: T[];
4
5
  loading: boolean;
5
- pagination: {
6
- onChange: (page: number, pageSize: number) => void;
7
- current?: number;
8
- pageSize?: number;
9
- sort?: string;
10
- };
6
+ pagination: BaseQueryParams;
7
+ onPaginationChange: (page: number, pageSize: number) => void;
11
8
  } | {
12
9
  data: T[] | undefined;
13
10
  loading: boolean;
@@ -16,6 +13,6 @@ export declare function useData<T = any>(props: DataTableProps<T>): {
16
13
  pageSize?: number;
17
14
  sort?: string;
18
15
  total: number | undefined;
19
- onChange: (page: number, pageSize: number) => void;
20
16
  };
17
+ onPaginationChange: (page: number, pageSize: number) => void;
21
18
  };
@@ -1,5 +1,5 @@
1
- import { useRequest, useUpdateEffect } from "ahooks";
2
1
  import { useState } from "react";
2
+ import { useRequest, useUpdateEffect } from "ahooks";
3
3
  function useData(props) {
4
4
  const { dataSource = [], request, refreshDeps = [] } = props;
5
5
  const pagination = props.pagination;
@@ -26,29 +26,29 @@ function useData(props) {
26
26
  ...refreshDeps
27
27
  ]);
28
28
  const onChange = (page, pageSize)=>{
29
+ const current = pageSize !== params.pageSize ? 1 : page;
29
30
  setParams({
30
31
  ...params,
31
- current: pageSize !== params.pageSize ? 1 : page,
32
+ current,
32
33
  pageSize
33
34
  });
35
+ pagination?.onChange?.(current, pageSize);
34
36
  };
35
37
  if (!request) return {
36
38
  data: dataSource,
37
39
  loading: false,
38
- pagination: {
39
- ...params,
40
- onChange
41
- }
40
+ pagination: params,
41
+ onPaginationChange: onChange
42
42
  };
43
- const { list, total, pageSize, current } = response || {};
43
+ const { list, total } = response || {};
44
44
  return {
45
45
  data: list,
46
46
  loading,
47
47
  pagination: {
48
48
  total,
49
- onChange,
50
49
  ...params
51
- }
50
+ },
51
+ onPaginationChange: onChange
52
52
  };
53
53
  }
54
54
  export { useData };
@@ -1,8 +1,8 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
+ import styled_components from "styled-components";
2
3
  import { Card, Pagination, Table } from "antd";
3
- import zh_CN from "antd/locale/zh_CN";
4
+ import { useColumns } from "./hooks/useColumns.js";
4
5
  import { useData } from "./hooks/useData.js";
5
- import styled_components from "styled-components";
6
6
  const PaginationContainer = styled_components.div`
7
7
  padding: 12px 16px 12px 0;
8
8
  border-top: 1px solid #f0f0f0;
@@ -18,12 +18,13 @@ const TableContainer = styled_components.div`
18
18
  `;
19
19
  const DataTable = (props)=>{
20
20
  const { title, extra, dataSource, request, refreshDeps, ...restProps } = props;
21
- const { data, loading, pagination } = useData({
21
+ const { data, loading, pagination, onPaginationChange } = useData({
22
22
  dataSource,
23
23
  request,
24
24
  refreshDeps,
25
25
  pagination: restProps.pagination
26
26
  });
27
+ const columns = useColumns(props, {});
27
28
  return /*#__PURE__*/ jsxs(Card, {
28
29
  title: title,
29
30
  extra: extra,
@@ -37,21 +38,18 @@ const DataTable = (props)=>{
37
38
  children: /*#__PURE__*/ jsx(Table, {
38
39
  bordered: false,
39
40
  loading: loading,
40
- locale: zh_CN.Table,
41
41
  ...restProps,
42
+ columns: columns,
42
43
  dataSource: data,
43
44
  pagination: false
44
45
  })
45
46
  }),
46
- /*#__PURE__*/ jsx(PaginationContainer, {
47
+ false !== restProps.pagination && /*#__PURE__*/ jsx(PaginationContainer, {
47
48
  children: /*#__PURE__*/ jsx(Pagination, {
48
- style: {
49
- border: 'none'
50
- },
51
49
  align: "end",
52
- locale: zh_CN.Pagination,
53
50
  ...pagination,
54
- ...restProps?.pagination
51
+ ...restProps?.pagination,
52
+ onChange: onPaginationChange
55
53
  })
56
54
  })
57
55
  ]
@@ -1,10 +1,18 @@
1
+ import React from "react";
1
2
  import { TableProps } from "antd";
2
3
  import { BaseQueryParams, PaginationData } from "../../../http-client";
3
- import React from "react";
4
- export interface DataTableProps<Record = any> extends Omit<TableProps<Record>, 'dataSource' | 'title'> {
4
+ import { ColumnType } from "antd/es/table";
5
+ import { ColumnRender } from "../components/Render";
6
+ export interface DataTableProps<Record = any> extends Omit<TableProps<Record>, 'dataSource' | 'title' | 'columns'> {
5
7
  dataSource?: Record[];
6
8
  request?: (pageParams: BaseQueryParams) => Promise<PaginationData<Record>>;
7
9
  refreshDeps?: any[];
8
10
  title?: React.ReactNode;
9
11
  extra?: React.ReactNode;
12
+ columns?: DataTableColumns<Record>[];
13
+ }
14
+ export interface DataTableColumns<Record = any> extends Omit<ColumnType<Record>, 'render'> {
15
+ render?: ColumnRender<Record>;
16
+ }
17
+ export interface DataTableContext {
10
18
  }
@@ -1,7 +1,17 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
- import { Button } from "antd";
2
+ import { Button, Switch } from "antd";
3
3
  import { DataTable } from "../components/DataTable/index.js";
4
4
  const columns = [
5
+ {
6
+ key: 'enable',
7
+ title: 'Enable',
8
+ dataIndex: 'enable',
9
+ render: (props)=>/*#__PURE__*/ jsx(Switch, {
10
+ size: "small",
11
+ checked: props.value,
12
+ onChange: (e)=>props.onChange(e)
13
+ })
14
+ },
5
15
  {
6
16
  title: 'Name',
7
17
  dataIndex: 'name',
@@ -22,6 +32,7 @@ const listdata = Array.from(new Array(100)).map((_, index)=>({
22
32
  key: index,
23
33
  name: `Edward King ${index}`,
24
34
  age: 32,
35
+ enable: true,
25
36
  address: `London, Park Lane no. ${index}`
26
37
  }));
27
38
  const request = async (params)=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "listpage-next",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",