@wordrhyme/auto-crud 1.2.5 → 1.3.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
@@ -266,6 +266,8 @@ interface AutoCrudTableProps<TSchema> {
266
266
  overrides?: Record<string, any>; // 列覆盖配置
267
267
  filterModes?: FilterMode | FilterMode[]; // 过滤模式
268
268
  batchFields?: (string | BatchUpdateField)[]; // 批量更新字段
269
+ /** @deprecated 新代码请使用 actions.batch */
270
+ batchActions?: BatchActionConfig<z.output<TSchema>>;
269
271
  defaultSort?: any[]; // 默认排序
270
272
  };
271
273
  form?: {
@@ -273,19 +275,20 @@ interface AutoCrudTableProps<TSchema> {
273
275
  columns?: number; // 表单列数
274
276
  };
275
277
  /**
276
- * 工具栏右侧操作配置
277
- * - 只传 custom 项:内置保持默认,custom 追加到首/尾
278
- * - 包含任意内置 type:数组完全接管,未列出的内置项自动隐藏
278
+ * 行操作配置,或统一配置 toolbar/row/batch。
279
+ *
280
+ * 旧写法 actions={[...]} 仍表示行操作;
281
+ * 新写法 actions={{ toolbar, row, batch }} 可集中配置三类操作。
279
282
  */
283
+ actions?: RowActionConfig<z.output<TSchema>> | {
284
+ toolbar?: ToolbarActionConfig;
285
+ row?: RowActionConfig<z.output<TSchema>>;
286
+ batch?: BatchActionConfig<z.output<TSchema>>;
287
+ };
288
+ /** @deprecated 新代码请使用 actions.toolbar */
280
289
  toolbar?: ToolbarActionConfig;
281
- /** @deprecated 新代码请使用 toolbar */
290
+ /** @deprecated 新代码请使用 actions.toolbar */
282
291
  toolbarActions?: ToolbarActionConfig;
283
- /**
284
- * 行操作配置
285
- * - 只传 custom 项:内置保持默认,custom 追加到首/尾
286
- * - 包含任意内置 type:数组完全接管,未列出的隐藏,顺序即渲染顺序
287
- */
288
- actions?: ActionItem<z.output<TSchema>>[];
289
292
  }
290
293
  ```
291
294
 
@@ -674,24 +677,39 @@ table={{
674
677
  ### `ToolbarActionItem` 类型
675
678
 
676
679
  ```typescript
680
+ type ActionMeta = {
681
+ id?: string; // 插件注册时用于区分同 type 的 action
682
+ order?: number; // 插件注册排序,默认 100
683
+ hidden?: boolean; // 显式隐藏该 action
684
+ };
685
+
677
686
  type ToolbarActionItem = ToolbarBuiltinActionItem | ToolbarCustomActionItem;
678
687
 
679
688
  type ToolbarActionConfig =
680
689
  | ToolbarActionItem[]
681
690
  | ((defaults: ToolbarBuiltinActionItem[]) => ToolbarActionItem[]);
682
691
 
