@ys-knife-crud/vue 0.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 +234 -0
- package/dist/index.cjs +1318 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +663 -0
- package/dist/index.d.ts +663 -0
- package/dist/index.js +1279 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# @ys-knife-crud/vue
|
|
2
|
+
|
|
3
|
+
Reusable **Vue 3 composables** for `ys.knife.crud` — the UI-library-agnostic logic layer that drives a CRUD table.
|
|
4
|
+
|
|
5
|
+
These composables encapsulate the table business logic (column configuration, Excel export, row actions) without depending on any specific UI component library, so any Vue 3 project can consume them. They build on top of [`@ys-knife-crud/core`](../core) for the framework-agnostic contracts (`Meta`, `Column`, `Action`, `ExportApi`, `CustomConfig`, …).
|
|
6
|
+
|
|
7
|
+
The element-plus view layer ([`@ys-knife-crud/element-plus`](../element-plus)) is one consumer: it keeps only the `.vue` components and delegates all logic to this package.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
| Composable | Responsibility |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `useDefault` | Table default state (`meta`, `paged`, `rows`, `metaLoading`, `dataLoading`, `currentPage`), derived `total`, and `loadMeta`. Leaves page-size-dependent logic (`loadData`, `showPagination`) to the view layer. |
|
|
14
|
+
| `useCustomConfig` | Column visibility / order / width + user default page size, plus the column-settings panel draft state. |
|
|
15
|
+
| `useExportExcel` | Export scope selection (selected / current page / all), streaming pagination fetch with progress & cancel, and "keep partial file / discard" flow. |
|
|
16
|
+
| `useRowActions` | Load row actions (`rowActionsFunc`), plus `visibleActions` / `isEnabled` pure helpers. |
|
|
17
|
+
| `useSelection` | Track checkbox selection (cross-page accumulation) and clear all selections. |
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# npm
|
|
23
|
+
npm install @ys-knife-crud/vue @ys-knife-crud/core
|
|
24
|
+
|
|
25
|
+
# pnpm
|
|
26
|
+
pnpm add @ys-knife-crud/vue @ys-knife-crud/core
|
|
27
|
+
|
|
28
|
+
# yarn
|
|
29
|
+
yarn add @ys-knife-crud/vue @ys-knife-crud/core
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Peer dependencies
|
|
33
|
+
|
|
34
|
+
| Package | Version | Notes |
|
|
35
|
+
| --- | --- | --- |
|
|
36
|
+
| `vue` | `^3.5.0` | Must be installed by the consuming project. |
|
|
37
|
+
|
|
38
|
+
`@ys-knife-crud/core` is a regular dependency and is pulled in automatically.
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
All composables are named exports from the package entry:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import {
|
|
46
|
+
useCustomConfig,
|
|
47
|
+
useExportExcel,
|
|
48
|
+
useRowActions,
|
|
49
|
+
useSelection,
|
|
50
|
+
visibleActions,
|
|
51
|
+
isEnabled,
|
|
52
|
+
} from "@ys-knife-crud/vue";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `useCustomConfig`
|
|
56
|
+
|
|
57
|
+
Manages the user's column configuration (visibility, order, width) and the user's default page size. Reads `meta` to determine which columns are candidates (`showForDisplay === true`).
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const props = defineProps<{
|
|
61
|
+
showCustomConfig: boolean;
|
|
62
|
+
loadCustomConfigFun?: loadCustomConfigFunc;
|
|
63
|
+
saveCustomConfigFun?: saveCustomConfigFunc;
|
|
64
|
+
}>();
|
|
65
|
+
|
|
66
|
+
const meta = ref<Meta | null>(null);
|
|
67
|
+
|
|
68
|
+
const {
|
|
69
|
+
columns, // ComputedRef<Column[]> — final visible columns in display order,
|
|
70
|
+
// each Column carries `width` (px string, e.g. "120") from CustomConfig
|
|
71
|
+
loadCustomConfigs, // (signal?) => Promise<void>
|
|
72
|
+
customConfigLoading, // Ref<boolean> — true while loadCustomConfigs is in flight
|
|
73
|
+
defaultPageSize, // Ref<number | undefined> — user's saved default page size
|
|
74
|
+
updateDefaultPageSize, // (size: number) => void — update state + persist as default
|
|
75
|
+
configDialogVisible, // Ref<boolean>
|
|
76
|
+
draftColumns, // Ref<DraftColumn[]>
|
|
77
|
+
openConfigDialog, // () => void
|
|
78
|
+
moveDraft, // (index: number, delta: number) => void
|
|
79
|
+
resetDraft, // () => void
|
|
80
|
+
saveConfigDialog, // () => Promise<void>
|
|
81
|
+
} = useCustomConfig(props, meta);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `useExportExcel`
|
|
85
|
+
|
|
86
|
+
Drives Excel export. `scope === "all"` streams data page by page via `dataFun`, writing each page through `ExportApi.renderRows` so memory stays bounded. Cancellation supports a "keep the partial file already written" confirmation.
|
|
87
|
+
|
|
88
|
+
The composable owns all dialog visibility, progress state, and actions. **Recommended pattern**: a dialog component calls `useExportExcel` internally and exposes `openExportDialog()` via `defineExpose`, so the parent only passes data inputs and never touches export-internal state.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { toRef } from "vue";
|
|
92
|
+
import { useExportExcel } from "@ys-knife-crud/vue";
|
|
93
|
+
|
|
94
|
+
// Inside a self-managing dialog component (e.g. exportExcelDialog.vue)
|
|
95
|
+
const props = defineProps<{
|
|
96
|
+
dataFun: PageFunc<unknown>;
|
|
97
|
+
exportSelected: boolean;
|
|
98
|
+
exportorFunc?: ExportApiFunc;
|
|
99
|
+
tableName?: string;
|
|
100
|
+
columns: Column[];
|
|
101
|
+
currentRows: Record<string, unknown>[];
|
|
102
|
+
selectedRows: unknown[];
|
|
103
|
+
total: number;
|
|
104
|
+
exportPageSize: number;
|
|
105
|
+
hasMorePage: boolean;
|
|
106
|
+
}>();
|
|
107
|
+
|
|
108
|
+
const {
|
|
109
|
+
exportOptions, // ComputedRef<ExportOption[]>
|
|
110
|
+
exportDialogVisible, // Ref<boolean>
|
|
111
|
+
exporting, // Ref<boolean>
|
|
112
|
+
exportFetched, // Ref<number>
|
|
113
|
+
exportTotal, // Ref<number> — starts from the caller-provided total
|
|
114
|
+
exportTotalKnown, // Ref<boolean> — true only after a response carries a real totalCount
|
|
115
|
+
exportEtaSeconds, // ComputedRef<number | null> — estimated seconds remaining, null when unknown
|
|
116
|
+
exportPercent, // ComputedRef<number> — capped at 99% until real totalCount arrives
|
|
117
|
+
exportCancelledVisible, // Ref<boolean>
|
|
118
|
+
exportCancelledRows, // Ref<number>
|
|
119
|
+
onExportClick, // () => void — auto-skips dialog when only one option is enabled
|
|
120
|
+
doExport, // (scope: ExportScope) => Promise<void>
|
|
121
|
+
cancelExport, // () => void
|
|
122
|
+
keepPartialExport, // () => Promise<void>
|
|
123
|
+
discardPartialExport, // () => Promise<void>
|
|
124
|
+
} = useExportExcel({
|
|
125
|
+
props, // reactive — dataFun/exportSelected/exportorFunc auto-tracked
|
|
126
|
+
tableName: toRef(props, "tableName"),
|
|
127
|
+
columns: toRef(props, "columns"),
|
|
128
|
+
currentRows: toRef(props, "currentRows"),
|
|
129
|
+
selectedRows: toRef(props, "selectedRows"),
|
|
130
|
+
total: toRef(props, "total"),
|
|
131
|
+
exportPageSize: toRef(props, "exportPageSize"),
|
|
132
|
+
hasMorePage: toRef(props, "hasMorePage"),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Expose the entry point so the parent's toolbar button can start the export flow
|
|
136
|
+
defineExpose({ openExportDialog: onExportClick });
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
> **Why `toRef`?** Vue auto-unwraps `Ref` values passed as props, so `props.tableName` is `string | undefined`, not `Ref<string | undefined>`. `toRef(props, "tableName")` re-wraps it into a `Ref` that the composable can read via `.value`, preserving reactivity. The `columns`/`currentRows`/`total`/`exportPageSize`/`hasMorePage` options accept `Ref<T>` (not strictly `ComputedRef<T>`), so both `computed()` results and `toRef()` wrappers work.
|
|
140
|
+
|
|
141
|
+
> **Unknown total.** During "export all", `exportTotal` is seeded from the caller-provided `total` (an estimate when the API omits `totalCount`), but `exportTotalKnown` only turns true once a page response carries a real `totalCount`. Until then the percentage scrolls with the loaded count, capped at 99%, and `exportEtaSeconds` stays `null` (no remaining-time estimate is possible against a moving denominator); after that the bar converges to the exact percentage and `exportEtaSeconds` extrapolates the remaining time from the observed fetch rate (recomputed on a 500ms tick so it counts down between page requests). The stream still terminates correctly on `hasNext: false`.
|
|
142
|
+
|
|
143
|
+
The parent component then only wires data inputs and calls `openExportDialog()`:
|
|
144
|
+
|
|
145
|
+
```vue
|
|
146
|
+
<ExportExcelDialog
|
|
147
|
+
v-if="showExportExcel"
|
|
148
|
+
ref="exportDialogRef"
|
|
149
|
+
:data-fun="props.dataFun"
|
|
150
|
+
:export-selected="props.showCheckbox"
|
|
151
|
+
:exportor-func="props.exportorFunc"
|
|
152
|
+
:table-name="meta?.displayName ?? '数据'"
|
|
153
|
+
:columns="columns"
|
|
154
|
+
:current-rows="rows"
|
|
155
|
+
:selected-rows="selectedRows"
|
|
156
|
+
:export-page-size="props.exportPageSize"
|
|
157
|
+
:total="total"
|
|
158
|
+
:has-more-page="showPagination"
|
|
159
|
+
/>
|
|
160
|
+
<!-- toolbar button -->
|
|
161
|
+
<el-button v-if="showExportExcel" @click="exportDialogRef?.openExportDialog()">⬇ 导出 Excel</el-button>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
If `exportorFunc` is not provided, the core console stub (`createConsoleExportApiFunc`) is used — it logs calls and produces no file. Inject a real implementation such as [`@ys-knife-crud/export-exceljs`](../export-exceljs) for actual file output.
|
|
165
|
+
|
|
166
|
+
### `useRowActions`
|
|
167
|
+
|
|
168
|
+
Loads the row action list. Visibility and enabled-state filtering are exposed as pure functions so they can be used in render functions / templates without depending on the composable's internal state.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const {
|
|
172
|
+
actions, // Ref<Action<unknown>[]>
|
|
173
|
+
actionsLoading, // Ref<boolean>
|
|
174
|
+
loadActions, // (signal?) => Promise<void>
|
|
175
|
+
} = useRowActions(props); // props: { rowActionsFunc?: RowActionsFunc<unknown> }
|
|
176
|
+
|
|
177
|
+
// Pure helpers (also exported from the package entry)
|
|
178
|
+
const visible = visibleActions(actions.value, row);
|
|
179
|
+
const disabled = !isEnabled(action, row);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### `useSelection`
|
|
183
|
+
|
|
184
|
+
Tracks checkbox selection in a UI-library-agnostic `Map` keyed by `rowKey`. The same state drives both a table view (el-table selection column in controlled mode — `restoreSelection` re-checks the current page after paging or remounting) and a card view (per-card checkbox via `isRowSelected` / `toggleRowSelection`), so selection persists across pages and across view switches.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
const tableEl = ref<{ clearSelection?: () => void; toggleRowSelection?: (row: unknown, selected?: boolean) => void } | null>(null);
|
|
188
|
+
|
|
189
|
+
const {
|
|
190
|
+
selectedRows, // ComputedRef<unknown[]> — cross-page accumulated selection
|
|
191
|
+
onSelectionChange, // (selection: unknown[]) => void — el-table selection-change handler
|
|
192
|
+
isRowSelected, // (row) => boolean — card checkbox model-value / selected style
|
|
193
|
+
toggleRowSelection, // (row, selected?) => void — card checkbox toggle (also syncs el-table)
|
|
194
|
+
restoreSelection, // () => Promise<void> — re-apply accumulated selection to the current table page
|
|
195
|
+
clearSelection, // () => void — clears all pages
|
|
196
|
+
} = useSelection({ tableEl, rows, rowKey: toRef(props, "rowKey") });
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Relationship to other packages
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
┌─────────────────────────────────────────────────┐
|
|
203
|
+
│ @ys-knife-crud/element-plus (.vue) │ ← element-plus view layer
|
|
204
|
+
└───────────────────────┬─────────────────────────┘
|
|
205
|
+
│ consumes
|
|
206
|
+
▼
|
|
207
|
+
┌─────────────────────────────────────────────────┐
|
|
208
|
+
│ @ys-knife-crud/vue (composables) │ ← this package
|
|
209
|
+
└───────────────────────┬─────────────────────────┘
|
|
210
|
+
│ depends on
|
|
211
|
+
▼
|
|
212
|
+
┌─────────────────────────────────────────────────┐
|
|
213
|
+
│ @ys-knife-crud/core (contracts & types) │ ← framework-agnostic
|
|
214
|
+
└─────────────────────────────────────────────────┘
|
|
215
|
+
|
|
216
|
+
@ys-knife-crud/export-exceljs
|
|
217
|
+
(ExportApi implementation, injected as exportorFunc)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Scripts
|
|
221
|
+
|
|
222
|
+
Run from `packages/vue`:
|
|
223
|
+
|
|
224
|
+
| Script | What it does |
|
|
225
|
+
| --- | --- |
|
|
226
|
+
| `pnpm build` | Build with `tsup` (ESM + CJS + `.d.ts`). |
|
|
227
|
+
| `pnpm test` | Run unit tests with `vitest`. |
|
|
228
|
+
| `pnpm test:watch` | Watch mode for tests. |
|
|
229
|
+
| `pnpm typecheck` | `tsc --noEmit`. |
|
|
230
|
+
| `pnpm clean` | Remove `dist/`. |
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MIT.
|