@wordrhyme/auto-crud 0.5.0 → 0.10.0

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.cts CHANGED
@@ -1,4 +1,4 @@
1
- import * as react_jsx_runtime2 from "react/jsx-runtime";
1
+ import * as react_jsx_runtime1 from "react/jsx-runtime";
2
2
  import { z } from "zod";
3
3
  import * as _tanstack_react_table0 from "@tanstack/react-table";
4
4
  import { Column, ColumnDef, ColumnSort, Row, RowData, Table, TableOptions, TableState } from "@tanstack/react-table";
@@ -86,6 +86,33 @@ interface UseAutoCrudResourceOptions<TSchema extends z.ZodObject<z.ZodRawShape>,
86
86
  * - ToastAdapter: 注入自定义 toast 实现
87
87
  */
88
88
  toast?: ToastAdapter | false;
89
+ /**
90
+ * 自定义导出数据获取函数(escape hatch)
91
+ *
92
+ * 通常不需要传此参数:
93
+ * - 导出选中行:纯客户端,零配置
94
+ * - 导出筛选结果:router 有 export procedure 时自动可用
95
+ *
96
+ * 仅当需要完全自定义导出逻辑时才使用(如队列导出、自定义接口等)
97
+ * 传入后优先级高于 router.export
98
+ */
99
+ exportFetcher?: (params: any) => Promise<{
100
+ data: Record<string, unknown>[];
101
+ total: number;
102
+ hasMore: boolean;
103
+ }>;
104
+ }
105
+ /**
106
+ * 导入结果类型(与服务端保持一致)
107
+ */
108
+ interface ImportResult {
109
+ success: number;
110
+ updated: number;
111
+ skipped: number;
112
+ failed: Array<{
113
+ row: number;
114
+ errors: string[];
115
+ }>;
89
116
  }
90
117
  /**
91
118
  * Hook 返回值类型
@@ -103,6 +130,7 @@ interface UseAutoCrudResourceReturn<TSchema extends z.ZodObject<z.ZodRawShape>,
103
130
  isCreating: boolean;
104
131
  isUpdating: boolean;
105
132
  isDeleting: boolean;
133
+ isImporting: boolean;
106
134
  };
107
135
  handlers: {
108
136
  openCreate: () => void;
@@ -117,10 +145,15 @@ interface UseAutoCrudResourceReturn<TSchema extends z.ZodObject<z.ZodRawShape>,
117
145
  deleteMany: (rows: TListItem[]) => void;
118
146
  updateMany: (rows: TListItem[], data: Record<string, unknown>) => void;
119
147
  setVariant: (variant: ModalVariant$1) => void;
148
+ /** 导入数据(router 有 import 路由时自动可用) */
149
+ import: ((rows: Record<string, unknown>[]) => Promise<ImportResult>) | null;
150
+ /** 导出筛选结果(router 有 export procedure 或传入 exportFetcher 时自动可用)。导出选中行由 AutoCrudTable 内部处理 */
151
+ export: (() => Promise<Record<string, unknown>[]>) | null;
120
152
  };
121
153
  }
122
154
  /**
123
155
  * tRPC Router 接口定义(宽松类型以兼容实际 tRPC router)
156
+ * import 为可选 — 传入 router 时自动检测是否存在
124
157
  */