683
- type ToolbarBuiltinActionItem = {
692
+ type ToolbarBuiltinActionItem = ActionMeta & {
684
693
  type: 'create' | 'import' | 'export';
685
694
  onClick?: () => void; // 覆盖默认行为
686
695
  label?: string; // 覆盖默认文案
687
- component?: React.ReactNode; // 完全替换按钮渲染
696
+ component?: React.ReactNode | ((context: AutoCrudToolbarContext) => React.ReactNode);
688
697
  };
689
698
 
690
- type ToolbarCustomActionItem = {
699
+ type ToolbarCustomActionItem = ActionMeta & {
691
700
  type: 'custom';
692
- component: React.ReactNode; // 自定义渲染内容
701
+ component: React.ReactNode | ((context: AutoCrudToolbarContext) => React.ReactNode);
693
702
  position?: 'start' | 'end'; // 仅在无内置项时生效,默认 "end"
694
703
  };
704
+
705
+ interface AutoCrudToolbarContext {
706
+ crudId: string;
707
+ idKey: string;
708
+ rowIds: string[];
709
+ selectedRowIds: string[];
710
+ selectedCount: number;
711
+ openCreate?: () => void;
712
+ }
695
713
  ```
696
714
 
697
715
  ### 工具栏扩展 Resolver
@@ -717,20 +735,50 @@ const toolbarResolver: AutoCrudToolbarResolver = (targetId, ownerActions, contex
717
735
  setToolbarResolver(toolbarResolver);
718
736
  ```
719
737
 
738
+ ### 插件式操作注册
739
+
740
+ 跨模块扩展推荐使用 `crudActions.register`。它按 `targetId` 和区域注入操作,不需要业务页把所有插件动作手动拼到 props 中。
741
+
742
+ ```tsx
743
+ import { crudActions } from '@wordrhyme/auto-crud';
744
+
745
+ crudActions.register({
746
+ targetId: 'com.wordrhyme.shop.products',
747
+ zone: 'toolbar', // 'toolbar' | 'row' | 'batch'
748
+ ownerId: 'com.wordrhyme.product-lab',
749
+ actions: [
750
+ { type: 'export', hidden: true },
751
+ { type: 'create', label: '发布商品', order: 80 },
752
+ {
753
+ type: 'custom',
754
+ id: 'bulk-publish',
755
+ position: 'end',
756
+ component: ({ selectedCount }) => (
757
+ <Button disabled={selectedCount === 0}>批量发布</Button>
758
+ ),
759
+ },
760
+ ],
761
+ });
762
+ ```
763
+
764
+ 同一个 `ownerId + targetId + zone` 再次注册会替换该 owner 的旧动作;卸载插件或页面时可调用 `crudActions.unregister(ownerId)`。
765
+
720
766
  ---
721
767
 
722
768
  ## 🎬 行操作配置
723
769
 
724
- `actions` 数组控制每行下拉菜单的操作项,支持覆盖内置操作、添加自定义操作、完全自定义顺序。
770
+ `actions` 旧数组写法控制每行下拉菜单的操作项;统一写法可使用 `actions.row`。两种写法都支持覆盖内置操作、添加自定义操作、完全自定义顺序。
725
771
 
726
772
  ### 只添加自定义项(内置不变)
727
773
 
728
774
  ```tsx
729
775
  <AutoCrudTable
730
- actions={[
731
- { type: 'custom', label: '分配', onClick: (row) => assign(row.id) },
732
- { type: 'custom', label: '预览', onClick: (row) => preview(row), position: 'start' },
733
- ]}
776
+ actions={{
777
+ row: [
778
+ { type: 'custom', label: '分配', onClick: (row) => assign(row.id) },
779
+ { type: 'custom', label: '预览', onClick: (row) => preview(row), position: 'start' },
780
+ ],
781
+ }}
734
782
  />
735
783
  // 渲染顺序: 预览 · 查看 · 编辑 · 复制 · 删除 · 分配
736
784
  ```
@@ -741,12 +789,14 @@ setToolbarResolver(toolbarResolver);
741
789
 
742
790
  ```tsx
743
791
  <AutoCrudTable
744
- actions={[
745
- { type: 'view', onClick: (row) => router.push(`/tasks/${row.id}`) }, // 覆盖跳转
746
- { type: 'custom', label: '分配', onClick: (row) => assign(row.id) },
747
- { type: 'edit' }, // 默认行为
748
- // copy / delete 未列出 → 隐藏
749
- ]}
792
+ actions={{
793
+ row: [
794
+ { type: 'view', onClick: (row) => router.push(`/tasks/${row.id}`) }, // 覆盖跳转
795
+ { type: 'custom', label: '分配', onClick: (row) => assign(row.id) },
796
+ { type: 'edit' }, // 默认行为
797
+ // copy / delete 未列出 → 隐藏
798
+ ],
799
+ }}
750
800
  />
751
801
  ```
752
802
 
@@ -779,24 +829,42 @@ setToolbarResolver(toolbarResolver);
779
829
  />
780
830
  ```
781
831
 
782
- ### `ActionItem` 类型
832
+ ### `RowActionItem` 类型
783
833
 
784
834
  ```typescript
785
- type ActionItem<T> =
786
- | {
787
- type: 'view' | 'edit' | 'copy' | 'delete';
788
- onClick?: (row: T) => void; // 不传则使用默认行为
789
- label?: string; // 不传则使用默认文案
790
- separator?: boolean; // 此项前加分隔线
791
- }
792
- | {
793
- type: 'custom';
794
- label: string;
795
- onClick: (row: T) => void;
796
- position?: 'start' | 'end'; // 仅无内置项时生效,默认 end
797
- separator?: boolean;
798
- variant?: 'default' | 'destructive';
799
- };
835
+ type RowActionItem<T> = RowBuiltinActionItem<T> | RowCustomActionItem<T>;
836
+
837
+ type RowActionConfig<T> =
838
+ | RowActionItem<T>[]
839
+ | ((defaults: RowBuiltinActionItem<T>[]) => RowActionItem<T>[]);
840
+
841
+ type RowBuiltinActionItem<T> = ActionMeta & {
842
+ type: 'view' | 'edit' | 'copy' | 'delete';
843
+ onClick?: (row: T) => void; // 不传则使用默认行为
844
+ label?: string; // 不传则使用默认文案
845
+ separator?: boolean; // 此项前加分隔线
846
+ };
847
+
848
+ type RowCustomActionItem<T> = ActionMeta & {
849
+ type: 'custom';
850
+ label?: string;
851
+ onClick?: (row: T) => void;
852
+ component?: React.ReactNode | ((context: AutoCrudRowActionContext<T>) => React.ReactNode);
853
+ position?: 'start' | 'end'; // 仅无内置项时生效,默认 end
854
+ separator?: boolean;
855
+ variant?: 'default' | 'destructive';
856
+ };
857
+
858
+ interface AutoCrudRowActionContext<T> {
859
+ crudId: string;
860
+ idKey: string;
861
+ row: T;
862
+ rowId?: string;
863
+ openView: (row: T) => void;
864
+ openEdit?: (row: T) => void;
865
+ copyRow?: (row: T) => void;
866
+ openDelete?: (row: T) => void;
867
+ }
800
868
  ```
801
869
 
802
870
  ## 🔄 批量操作
@@ -822,13 +890,13 @@ type ActionItem<T> =
822
890
 
823
891
  ### 批量悬浮栏操作顺序
824
892
 
825
- `table.batchActions` 控制多选后悬浮操作栏,设计思路对齐行操作 `actions` 和顶部 `toolbar`。
893
+ `actions.batch` 控制多选后悬浮操作栏,设计思路对齐 `actions.row` `actions.toolbar`。旧的 `table.batchActions` 仍兼容,但新代码建议使用 `actions.batch`。
826
894
 
827
895
  内置操作类型:`batchUpdate`(批量更新字段下拉)、`export`(导出选中行)、`delete`(删除选中行)。
828
896
 
829
897
  只传 `type: "custom"` 项时,默认批量操作保持原样;包含任意内置 `type` 时,将完全接管顺序,未列出的内置操作不渲染。
830
898
 
831
- `AutoCrudTable` 默认不在悬浮栏重复显示导出按钮;需要悬浮栏导出时,在 `batchActions` 中显式列出 `{ type: 'export' }`。
899
+ `AutoCrudTable` 默认不在悬浮栏重复显示导出按钮;需要悬浮栏导出时,在 `actions.batch` 中显式列出 `{ type: 'export' }`。
832
900
 
833
901
  ```tsx
834
902
  <AutoCrudTable
@@ -836,7 +904,9 @@ type ActionItem<T> =
836
904
  resource={resource}
837
905
  table={{
838
906
  batchFields: ['status', 'priority'],
839
- batchActions: [
907
+ }}
908
+ actions={{
909
+ batch: [
840
910
  { type: 'batchUpdate' },
841
911
  {
842
912
  type: 'custom',
@@ -854,8 +924,8 @@ type ActionItem<T> =
854
924
 
855
925
  ```tsx
856
926
  <AutoCrudTable
857
- table={{
858
- batchActions: (defaults) => [
927
+ actions={{
928
+ batch: (defaults) => [
859
929
  defaults[0], // batchUpdate
860
930
  {
861
931
  type: 'custom',