@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/README.md CHANGED
@@ -133,6 +133,32 @@ export const postPageConfig = {
133
133
  - 成功回调统一为 `onSuccess(event)`,按 `event.action` 区分 `create / update / delete`。
134
134
  - 需要受控模式时传 `mode`,并通过 `onModeChange` 同步。
135
135
 
136
+ ## Router 分页
137
+
138
+ `SchemaCrudList` 默认从路由查询参数读取并写回分页,不需要额外组件或子入口:
139
+
140
+ ```tsx
141
+ import { SchemaCrudList } from '@jswork/antd-form-schema-crud';
142
+
143
+ <SchemaCrudList
144
+ resource="posts"
145
+ schema={postSchema}
146
+ title="文章"
147
+ onCreate={() => navigate('/posts/new')}
148
+ onEdit={(record) => navigate(`/posts/${record.id}/edit`)}
149
+ />
150
+ ```
151
+
152
+ - 默认读写 `?page=1&size=10`,翻页和切换每页数量会自动更新 URL。
153
+ - URL 参数名跟随 `configureSchemaCrudList({ keys: { page, size } })` 的全局配置。
154
+ - 业务不再需要手写 `handlePageChange`,也不需要自己读取 `useSearchParams`。
155
+ - `keepDefaultParams` 默认 `true`,即使等于默认值也保留在 URL 中;传 `false` 时默认值会从 URL 移除。
156
+ - 需要额外处理时仍可传 `onPageChange`,它在 URL 更新后触发。
157
+ - 同时传 `page` 或 `pageSize` 视为受控分页,此时不读写 URL,回到纯组件状态。
158
+ - 不在 Router 上下文中时自动退回组件内部分页,单元测试和无路由页面仍可直接使用。
159
+ - 在 Router 上下文中,新建默认导航到 `${pathname}/add${search}`,编辑默认导航到 `${pathname}/${rowKey}/edit${search}`;传入 `onCreate` / `onEdit` 即完全覆盖。
160
+ - 删除成功默认提示“删除成功”,请求失败默认提示“操作失败,请重试。”;`onSuccess` / `onError` 可覆盖。
161
+
136
162
  ## Column presets
137
163
 
138
164
  ```ts
package/dist/index.cjs CHANGED
@@ -181,8 +181,8 @@ function errorMessage(error, fallback = DEFAULT_ERROR_MESSAGE) {
181
181
  if (typeof error === "object") {
182
182
  const record = error;
183
183
  const nested = record.error && typeof record.error === "object" ? record.error : void 0;
184
- const message = nested && nested.message || record.message || record.msg;
185
- if (typeof message === "string" && message) return message;
184
+ const message2 = nested && nested.message || record.message || record.msg;
185
+ if (typeof message2 === "string" && message2) return message2;
186
186
  }
187
187
  return fallback;
188
188
  }
@@ -915,19 +915,68 @@ function SchemaCrudFormRender(props) {
915
915
  }
916
916
  var SchemaCrudForm = Object.assign(SchemaCrudFormRender, {
917
917
  displayName: "SchemaCrudForm",
918
- version: "1.0.0"
918
+ version: "1.0.1"
919
919
  });
920
920
  var AntdFormSchemaCrudForm = SchemaCrudForm;
921
921
  var schema_crud_form_default = SchemaCrudForm;
922
922
 
923
923
  // src/components/schema-crud-list.tsx
924
- var import_react12 = require("react");
924
+ var import_react13 = require("react");
925
925
  var import_antd7 = require("antd");
926
926
  var import_icons3 = require("@ant-design/icons");
927
927
  var import_classnames2 = __toESM(require("classnames"));
928
+ var import_react_router_dom2 = require("react-router-dom");
928
929
 
929
- // src/hooks/use-crud-columns.tsx
930
+ // src/hooks/use-crud-page-params.ts
930
931
  var import_react10 = require("react");
932
+ var import_react_router_dom = require("react-router-dom");
933
+ function readPositive(value, fallback) {
934
+ const parsed = Number(value);
935
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
936
+ }
937
+ function useCrudPageParams(options = {}) {
938
+ const {
939
+ defaultPage = 1,
940
+ defaultPageSize = 10,
941
+ pageKey = "page",
942
+ sizeKey = "size",
943
+ keepDefaultParams = true,
944
+ onPageChange
945
+ } = options;
946
+ const [searchParams, setSearchParams] = (0, import_react_router_dom.useSearchParams)();
947
+ const page = readPositive(searchParams.get(pageKey), defaultPage);
948
+ const pageSize = readPositive(searchParams.get(sizeKey), defaultPageSize);
949
+ const handlePageChange = (0, import_react10.useCallback)(
950
+ (nextPage, nextPageSize) => {
951
+ const next = new URLSearchParams(searchParams);
952
+ if (keepDefaultParams) {
953
+ next.set(pageKey, String(nextPage));
954
+ next.set(sizeKey, String(nextPageSize));
955
+ } else {
956
+ if (nextPage <= defaultPage) next.delete(pageKey);
957
+ else next.set(pageKey, String(nextPage));
958
+ if (nextPageSize === defaultPageSize) next.delete(sizeKey);
959
+ else next.set(sizeKey, String(nextPageSize));
960
+ }
961
+ setSearchParams(next, { replace: true });
962
+ onPageChange == null ? void 0 : onPageChange(nextPage, nextPageSize);
963
+ },
964
+ [
965
+ searchParams,
966
+ setSearchParams,
967
+ pageKey,
968
+ sizeKey,
969
+ keepDefaultParams,
970
+ defaultPage,
971
+ defaultPageSize,
972
+ onPageChange
973
+ ]
974
+ );
975
+ return { page, pageSize, onPageChange: handlePageChange };
976
+ }
977
+
978
+ // src/hooks/use-crud-columns.tsx
979
+ var import_react11 = require("react");
931
980
  var import_antd6 = require("antd");
932
981
 
933
982
  // src/utils/list.ts
@@ -1032,8 +1081,8 @@ function useCrudColumns({
1032
1081
  onEdit,
1033
1082
  onDelete
1034
1083
  }) {
1035
- const presetMap = (0, import_react10.useMemo)(() => resolveListPresets(presets), [presets]);
1036
- const dataColumns = (0, import_react10.useMemo)(() => {
1084
+ const presetMap = (0, import_react11.useMemo)(() => resolveListPresets(presets), [presets]);
1085
+ const dataColumns = (0, import_react11.useMemo)(() => {
1037
1086
  if (columns) {
1038
1087
  return columns.map((column) => {
1039
1088
  if (hasChildren(column)) return column;
@@ -1064,7 +1113,7 @@ function useCrudColumns({
1064
1113
  };
1065
1114
  });
1066
1115
  }, [columns, presetMap, schema.fields]);
1067
- const actionColumn = (0, import_react10.useMemo)(() => {
1116
+ const actionColumn = (0, import_react11.useMemo)(() => {
1068
1117
  if (!canEdit && !canDelete) return null;
1069
1118
  return [
1070
1119
  {
@@ -1102,7 +1151,7 @@ function useCrudColumns({
1102
1151
  }
1103
1152
  ];
1104
1153
  }, [canEdit, canDelete, confirmDelete, deletingKey, rowKey, onEdit, onDelete]);
1105
- return (0, import_react10.useMemo)(() => {
1154
+ return (0, import_react11.useMemo)(() => {
1106
1155
  const list = [...dataColumns != null ? dataColumns : []];
1107
1156
  if (actionColumn) list.push(...actionColumn);
1108
1157
  return list;
@@ -1110,7 +1159,7 @@ function useCrudColumns({
1110
1159
  }
1111
1160
 
1112
1161
  // src/hooks/use-crud-list.ts
1113
- var import_react11 = require("react");
1162
+ var import_react12 = require("react");
1114
1163
  function useCrudList({
1115
1164
  enabled,
1116
1165
  resource,
@@ -1125,32 +1174,32 @@ function useCrudList({
1125
1174
  onError
1126
1175
  }) {
1127
1176
  const initialPage = controlledPage && controlledPage > 0 ? controlledPage : 1;
1128
- const [page, setPage] = (0, import_react11.useState)(initialPage);
1129
- const [size, setSize] = (0, import_react11.useState)(pageSize);
1130
- const [rows, setRows] = (0, import_react11.useState)([]);
1131
- const [total, setTotal] = (0, import_react11.useState)(0);
1132
- const [loading, setLoading] = (0, import_react11.useState)(false);
1133
- const [loadError, setLoadError] = (0, import_react11.useState)(null);
1134
- const [actionError, setActionError] = (0, import_react11.useState)(
1177
+ const [page, setPage] = (0, import_react12.useState)(initialPage);
1178
+ const [size, setSize] = (0, import_react12.useState)(pageSize);
1179
+ const [rows, setRows] = (0, import_react12.useState)([]);
1180
+ const [total, setTotal] = (0, import_react12.useState)(0);
1181
+ const [loading, setLoading] = (0, import_react12.useState)(false);
1182
+ const [loadError, setLoadError] = (0, import_react12.useState)(null);
1183
+ const [actionError, setActionError] = (0, import_react12.useState)(
1135
1184
  null
1136
1185
  );
1137
- const [deletingKey, setDeletingKey] = (0, import_react11.useState)(null);
1138
- const [reloadVersion, setReloadVersion] = (0, import_react11.useState)(0);
1139
- const requestIdRef = (0, import_react11.useRef)(0);
1140
- const deletingRef = (0, import_react11.useRef)(false);
1186
+ const [deletingKey, setDeletingKey] = (0, import_react12.useState)(null);
1187
+ const [reloadVersion, setReloadVersion] = (0, import_react12.useState)(0);
1188
+ const requestIdRef = (0, import_react12.useRef)(0);
1189
+ const deletingRef = (0, import_react12.useRef)(false);
1141
1190
  const paramsRef = useLatest(params);
1142
1191
  const onErrorRef = useLatest(onError);
1143
1192
  const onSuccessRef = useLatest(onSuccess);
1144
1193
  const onPageChangeRef = useLatest(onPageChange);
1145
- const paramsKey = (0, import_react11.useMemo)(() => stableStringify(params != null ? params : {}), [params]);
1146
- const previousParamsKeyRef = (0, import_react11.useRef)(paramsKey);
1147
- const previousPageSizeRef = (0, import_react11.useRef)(pageSize);
1148
- (0, import_react11.useEffect)(() => {
1194
+ const paramsKey = (0, import_react12.useMemo)(() => stableStringify(params != null ? params : {}), [params]);
1195
+ const previousParamsKeyRef = (0, import_react12.useRef)(paramsKey);
1196
+ const previousPageSizeRef = (0, import_react12.useRef)(pageSize);
1197
+ (0, import_react12.useEffect)(() => {
1149
1198
  if (controlledPage === void 0 || !Number.isFinite(controlledPage)) return;
1150
1199
  const nextPage = controlledPage > 0 ? controlledPage : 1;
1151
1200
  setPage((current) => current === nextPage ? current : nextPage);
1152
1201
  }, [controlledPage]);
1153
- const commitPage = (0, import_react11.useCallback)(
1202
+ const commitPage = (0, import_react12.useCallback)(
1154
1203
  (nextPage, nextSize = size) => {
1155
1204
  var _a;
1156
1205
  setPage(nextPage);
@@ -1158,13 +1207,13 @@ function useCrudList({
1158
1207
  },
1159
1208
  [size, onPageChangeRef]
1160
1209
  );
1161
- (0, import_react11.useEffect)(() => {
1210
+ (0, import_react12.useEffect)(() => {
1162
1211
  if (previousPageSizeRef.current === pageSize) return;
1163
1212
  previousPageSizeRef.current = pageSize;
1164
1213
  setSize(pageSize);
1165
1214
  if (page !== 1) commitPage(1, pageSize);
1166
1215
  }, [pageSize, page, commitPage]);
1167
- (0, import_react11.useEffect)(() => {
1216
+ (0, import_react12.useEffect)(() => {
1168
1217
  var _a;
1169
1218
  if (!enabled) {
1170
1219
  setLoading(false);
@@ -1223,10 +1272,10 @@ function useCrudList({
1223
1272
  onErrorRef,
1224
1273
  commitPage
1225
1274
  ]);
1226
- const reload = (0, import_react11.useCallback)(() => {
1275
+ const reload = (0, import_react12.useCallback)(() => {
1227
1276
  setReloadVersion((value) => value + 1);
1228
1277
  }, []);
1229
- const handlePageChange = (0, import_react11.useCallback)(
1278
+ const handlePageChange = (0, import_react12.useCallback)(
1230
1279
  (nextPage, nextPageSize) => {
1231
1280
  if (nextPageSize !== size) {
1232
1281
  setSize(nextPageSize);
@@ -1237,7 +1286,7 @@ function useCrudList({
1237
1286
  },
1238
1287
  [size, commitPage]
1239
1288
  );
1240
- const emitSuccess = (0, import_react11.useCallback)(
1289
+ const emitSuccess = (0, import_react12.useCallback)(
1241
1290
  (event) => __async(null, null, function* () {
1242
1291
  var _a, _b;
1243
1292
  try {
@@ -1248,7 +1297,7 @@ function useCrudList({
1248
1297
  }),
1249
1298
  [onSuccessRef, onErrorRef]
1250
1299
  );
1251
- const handleDelete = (0, import_react11.useCallback)(
1300
+ const handleDelete = (0, import_react12.useCallback)(
1252
1301
  (record) => __async(null, null, function* () {
1253
1302
  var _a;
1254
1303
  const destroy = apiRef.current.destroy;
@@ -1277,7 +1326,7 @@ function useCrudList({
1277
1326
  }),
1278
1327
  [apiRef, rowKey, rows.length, page, reload, emitSuccess, onErrorRef, commitPage]
1279
1328
  );
1280
- const clearActionError = (0, import_react11.useCallback)(() => setActionError(null), []);
1329
+ const clearActionError = (0, import_react12.useCallback)(() => setActionError(null), []);
1281
1330
  return {
1282
1331
  page,
1283
1332
  pageSize: size,
@@ -1323,8 +1372,8 @@ function SchemaCrudListRender(props) {
1323
1372
  } = props;
1324
1373
  const listKeys = getSchemaCrudListKeys();
1325
1374
  const cardSize = getSchemaCrudCardSize();
1326
- const resolvedApi = (0, import_react12.useMemo)(() => resolveApi(api, resource), [api, resource]);
1327
- const effectiveSchema = (0, import_react12.useMemo)(() => selectSchemaFields(schema, fields), [schema, fields]);
1375
+ const resolvedApi = (0, import_react13.useMemo)(() => resolveApi(api, resource), [api, resource]);
1376
+ const effectiveSchema = (0, import_react13.useMemo)(() => selectSchemaFields(schema, fields), [schema, fields]);
1328
1377
  const requirement = checkListRequirement(resolvedApi, resource);
1329
1378
  const blocked = requirement.missing.length > 0;
1330
1379
  const {
@@ -1456,9 +1505,46 @@ function SchemaCrudListRender(props) {
1456
1505
  }
1457
1506
  );
1458
1507
  }
1459
- var SchemaCrudList = Object.assign(SchemaCrudListRender, {
1508
+ function SchemaCrudListRoute(props) {
1509
+ var _a, _b, _c, _d, _e, _f, _g;
1510
+ const listKeys = getSchemaCrudListKeys();
1511
+ const navigate = (0, import_react_router_dom2.useNavigate)();
1512
+ const location = (0, import_react_router_dom2.useLocation)();
1513
+ const route = useCrudPageParams({
1514
+ pageKey: listKeys.page,
1515
+ sizeKey: listKeys.size,
1516
+ defaultPage: (_a = props.page) != null ? _a : 1,
1517
+ defaultPageSize: (_b = props.pageSize) != null ? _b : 10,
1518
+ keepDefaultParams: props.keepDefaultParams,
1519
+ onPageChange: props.onPageChange
1520
+ });
1521
+ const basePath = location.pathname.replace(/\/+$/, "") || "/";
1522
+ const search = location.search;
1523
+ const rowKey = (_c = props.rowKey) != null ? _c : "id";
1524
+ const recordId = (record) => typeof rowKey === "function" ? rowKey(record) : record[rowKey];
1525
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1526
+ SchemaCrudListRender,
1527
+ __spreadProps(__spreadValues({}, props), {
1528
+ page: route.page,
1529
+ pageSize: route.pageSize,
1530
+ onPageChange: route.onPageChange,
1531
+ onCreate: (_d = props.onCreate) != null ? _d : (() => navigate(`${basePath}/add${search}`)),
1532
+ onEdit: (_e = props.onEdit) != null ? _e : ((record) => navigate(`${basePath}/${recordId(record)}/edit${search}`)),
1533
+ onSuccess: (_f = props.onSuccess) != null ? _f : ((event) => {
1534
+ if (event.action === "delete") import_antd7.message.success("\u5220\u9664\u6210\u529F");
1535
+ }),
1536
+ onError: (_g = props.onError) != null ? _g : ((error) => import_antd7.message.error(errorMessage(error, "\u64CD\u4F5C\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002")))
1537
+ })
1538
+ );
1539
+ }
1540
+ function SchemaCrudListWithRouter(props) {
1541
+ const inRouter = (0, import_react_router_dom2.useInRouterContext)();
1542
+ const controlled = props.page !== void 0 || props.pageSize !== void 0;
1543
+ return inRouter && !controlled ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SchemaCrudListRoute, __spreadValues({}, props)) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SchemaCrudListRender, __spreadValues({}, props));
1544
+ }
1545
+ var SchemaCrudList = Object.assign(SchemaCrudListWithRouter, {
1460
1546
  displayName: "SchemaCrudList",
1461
- version: "1.0.0"
1547
+ version: "1.0.1"
1462
1548
  });
1463
1549
  // Annotate the CommonJS export names for ESM import in node:
1464
1550
  0 && (module.exports = {