125
158
  interface CrudRouter {
126
159
  list: {
@@ -141,6 +174,27 @@ interface CrudRouter {
141
174
  updateMany?: {
142
175
  useMutation: (opts?: any) => any;
143
176
  };
177
+ /** 导入路由(mutation,由 createCrudRouter 自动生成) */
178
+ import?: {
179
+ useMutation: (opts?: any) => any;
180
+ };
181
+ /** 导出路由(query,由 createCrudRouter 自动生成) */
182
+ export?: {
183
+ useQuery: (input?: any, opts?: any) => any;
184
+ };
185
+ }
186
+ /**
187
+ * 自动生成的查询参数(由内部 URL 状态管理生成)
188
+ */
189
+ interface AutoQueryParams {
190
+ page: number;
191
+ perPage: number;
192
+ sort: Array<{
193
+ id: string;
194
+ desc: boolean;
195
+ }>;
196
+ filters: any[];
197
+ joinOperator: "and" | "or";
144
198
  }
145
199
  /**
146
200
  * Hook 配置参数
@@ -148,10 +202,24 @@ interface CrudRouter {
148
202
  interface UseAutoCrudResourceParams<TSchema extends z.ZodObject<z.ZodRawShape>, TListItem> {
149
203
  /** tRPC router (如 trpc.tasks) */
150
204
  router: CrudRouter;
151
- /** 列表查询参数 */
152
- queryInput?: any;
153
205
  /** Zod schema */
154
206
  schema: TSchema;
207
+ /**
208
+ * 查询参数变换函数
209
+ *
210
+ * 默认零配置:hook 内部自动管理 URL 状态(分页、排序、筛选),
211
+ * 传入此函数可注入额外参数或修改自动生成的参数。
212
+ *
213
+ * @example
214
+ * ```tsx
215
+ * // 注入租户 ID
216
+ * query: (params) => ({ ...params, tenantId: currentTenant.id })
217
+ *
218
+ * // 覆盖默认分页
219
+ * query: (params) => ({ ...params, perPage: 20 })
220
+ * ```
221
+ */
222
+ query?: (params: AutoQueryParams) => Record<string, unknown>;
155
223
  /** 其他配置选项 */
156
224
  options?: UseAutoCrudResourceOptions<TSchema, TListItem>;
157
225
  }
@@ -160,7 +228,8 @@ interface UseAutoCrudResourceParams<TSchema extends z.ZodObject<z.ZodRawShape>,
160
228
  */
161
229
  declare function useAutoCrudResource<TSchema extends z.ZodObject<z.ZodRawShape>, TListItem = z.output<TSchema>>({
162
230
  router,
163
- queryInput,
231
+ schema,
232
+ query: queryTransform,
164
233
  options
165
234
  }: UseAutoCrudResourceParams<TSchema, TListItem>): UseAutoCrudResourceReturn<TSchema, TListItem>;
166
235
  //#endregion
@@ -178,6 +247,8 @@ interface CrudOperationPermissions {
178
247
  delete?: boolean;
179
248
  /** 是否允许导出 */
180
249
  export?: boolean;
250
+ /** 是否允许导入 */
251
+ import?: boolean;
181
252
  }
182
253
  /**
183
254
  * AutoCrudTable 权限配置
@@ -243,7 +314,7 @@ declare function AutoTableActionBar<TData>({
243
314
  enableExport,
244
315
  enableDelete,
245
316
  extraActions
246
- }: AutoTableActionBarProps<TData>): react_jsx_runtime2.JSX.Element;
317
+ }: AutoTableActionBarProps<TData>): react_jsx_runtime1.JSX.Element;
247
318
  //#endregion
248
319
  //#region src/lib/schema-bridge/types.d.ts
249
320
  /**
@@ -369,6 +440,10 @@ interface AutoTableProps<T extends z.ZodObject<z.ZodRawShape>> {
369
440
  initialSorting?: any[];
370
441
  /** 是否启用导出功能 (默认 true) */
371
442
  enableExport?: boolean;
443
+ /** 选中行数变化回调(用于外层组件获知选中状态) */
444
+ onSelectedCountChange?: (count: number) => void;
445
+ /** 获取选中行数据的回调(由外层组件调用) */
446
+ getSelectedRows?: React.MutableRefObject<(() => z.infer<T>[]) | null>;
372
447
  }
373
448
  declare function AutoTable<T extends z.ZodObject<z.ZodRawShape>>({
374
449
  schema,
@@ -385,8 +460,10 @@ declare function AutoTable<T extends z.ZodObject<z.ZodRawShape>>({
385
460
  batchUpdateFields,
386
461
  actionBarExtra,
387
462
  initialSorting,
388
- enableExport
389
- }: AutoTableProps<T>): react_jsx_runtime2.JSX.Element;
463
+ enableExport,
464
+ onSelectedCountChange,
465
+ getSelectedRows
466
+ }: AutoTableProps<T>): react_jsx_runtime1.JSX.Element;
390
467
  //#endregion
391
468
  //#region src/components/auto-crud/auto-crud-table.d.ts
392
469
  /**
@@ -468,9 +545,9 @@ interface AutoCrudTableProps<TSchema extends z.ZodObject<z.ZodRawShape>> {
468
545
  /** 扩展点 */
