@wordrhyme/auto-crud 1.12.2 → 1.14.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/README.md CHANGED
@@ -172,20 +172,20 @@ Zod Schema → 自动推断字段类型 → 生成表格列 + 表单字段
172
172
  ```typescript
173
173
  import { createMemoryDataSource } from '@wordrhyme/auto-crud';
174
174
 
175
- const dataSource = createMemoryDataSource({
176
- data: initialData,
177
- onCreate: async (data) => {
178
- /* ... */
179
- },
180
- onUpdate: async (id, data) => {
181
- /* ... */
182
- },
183
- onDelete: async (id) => {
184
- /* ... */
185
- },
175
+ const dataSource = createMemoryDataSource(initialData);
176
+ const result = await dataSource.list({
177
+ page: 1,
178
+ perPage: 10,
179
+ filters: [{ id: 'status', operator: 'inArray', value: ['draft', 'published'] }],
180
+ sort: [
181
+ { id: 'createdAt', desc: true },
182
+ { id: 'id', desc: false },
183
+ ],
186
184
  });
187
185
  ```
188
186
 
187
+ 筛选支持省略 operator / `eq`(严格相等)和 `inArray`(字段值属于给定数组);其他 operator 会报错。排序按配置顺序比较,前一字段相同时使用下一字段,再进行分页。内存数据源不执行全文搜索。
188
+
189
189
  #### 2. tRPC 数据源(tRPC Data Source)
190
190
 
191
191
  适合:全栈 TypeScript 项目
@@ -278,6 +278,8 @@ interface AutoCrudTableProps<TSchema> {
278
278
  form?: {
279
279
  overrides?: Record<string, any>; // 表单覆盖配置
280
280
  columns?: number; // 表单列数
281
+ labelAlign?: 'left' | 'top' | 'right'; // 默认 top;left/right 为左右布局及标签文字对齐
282
+ labelWidth?: number | string; // 左右布局的标签宽度,数字单位为 px
281
283
  };
282
284
  /**
283
285
  * 行操作配置,或统一配置 toolbar/row/batch。
@@ -326,6 +328,37 @@ interface AutoCrudTableProps<TSchema> {
326
328
 
327
329
  `expandedRow` 的第二个参数是当前 TanStack `Row`,可通过 `row.getAllCells()` 读取已解析的列和单元格上下文,复用字段渲染器及数据源标签。只接收订单数据的单参数回调仍可使用。
328
330
 
331
+ #### 表格状态与 URL 参数
332
+
333
+ 同页显示多个表格时,在资源 Hook 中配置独立的分页、排序和条件连接符参数名,`AutoCrudTable` 会自动传给表格:
334
+
335
+ ```tsx
336
+ const resource = useAutoCrudResource({
337
+ router: trpc.orders,
338
+ schema: orderSchema,
339
+ queryKeys: {
340
+ page: 'ordersPage',
341
+ perPage: 'ordersSize',
342
+ sort: 'ordersSort',
343
+ joinOperator: 'ordersJoin',
344
+ },
345
+ });
346
+
347
+ <AutoCrudTable
348
+ id="orders"
349
+ schema={orderSchema}
350
+ resource={resource}
351
+ table={{
352
+ enableRowSelection: false,
353
+ initialColumnVisibility: { createdAt: false },
354
+ }}
355
+ />;
356
+ ```
357
+
358
+ 未配置的参数名保留默认值。搜索仍使用共享的 `search` 参数,字段筛选仍使用字段名;`queryKeys` 不隔离这些值。筛选或搜索变化时,仅重置该表格对应的页码。直接使用 `AutoTable` 时传入相同的 `queryKeys`;`table.queryKeys` 可覆盖资源提供的整组配置。
359
+
360
+ `enableRowSelection` 默认 `true`,设为 `false` 后关闭行选择并隐藏选择列和批量操作栏。`initialColumnVisibility` 只设置首次显示的列,用户仍可通过列设置切换;它不同于 `table.hidden` 对列的排除。
361
+
329
362
  ### `useAutoCrudResource()`
330
363
 
331
364
  连接数据源到组件的 Hook。
@@ -1509,27 +1542,39 @@ function CustomTable({ data }) {
1509
1542
  ### 使用 AutoForm 组件
1510
1543
 
1511
1544
  ```typescript
1512
- import { AutoForm, createFormSchema } from "@wordrhyme/auto-crud";
1513
- import { createForm } from "@formily/core";
1545
+ import { AutoForm } from "@wordrhyme/auto-crud";
1514
1546
 
1515
1547
  function CustomForm() {
1516
- const form = createForm();
1517
- const formSchema = createFormSchema(taskSchema, {
1518
- exclude: ["id", "createdAt"],
1519
- layout: "grid",
1520
- gridColumns: 2,
1521
- });
1522
-
1523
1548
  return (
1524
1549
  <AutoForm
1525
- form={form}
1526
- schema={formSchema}
1550
+ schema={taskSchema}
1551
+ gridColumns={2}
1527
1552
  onSubmit={(values) => console.log(values)}
1528
1553
  />
1529
1554
  );
1530
1555
  }
1531
1556
  ```
1532
1557
 
1558
+ 详情使用同一条 Formily 渲染链路,通过 `mode="view"` 启用 `readPretty`,无需传入 `onSubmit`,也不会显示提交按钮:
1559
+
1560
+ ```tsx
1561
+ <AutoForm
1562
+ mode="view"
1563
+ schema={taskSchema}
1564
+ initialValues={selectedTask}
1565
+ fields={fields}
1566
+ gridColumns={2}
1567
+ />
1568
+ ```
1569
+
1570
+ 创建、编辑排除 ID 和审计托管字段;详情保留 ID、创建时间和更新时间。`form: false` 和字段级 `form['x-hidden']` 只影响创建、编辑;全局 `hidden` 和 `permissions.deny` 仍限制详情字段。其他表单覆盖配置、布局、scope 和选项标签继续共用。
1571
+
1572
+ 内置字段提供阅读态映射;通过 `components.register()` 注册的自定义组件应使用 Formily `mapReadPretty` 实现自己的阅读态。`AutoCrudTable` 详情仍展示当前选中记录,不会额外请求单条接口。
1573
+
1574
+ 详情默认单列,列数由 `AutoCrudTable` 的 `form.columns` 或 `AutoForm` 的 `gridColumns` 控制,使用浅色标签和宽松间距;长文本、数组和对象默认占满整行,字段 `gridSpan` 可指定跨度。单选标签显示为文字,多选标签使用轻量徽标。长内容自动换行;弹窗最高为视口高度的 85%,超出后正文滚动,标题保持可见。
1575
+
1576
+ `AutoCrudTable` 的 `form.labelAlign` 和 `form.labelWidth` 同时作用于创建、编辑及详情弹窗。例如 `form={{ columns: 1, labelAlign: 'left', labelWidth: 100 }}` 表示单列、左侧标签文字左对齐,标签宽度为 100px。`top` 表示上下布局(默认),`right` 表示左侧标签文字右对齐;`labelWidth` 仅在 `left` / `right` 布局时生效。
1577
+
1533
1578
  ### 使用 useDataTable Hook
1534
1579
 
1535
1580
  ```typescript