@jswork/antd-form-schema-crud 1.0.0 → 1.0.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/dist/index.mjs CHANGED
@@ -130,8 +130,8 @@ function errorMessage(error, fallback = DEFAULT_ERROR_MESSAGE) {
130
130
  if (typeof error === "object") {
131
131
  const record = error;
132
132
  const nested = record.error && typeof record.error === "object" ? record.error : void 0;
133
- const message = nested && nested.message || record.message || record.msg;
134
- if (typeof message === "string" && message) return message;
133
+ const message2 = nested && nested.message || record.message || record.msg;
134
+ if (typeof message2 === "string" && message2) return message2;
135
135
  }
136
136
  return fallback;
137
137
  }
@@ -864,20 +864,69 @@ function SchemaCrudFormRender(props) {
864
864
  }
865
865
  var SchemaCrudForm = Object.assign(SchemaCrudFormRender, {
866
866
  displayName: "SchemaCrudForm",
867
- version: "1.0.0"
867
+ version: "1.0.1"
868
868
  });
869
869
  var AntdFormSchemaCrudForm = SchemaCrudForm;
870
870
  var schema_crud_form_default = SchemaCrudForm;
871
871
 
872
872
  // src/components/schema-crud-list.tsx
873
873
  import { useMemo as useMemo5 } from "react";
874
- import { Alert as Alert2, Button as Button4, Card as Card2, Space as Space3, Table } from "antd";
874
+ import { Alert as Alert2, Button as Button4, Card as Card2, Space as Space3, Table, message } from "antd";
875
875
  import {
876
876
  ArrowLeftOutlined as ArrowLeftOutlined3,
877
877
  PlusOutlined,
878
878
  ReloadOutlined
879
879
  } from "@ant-design/icons";
880
880
  import cx2 from "classnames";
881
+ import { useInRouterContext, useLocation, useNavigate } from "react-router-dom";
882
+
883
+ // src/hooks/use-crud-page-params.ts
884
+ import { useCallback as useCallback6 } from "react";
885
+ import { useSearchParams } from "react-router-dom";
886
+ function readPositive(value, fallback) {
887
+ const parsed = Number(value);
888
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
889
+ }
890
+ function useCrudPageParams(options = {}) {
891
+ const {
892
+ defaultPage = 1,
893
+ defaultPageSize = 10,
894
+ pageKey = "page",
895
+ sizeKey = "size",
896
+ keepDefaultParams = true,
897
+ onPageChange
898
+ } = options;
899
+ const [searchParams, setSearchParams] = useSearchParams();
900
+ const page = readPositive(searchParams.get(pageKey), defaultPage);
901
+ const pageSize = readPositive(searchParams.get(sizeKey), defaultPageSize);
902
+ const handlePageChange = useCallback6(
903
+ (nextPage, nextPageSize) => {
904
+ const next = new URLSearchParams(searchParams);
905
+ if (keepDefaultParams) {
906
+ next.set(pageKey, String(nextPage));
907
+ next.set(sizeKey, String(nextPageSize));
908
+ } else {
909
+ if (nextPage <= defaultPage) next.delete(pageKey);
910
+ else next.set(pageKey, String(nextPage));
911
+ if (nextPageSize === defaultPageSize) next.delete(sizeKey);
912
+ else next.set(sizeKey, String(nextPageSize));
913
+ }
914
+ setSearchParams(next, { replace: true });
915
+ onPageChange == null ? void 0 : onPageChange(nextPage, nextPageSize);
916
+ },
917
+ [
918
+ searchParams,
919
+ setSearchParams,
920
+ pageKey,
921
+ sizeKey,
922
+ keepDefaultParams,
923
+ defaultPage,
924
+ defaultPageSize,
925
+ onPageChange
926
+ ]
927
+ );
928
+ return { page, pageSize, onPageChange: handlePageChange };
929
+ }
881
930
 
882
931
  // src/hooks/use-crud-columns.tsx
883
932
  import { useMemo as useMemo3 } from "react";