469
546
  slots?: {
470
547
  /** 工具栏左侧插槽 */
471
- toolbarStart?: React.ReactNode;
548
+ toolbarStart?: React$1.ReactNode;
472
549
  /** 工具栏右侧插槽 */
473
- toolbarEnd?: React.ReactNode;
550
+ toolbarEnd?: React$1.ReactNode;
474
551
  /** 自定义行操作 */
475
552
  rowActions?: (row: z.output<TSchema>) => Array<{
476
553
  label: string;
@@ -489,6 +566,7 @@ interface AutoCrudTableProps<TSchema extends z.ZodObject<z.ZodRawShape>> {
489
566
  * update: user.role === 'admin' || user.role === 'editor',
490
567
  * delete: user.role === 'admin',
491
568
  * export: true,
569
+ * import: true,
492
570
  * },
493
571
  * deny: user.role === 'user' ? ['salary', 'ssn'] : [],
494
572
  * }), [user.role]);
@@ -513,7 +591,7 @@ declare function AutoCrudTable<TSchema extends z.ZodObject<z.ZodRawShape>>({
513
591
  form: formConfig,
514
592
  slots,
515
593
  permissions
516
- }: AutoCrudTableProps<TSchema>): react_jsx_runtime2.JSX.Element;
594
+ }: AutoCrudTableProps<TSchema>): react_jsx_runtime1.JSX.Element;
517
595
  //#endregion
518
596
  //#region src/components/auto-crud/auto-form.d.ts
519
597
  interface AutoFormProps<T extends z.ZodObject<z.ZodRawShape>> {
@@ -545,7 +623,7 @@ declare function AutoFormInner<T extends z.ZodObject<z.ZodRawShape>>({
545
623
  labelAlign,
546
624
  labelWidth,
547
625
  showSubmitButton
548
- }: AutoFormProps<T>, ref: React.Ref<AutoFormRef>): react_jsx_runtime2.JSX.Element;
626
+ }: AutoFormProps<T>, ref: React.Ref<AutoFormRef>): react_jsx_runtime1.JSX.Element;
549
627
  declare const AutoForm: <T extends z.ZodObject<z.ZodRawShape>>(props: AutoFormProps<T> & {
550
628
  ref?: React.Ref<AutoFormRef>;
551
629
  }) => ReturnType<typeof AutoFormInner>;
@@ -759,12 +837,12 @@ declare const filterItemSchema: z.ZodObject<{
759
837
  variant: z.ZodEnum<{
760
838
  number: "number";
761
839
  boolean: "boolean";
762
- date: "date";
763
840
  text: "text";
841
+ range: "range";
842
+ date: "date";
843
+ dateRange: "dateRange";
764
844
  select: "select";
765
845
  multiSelect: "multiSelect";
766
- dateRange: "dateRange";
767
- range: "range";
768
846
  }>;
769
847
  operator: z.ZodEnum<{
770
848
  iLike: "iLike";
@@ -840,7 +918,7 @@ declare function AutoTableSimpleFilters<TData>({
840
918
  shallow,
841
919
  filters: externalFilters,
842
920
  onFiltersChange
843
- }: AutoTableSimpleFiltersProps<TData>): react_jsx_runtime2.JSX.Element | null;
921
+ }: AutoTableSimpleFiltersProps<TData>): react_jsx_runtime1.JSX.Element | null;
844
922
  //#endregion
845
923
  //#region src/components/auto-crud/form-modal.d.ts
846
924
  type ModalVariant = "dialog" | "sheet";