@@ -1063,7 +1112,7 @@ function useCrudColumns({
1063
1112
  }
1064
1113
 
1065
1114
  // src/hooks/use-crud-list.ts
1066
- import { useCallback as useCallback6, useEffect as useEffect7, useMemo as useMemo4, useRef as useRef4, useState as useState7 } from "react";
1115
+ import { useCallback as useCallback7, useEffect as useEffect7, useMemo as useMemo4, useRef as useRef4, useState as useState7 } from "react";
1067
1116
  function useCrudList({
1068
1117
  enabled,
1069
1118
  resource,
@@ -1103,7 +1152,7 @@ function useCrudList({
1103
1152
  const nextPage = controlledPage > 0 ? controlledPage : 1;
1104
1153
  setPage((current) => current === nextPage ? current : nextPage);
1105
1154
  }, [controlledPage]);
1106
- const commitPage = useCallback6(
1155
+ const commitPage = useCallback7(
1107
1156
  (nextPage, nextSize = size) => {
1108
1157
  var _a;
1109
1158
  setPage(nextPage);
@@ -1176,10 +1225,10 @@ function useCrudList({
1176
1225
  onErrorRef,
1177
1226
  commitPage
1178
1227
  ]);
1179
- const reload = useCallback6(() => {
1228
+ const reload = useCallback7(() => {
1180
1229
  setReloadVersion((value) => value + 1);
1181
1230
  }, []);
1182
- const handlePageChange = useCallback6(
1231
+ const handlePageChange = useCallback7(
1183
1232
  (nextPage, nextPageSize) => {
1184
1233
  if (nextPageSize !== size) {
1185
1234
  setSize(nextPageSize);
@@ -1190,7 +1239,7 @@ function useCrudList({
1190
1239
  },
1191
1240
  [size, commitPage]
1192
1241
  );
1193
- const emitSuccess = useCallback6(
1242
+ const emitSuccess = useCallback7(
1194
1243
  (event) => __async(null, null, function* () {
1195
1244
  var _a, _b;
1196
1245
  try {
@@ -1201,7 +1250,7 @@ function useCrudList({
1201
1250
  }),
1202
1251
  [onSuccessRef, onErrorRef]
1203
1252
  );
1204
- const handleDelete = useCallback6(
1253
+ const handleDelete = useCallback7(
1205
1254
  (record) => __async(null, null, function* () {
1206
1255
  var _a;
1207
1256
  const destroy = apiRef.current.destroy;
@@ -1230,7 +1279,7 @@ function useCrudList({
1230
1279
  }),
1231
1280
  [apiRef, rowKey, rows.length, page, reload, emitSuccess, onErrorRef, commitPage]
1232
1281
  );
1233
- const clearActionError = useCallback6(() => setActionError(null), []);
1282
+ const clearActionError = useCallback7(() => setActionError(null), []);
1234
1283
  return {
1235
1284
  page,
1236
1285
  pageSize: size,
@@ -1409,9 +1458,46 @@ function SchemaCrudListRender(props) {
1409
1458
  }
1410
1459
  );
1411
1460
  }
1412
- var SchemaCrudList = Object.assign(SchemaCrudListRender, {
1461
+ function SchemaCrudListRoute(props) {
1462
+ var _a, _b, _c, _d, _e, _f, _g;
1463
+ const listKeys = getSchemaCrudListKeys();
1464
+ const navigate = useNavigate();
1465
+ const location = useLocation();
1466
+ const route = useCrudPageParams({
1467
+ pageKey: listKeys.page,
1468
+ sizeKey: listKeys.size,
1469
+ defaultPage: (_a = props.page) != null ? _a : 1,
1470
+ defaultPageSize: (_b = props.pageSize) != null ? _b : 10,
1471
+ keepDefaultParams: props.keepDefaultParams,
1472
+ onPageChange: props.onPageChange
1473
+ });
1474
+ const basePath = location.pathname.replace(/\/+$/, "") || "/";
1475
+ const search = location.search;
1476
+ const rowKey = (_c = props.rowKey) != null ? _c : "id";
1477
+ const recordId = (record) => typeof rowKey === "function" ? rowKey(record) : record[rowKey];
1478
+ return /* @__PURE__ */ jsx5(
1479
+ SchemaCrudListRender,
1480
+ __spreadProps(__spreadValues({}, props), {
1481
+ page: route.page,
1482
+ pageSize: route.pageSize,
1483
+ onPageChange: route.onPageChange,
1484
+ onCreate: (_d = props.onCreate) != null ? _d : (() => navigate(`${basePath}/add${search}`)),
1485
+ onEdit: (_e = props.onEdit) != null ? _e : ((record) => navigate(`${basePath}/${recordId(record)}/edit${search}`)),
1486
+ onSuccess: (_f = props.onSuccess) != null ? _f : ((event) => {
1487
+ if (event.action === "delete") message.success("\u5220\u9664\u6210\u529F");
1488
+ }),
1489
+ onError: (_g = props.onError) != null ? _g : ((error) => message.error(errorMessage(error, "\u64CD\u4F5C\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002")))
1490
+ })
1491
+ );
1492
+ }
1493
+ function SchemaCrudListWithRouter(props) {
1494
+ const inRouter = useInRouterContext();
1495
+ const controlled = props.page !== void 0 || props.pageSize !== void 0;
1496
+ return inRouter && !controlled ? /* @__PURE__ */ jsx5(SchemaCrudListRoute, __spreadValues({}, props)) : /* @__PURE__ */ jsx5(SchemaCrudListRender, __spreadValues({}, props));
1497
+ }
1498
+ var SchemaCrudList = Object.assign(SchemaCrudListWithRouter, {
1413
1499
  displayName: "SchemaCrudList",
1414
- version: "1.0.0"
1500
+ version: "1.0.1"
1415
1501
  });
1416
1502
  export {
1417
1503
  AntdFormSchemaCrudForm,