@@ -875,7 +953,46 @@ declare function CrudFormModal<T extends z.ZodObject<z.ZodRawShape>>({
875
953
  title,
876
954
  labelAlign,
877
955
  labelWidth
878
- }: CrudFormModalProps<T>): react_jsx_runtime2.JSX.Element;
956
+ }: CrudFormModalProps<T>): react_jsx_runtime1.JSX.Element;
957
+ //#endregion
958
+ //#region src/components/auto-crud/import-dialog.d.ts
959
+ interface ImportDialogProps {
960
+ open: boolean;
961
+ onOpenChange: (open: boolean) => void;
962
+ /** 执行导入的回调 */
963
+ onImport: (rows: Record<string, unknown>[]) => Promise<ImportResult>;
964
+ /** 可用列名(用于生成模板) */
965
+ columns?: string[];
966
+ /** 对话框标题 */
967
+ title?: string;
968
+ }
969
+ declare function ImportDialog({
970
+ open,
971
+ onOpenChange,
972
+ onImport,
973
+ columns,
974
+ title
975
+ }: ImportDialogProps): react_jsx_runtime1.JSX.Element;
976
+ //#endregion
977
+ //#region src/components/auto-crud/export-dialog.d.ts
978
+ type ExportMode = "selected" | "filtered";
979
+ interface ExportDialogProps {
980
+ open: boolean;
981
+ onOpenChange: (open: boolean) => void;
982
+ /** 当前选中的行数 */
983
+ selectedCount: number;
984
+ /** 执行导出 */
985
+ onExport: (mode: ExportMode) => Promise<void>;
986
+ /** 是否支持导出筛选结果(需要 exportFetcher) */
987
+ canExportFiltered?: boolean;
988
+ }
989
+ declare function ExportDialog({
990
+ open,
991
+ onOpenChange,
992
+ selectedCount,
993
+ onExport,
994
+ canExportFiltered
995
+ }: ExportDialogProps): react_jsx_runtime1.JSX.Element;
879
996
  //#endregion
880
997
  //#region src/components/data-table/data-table.d.ts
881
998
  interface DataTableProps<TData> extends React$1.ComponentProps<"div"> {
@@ -888,7 +1005,7 @@ declare function DataTable<TData>({
888
1005
  children,
889
1006
  className,
890
1007
  ...props
891
- }: DataTableProps<TData>): react_jsx_runtime2.JSX.Element;
1008
+ }: DataTableProps<TData>): react_jsx_runtime1.JSX.Element;
892
1009
  //#endregion
893
1010
  //#region src/components/data-table/data-table-advanced-toolbar.d.ts
894
1011
  interface DataTableAdvancedToolbarProps<TData> extends React$1.ComponentProps<"div"> {
@@ -899,7 +1016,7 @@ declare function DataTableAdvancedToolbar<TData>({
899
1016
  children,
900
1017
  className,
901
1018
  ...props
902
- }: DataTableAdvancedToolbarProps<TData>): react_jsx_runtime2.JSX.Element;
1019
+ }: DataTableAdvancedToolbarProps<TData>): react_jsx_runtime1.JSX.Element;
903
1020
  //#endregion
904
1021
  //#region src/components/data-table/data-table-column-header.d.ts
905
1022
  interface DataTableColumnHeaderProps<TData, TValue> extends React.ComponentProps<typeof DropdownMenuTrigger> {
@@ -911,7 +1028,7 @@ declare function DataTableColumnHeader<TData, TValue>({
911
1028
  label,
912
1029
  className,
913
1030
  ...props
914
- }: DataTableColumnHeaderProps<TData, TValue>): react_jsx_runtime2.JSX.Element;
1031
+ }: DataTableColumnHeaderProps<TData, TValue>): react_jsx_runtime1.JSX.Element;
915
1032
  //#endregion
916
1033
  //#region src/components/data-table/data-table-faceted-filter.d.ts
917
1034
  interface DataTableFacetedFilterProps<TData, TValue> {
@@ -925,7 +1042,7 @@ declare function DataTableFacetedFilter<TData, TValue>({
925
1042
  title,
926
1043
  options,
927
1044
  multiple
928
- }: DataTableFacetedFilterProps<TData, TValue>): react_jsx_runtime2.JSX.Element;
1045
+ }: DataTableFacetedFilterProps<TData, TValue>): react_jsx_runtime1.JSX.Element;
929
1046
  //#endregion
930
1047
  //#region src/components/data-table/data-table-pagination.d.ts
931
1048
  interface DataTablePaginationProps<TData> extends React.ComponentProps<"div"> {
@@ -937,7 +1054,7 @@ declare function DataTablePagination<TData>({
937
1054
  pageSizeOptions,
938
1055
  className,
939
1056
  ...props
940
- }: DataTablePaginationProps<TData>): react_jsx_runtime2.JSX.Element;
1057
+ }: DataTablePaginationProps<TData>): react_jsx_runtime1.JSX.Element;
941
1058
  //#endregion
942
1059
  //#region src/components/data-table/data-table-toolbar.d.ts
943
1060
  interface DataTableToolbarProps<TData> extends React$1.ComponentProps<"div"> {
@@ -948,7 +1065,7 @@ declare function DataTableToolbar<TData>({
948
1065
  children,
949
1066
  className,
950
1067
  ...props
951
- }: DataTableToolbarProps<TData>): react_jsx_runtime2.JSX.Element;
1068
+ }: DataTableToolbarProps<TData>): react_jsx_runtime1.JSX.Element;
952
1069
  //#endregion
953
1070
  //#region src/components/data-table/data-table-view-options.d.ts
954
1071
  interface DataTableViewOptionsProps<TData> extends React$1.ComponentProps<typeof PopoverContent> {
@@ -959,7 +1076,7 @@ declare function DataTableViewOptions<TData>({
959
1076
  table,
960
1077
  disabled,
961
1078
  ...props
962
- }: DataTableViewOptionsProps<TData>): react_jsx_runtime2.JSX.Element;
1079
+ }: DataTableViewOptionsProps<TData>): react_jsx_runtime1.JSX.Element;
963
1080
  //#endregion
964
1081
  //#region src/hooks/use-data-table.d.ts
965
1082
  interface UseDataTableProps<TData> extends Omit<TableOptions<TData>, "state" | "pageCount" | "getCoreRowModel" | "manualFiltering" | "manualPagination" | "manualSorting">, Required<Pick<TableOptions<TData>, "pageCount">> {
@@ -1230,4 +1347,68 @@ declare function formatDate(date: Date | string | number | undefined, opts?: Int
1230
1347
  */
1231
1348
  declare function humanize(str: string): string;
1232
1349
  //#endregion
1233
- export { type ActionsColumnConfig, AutoCrudTable, type AutoCrudTableProps, AutoForm, AutoTable, AutoTableActionBar, AutoTableSimpleFilters, type ColumnOverrides, type CreateFormSchemaOptions, type CreateTableSchemaOptions, CrudFormModal, type CrudHooks, type CrudOperationPermissions, type CrudPermissions, type DataSource, DataTable, DataTableAdvancedToolbar, DataTableColumnHeader, DataTableFacetedFilter, DataTablePagination, DataTableRowAction, DataTableToolbar, DataTableViewOptions, type EnumOption, ExtendedColumnFilter, ExtendedColumnSort, type Field, type FieldType, type Fields, FilterOperator, type FilterVariant, type FormSchemaOverrides, type JSONSchema, type JSONSchemaProperty, JoinOperator, type ListParams, type ListResult, Option, type ParsedZodField, type Parser, QueryKeys, SchemaAdapter, type SimpleFieldConfig, type SimpleFieldsConfig, type ToastAdapter, type UnifiedField, type UnifiedSchema, type UrlStateOptions, type UseAutoCrudResourceOptions, cn, createActionsColumn, createEditFormSchema, createFormSchema, createMemoryDataSource, createSelectColumn, createTRPCDataSource, createTableSchema, formatDate, getUrlParams, humanize, noopToastAdapter, parseAsArrayOf, parseAsInteger, parseAsString, parseAsStringEnum, parseZodField, setSearchParams, useAutoCrudResource, useDataTable, useQueryState, useQueryStates, useReadableFilters, useUrlState, useUrlStates };
1350
+ //#region src/lib/import.d.ts
1351
+ interface ParsedImportData {
1352
+ headers: string[];
1353
+ rows: Record<string, unknown>[];
1354
+ format: "csv" | "json";
1355
+ }
1356
+ /**
1357
+ * 解析 CSV 字符串为对象数组
1358
+ * 支持:引号包裹、逗号转义、换行符、空值
1359
+ */
1360
+ declare function parseCSV(content: string): {
1361
+ headers: string[];
1362
+ rows: Record<string, string>[];
1363
+ };
1364
+ /**
1365
+ * 解析 JSON 字符串为对象数组
1366
+ * 支持 JSON 数组或 { data: [...] } 格式
1367
+ */
1368
+ declare function parseJSON(content: string): Record<string, unknown>[];
1369
+ /**
1370
+ * 统一解析入口:根据文件扩展名自动选择解析器
1371
+ */
1372
+ declare function parseImportFile(file: File): Promise<ParsedImportData>;
1373
+ /**
1374
+ * 生成 CSV 模板(仅包含表头,用于下载空模板)
1375
+ */
1376
+ declare function generateCSVTemplate(headers: string[]): string;
1377
+ /**
1378
+ * 将数据数组转换为 CSV 字符串
1379
+ */
1380
+ declare function dataToCSV<T extends Record<string, unknown>>(data: T[], opts?: {
1381
+ headers?: string[];
1382
+ excludeColumns?: string[];
1383
+ }): string;
1384
+ /**
1385
+ * 根据 Zod Schema 将 CSV 解析出的 string 值转换为正确类型
1386
+ *
1387
+ * CSV 解析后所有值都是 string,需要根据 schema 字段类型进行转换:
1388
+ * - number: parseFloat
1389
+ * - boolean: "true"/"1"/"yes" → true, 其他 → false
1390
+ * - date: new Date(value)
1391
+ * - 空字符串 → undefined(让 schema 默认值生效)
1392
+ */
1393
+ declare function coerceRowValues<T extends z.ZodObject<z.ZodRawShape>>(rows: Record<string, unknown>[], schema: T): Record<string, unknown>[];
1394
+ //#endregion
1395
+ //#region src/lib/export.d.ts
1396
+ declare function exportTableToCSV<TData>(table: Table<TData>, opts?: {
1397
+ filename?: string;
1398
+ excludeColumns?: (keyof TData | "select" | "actions")[];
1399
+ onlySelected?: boolean;
1400
+ }): void;
1401
+ /**
1402
+ * 导出原始数据数组为 CSV(用于服务端全量导出)
1403
+ */
1404
+ declare function exportAllToCSV<T extends Record<string, unknown>>(data: T[], opts?: {
1405
+ filename?: string;
1406
+ headers?: string[];
1407
+ excludeColumns?: string[];
1408
+ }): void;
1409
+ /**
1410
+ * 下载 CSV 模板文件
1411
+ */
1412
+ declare function downloadCSVTemplate(headers: string[], filename?: string): void;
1413
+ //#endregion
1414
+ export { type ActionsColumnConfig, AutoCrudTable, type AutoCrudTableProps, AutoForm, type AutoQueryParams, AutoTable, AutoTableActionBar, AutoTableSimpleFilters, type ColumnOverrides, type CreateFormSchemaOptions, type CreateTableSchemaOptions, CrudFormModal, type CrudHooks, type CrudOperationPermissions, type CrudPermissions, type DataSource, DataTable, DataTableAdvancedToolbar, DataTableColumnHeader, DataTableFacetedFilter, DataTablePagination, DataTableRowAction, DataTableToolbar, DataTableViewOptions, type EnumOption, ExportDialog, type ExportDialogProps, type ExportMode, ExtendedColumnFilter, ExtendedColumnSort, type Field, type FieldType, type Fields, FilterOperator, type FilterVariant, type FormSchemaOverrides, ImportDialog, type ImportDialogProps, type ImportResult, type JSONSchema, type JSONSchemaProperty, JoinOperator, type ListParams, type ListResult, Option, type ParsedImportData, type ParsedZodField, type Parser, QueryKeys, SchemaAdapter, type SimpleFieldConfig, type SimpleFieldsConfig, type ToastAdapter, type UnifiedField, type UnifiedSchema, type UrlStateOptions, type UseAutoCrudResourceOptions, type UseAutoCrudResourceParams, type UseAutoCrudResourceReturn, cn, coerceRowValues, createActionsColumn, createEditFormSchema, createFormSchema, createMemoryDataSource, createSelectColumn, createTRPCDataSource, createTableSchema, dataToCSV, downloadCSVTemplate, exportAllToCSV, exportTableToCSV, formatDate, generateCSVTemplate, getUrlParams, humanize, noopToastAdapter, parseAsArrayOf, parseAsInteger, parseAsString, parseAsStringEnum, parseCSV, parseImportFile, parseJSON, parseZodField, setSearchParams, useAutoCrudResource, useDataTable, useQueryState, useQueryStates, useReadableFilters, useUrlState, useUrlStates };