@vuetkit/components 0.0.10 → 0.0.11

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.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { RequestService } from "@vuetkit/core";
2
- import { ComponentSize, FormRules, MessageProps, MessageType, TableColumnCtx, TableProps } from "element-plus";
3
- import { Component, VNode } from "vue";
2
+ import { ComponentSize, FormRules, MessageProps, MessageType, PaginationProps, TableProps } from "element-plus";
3
+ import { CSSProperties, Component, MaybeRef, VNode } from "vue";
4
+ import { TableColumnProps } from "element-plus/es/components/table/src/table-column/defaults.mjs";
4
5
 
5
6
  //#region src/feedback/useAsyncConfirm/index.d.ts
6
7
  interface AsyncConfirmOptions {
@@ -76,40 +77,31 @@ type FormReturnType<T extends object> = [Component, {
76
77
  setData: (newData: T) => void;
77
78
  }];
78
79
  declare function setDeepProperty(data: Recordable, propArr: string[], value: unknown): void;
80
+ declare function useForm(options: undefined): [null, Recordable];
79
81
  declare function useForm<T extends object>(options: FormOptions<T>): FormReturnType<T>;
80
82
  //#endregion
81
83
  //#region src/table/useTable/index.d.ts
82
84
  type DefaultRow = Record<PropertyKey, any>;
83
- interface TableColumn<T extends DefaultRow> {
85
+ interface TableColumnOptions<T extends DefaultRow> extends TableColumnProps<T> {
84
86
  render?: (row: T) => VNode | string;
85
- children?: TableColumn<T>[];
87
+ children?: TableColumnOptions<T>[];
86
88
  prop?: string;
87
- label?: string;
88
- width?: string | number;
89
- minWidth?: string | number;
90
- fixed?: boolean | 'left' | 'right';
91
- align?: 'left' | 'center' | 'right';
92
- headerAlign?: 'left' | 'center' | 'right';
93
- className?: string;
94
- labelClassName?: string;
95
- showOverflowTooltip?: boolean;
96
- sortable?: boolean | string;
97
- sortMethod?: (a: T, b: T) => number;
98
- sortBy?: string | string[];
99
- sortOrders?: ('ascending' | 'descending')[];
100
- resizable?: boolean;
101
- formatter?: (row: T, column: TableColumnCtx<T>, cellValue: any, index: number) => any;
102
- type?: 'selection' | 'index' | 'expand';
103
- selectable?: (row: T, index: number) => boolean;
104
- reserveSelection?: boolean;
105
- index?: number | ((index: number) => number);
89
+ }
90
+ interface PaginationOptions extends Partial<PaginationProps> {
91
+ wrapStyle?: CSSProperties;
106
92
  }
107
93
  interface TableOptions<T extends DefaultRow> extends TableProps<T> {
108
- columns: TableColumn<T>[];
94
+ columns: TableColumnOptions<T>[];
95
+ service?: RequestService;
96
+ params?: MaybeRef<unknown> | unknown;
97
+ formatData?: (res: unknown) => T[];
109
98
  align?: 'left' | 'center' | 'right';
110
99
  headerAlign?: 'left' | 'center' | 'right';
100
+ paginationConfig?: boolean | PaginationOptions;
101
+ tableWrapStyle?: CSSProperties;
102
+ searchFormConfig?: FormOptions<T>;
111
103
  }
112
104
  type TableReturn = [Component];
113
105
  declare function useTable<T extends DefaultRow>(options: TableOptions<T>): TableReturn;
114
106
  //#endregion
115
- export { AsyncConfirmOptions, CustomRender, DefaultComponentKey, FormItemType, FormOptions, FormReturnType, FormRuleFn, FormSchema, Recordable, TableColumn, TableOptions, TableReturn, setDeepProperty, useAsyncConfirm, useForm, useMessage, useTable };
107
+ export { AsyncConfirmOptions, CustomRender, DefaultComponentKey, FormItemType, FormOptions, FormReturnType, FormRuleFn, FormSchema, PaginationOptions, Recordable, TableColumnOptions, TableOptions, TableReturn, setDeepProperty, useAsyncConfirm, useForm, useMessage, useTable };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useRequest } from "@vuetkit/core";
2
- import { ElCascader, ElCheckbox, ElCol, ElColorPicker, ElDatePicker, ElForm, ElFormItem, ElInput, ElInputNumber, ElInputOtp, ElMention, ElMessage, ElMessageBox, ElRadio, ElRate, ElRow, ElSelect, ElSelectV2, ElSlider, ElSwitch, ElTable, ElTableColumn, ElTimePicker, ElTimeSelect, ElTransfer, ElTreeSelect, ElUpload } from "element-plus";
3
- import { isFunc } from "@vuetkit/shared";
4
- import { computed, defineComponent, h, nextTick, onMounted, ref, useTemplateRef } from "vue";
2
+ import { ElButton, ElCascader, ElCheckbox, ElCol, ElColorPicker, ElDatePicker, ElForm, ElFormItem, ElInput, ElInputNumber, ElInputOtp, ElMention, ElMessage, ElMessageBox, ElPagination, ElRadio, ElRate, ElRow, ElSelect, ElSelectV2, ElSlider, ElSwitch, ElTable, ElTableColumn, ElTimePicker, ElTimeSelect, ElTransfer, ElTreeSelect, ElUpload, vLoading } from "element-plus";
3
+ import { isFunc, realObj } from "@vuetkit/shared";
4
+ import { computed, defineComponent, h, nextTick, onMounted, reactive, ref, toValue, useTemplateRef, watch, withDirectives } from "vue";
5
5
  //#region src/feedback/useMessage/index.ts
6
6
  const DEFAULT_DURATION = 3e3;
7
7
  const DEFAULT_SHOW_CLOSE = true;
@@ -112,7 +112,9 @@ function setDeepProperty(data, propArr, value) {
112
112
  temp[propArr[propArr.length - 1]] = value;
113
113
  }
114
114
  function useForm(options) {
115
- const { schemas = [], rules = {}, colSpan = 24, defaultData, disabled = false, inline = false, size, rowGutter = 20, labelWidth, labelSuffix = "", labelPosition = "left", "validate-on-rule-change": validateOnRuleChange = true, enterCallback, customComponent, "scrollTo-error": scrollToError = false, "scroll-into-view-options": scrollIntoViewOptions = true, "status-icon": statusIcon = false, "require-asterisk-position": requireAsteriskPosition = "left", "show-message": showMessage = true, "inline-message": inlineMessage = false } = options || {};
115
+ var _options$schemas;
116
+ if (!options || !((_options$schemas = options.schemas) === null || _options$schemas === void 0 ? void 0 : _options$schemas.length)) return [null, {}];
117
+ const { schemas = [], rules = {}, colSpan = 24, defaultData, disabled = false, inline = false, size, rowGutter = 20, labelWidth, labelSuffix = "", labelPosition = "left", "validate-on-rule-change": validateOnRuleChange = true, enterCallback, customComponent, "scrollTo-error": scrollToError = false, "scroll-into-view-options": scrollIntoViewOptions = true, "status-icon": statusIcon = false, "require-asterisk-position": requireAsteriskPosition = "left", "show-message": showMessage = true, "inline-message": inlineMessage = false } = options;
116
118
  const components = {};
117
119
  if (customComponent) Object.assign(components, defaultComponent, customComponent);
118
120
  else Object.assign(components, defaultComponent);
@@ -272,14 +274,41 @@ function useForm(options) {
272
274
  //#endregion
273
275
  //#region src/table/useTable/index.ts
274
276
  function useTable(options) {
275
- const { columns, align, headerAlign, ...rest } = options;
277
+ const { columns = [], service, params, formatData, align, headerAlign, paginationConfig, searchFormConfig, tableWrapStyle = {}, data = [], ...rest } = options;
278
+ if ((paginationConfig || searchFormConfig) && !realObj(toValue(params))) throw new Error("params must be an object when paginationConfig or searchFormConfig is provided");
276
279
  return [defineComponent((props, { slots }) => {
280
+ const pageInfo = reactive({
281
+ pageSize: 10,
282
+ currentPage: 1
283
+ });
284
+ const [SearchForm, { reset, getData }] = searchFormConfig ? useForm(searchFormConfig) : [null, {
285
+ reset: () => {},
286
+ getData: () => ({})
287
+ }];
288
+ const requestParams = computed(() => {
289
+ if (paginationConfig) return {
290
+ ...toValue(params) || {},
291
+ ...searchFormConfig ? (getData === null || getData === void 0 ? void 0 : getData()) || {} : {},
292
+ currentPage: pageInfo.currentPage,
293
+ pageSize: pageInfo.pageSize
294
+ };
295
+ if (searchFormConfig) return {
296
+ ...toValue(params) || {},
297
+ ...(getData === null || getData === void 0 ? void 0 : getData()) || {}
298
+ };
299
+ return toValue(params);
300
+ });
301
+ const { data: asyncRequestData, loading, execute } = useRequest(service, {
302
+ manual: false,
303
+ defaultParams: requestParams.value,
304
+ formatData
305
+ });
277
306
  const renderTableItemNodes = (column) => {
278
307
  var _column$children;
279
308
  if (isFunc(column.render)) return { default: ({ row }) => column.render(row) };
280
309
  const tableItemNodes = [];
281
310
  if (column === null || column === void 0 || (_column$children = column.children) === null || _column$children === void 0 ? void 0 : _column$children.length) tableItemNodes.push(...renderTableItems(column.children));
282
- return () => tableItemNodes;
311
+ return tableItemNodes.length ? () => tableItemNodes : void 0;
283
312
  };
284
313
  const renderTableItem = (column) => {
285
314
  const { align: columnAlign, headerAlign: columnHeaderAlign } = column;
@@ -296,10 +325,48 @@ function useTable(options) {
296
325
  return renderTableItem(column);
297
326
  });
298
327
  };
299
- return () => {
300
- return h(ElTable, {
328
+ function executeRequest() {
329
+ if (service) execute(requestParams.value);
330
+ }
331
+ onMounted(() => {
332
+ if (service) execute();
333
+ });
334
+ const handleReset = () => {
335
+ reset === null || reset === void 0 || reset();
336
+ executeRequest();
337
+ };
338
+ const handleSearch = () => {
339
+ executeRequest();
340
+ };
341
+ const defaultPaginationLayout = "total, sizes, prev, pager, next, jumper";
342
+ const defaultPaginationWrapStyle = {
343
+ display: "flex",
344
+ justifyContent: "flex-end",
345
+ alignItems: "center",
346
+ margin: "20px"
347
+ };
348
+ watch(() => toValue(params), () => {
349
+ executeRequest();
350
+ }, { deep: true });
351
+ watch(() => [pageInfo.currentPage, pageInfo.pageSize], () => {
352
+ executeRequest();
353
+ });
354
+ const tableData = computed(() => {
355
+ if (paginationConfig) {
356
+ var _asyncRequestData$val;
357
+ return ((_asyncRequestData$val = asyncRequestData.value) === null || _asyncRequestData$val === void 0 ? void 0 : _asyncRequestData$val.data) || [];
358
+ }
359
+ return asyncRequestData.value || data || [];
360
+ });
361
+ const tableTotal = computed(() => {
362
+ var _asyncRequestData$val2;
363
+ return ((_asyncRequestData$val2 = asyncRequestData.value) === null || _asyncRequestData$val2 === void 0 ? void 0 : _asyncRequestData$val2.total) || 0;
364
+ });
365
+ const renderTable = () => {
366
+ return withDirectives(h(ElTable, {
301
367
  ...rest,
302
- ...props
368
+ ...props,
369
+ data: tableData.value
303
370
  }, {
304
371
  default: () => {
305
372
  const tableNodes = [];
@@ -309,7 +376,46 @@ function useTable(options) {
309
376
  },
310
377
  append: slots === null || slots === void 0 ? void 0 : slots.append,
311
378
  empty: slots === null || slots === void 0 ? void 0 : slots.empty
312
- });
379
+ }), [[vLoading, loading.value]]);
380
+ };
381
+ const renderPagination = () => {
382
+ return h("div", { style: paginationConfig === true ? defaultPaginationWrapStyle : (paginationConfig === null || paginationConfig === void 0 ? void 0 : paginationConfig.wrapStyle) || defaultPaginationWrapStyle }, [h(ElPagination, {
383
+ "currentPage": pageInfo.currentPage,
384
+ "pageSize": pageInfo.pageSize,
385
+ "total": tableTotal.value,
386
+ ...paginationConfig === true ? {} : paginationConfig,
387
+ "layout": paginationConfig === true ? defaultPaginationLayout : (paginationConfig === null || paginationConfig === void 0 ? void 0 : paginationConfig.layout) || defaultPaginationLayout,
388
+ "onUpdate:currentPage": (val) => {
389
+ pageInfo.currentPage = val;
390
+ },
391
+ "onUpdate:pageSize": (val) => {
392
+ pageInfo.pageSize = val;
393
+ }
394
+ })]);
395
+ };
396
+ function renderSearchForm() {
397
+ return h(SearchForm, {}, { footer: () => {
398
+ return h(ElFormItem, {}, () => [h(ElButton, {
399
+ type: "default",
400
+ size: (searchFormConfig === null || searchFormConfig === void 0 ? void 0 : searchFormConfig.size) || "small",
401
+ onClick: handleReset
402
+ }, () => "Reset"), h(ElButton, {
403
+ type: "primary",
404
+ size: (searchFormConfig === null || searchFormConfig === void 0 ? void 0 : searchFormConfig.size) || "small",
405
+ onClick: handleSearch
406
+ }, () => "Search")]);
407
+ } });
408
+ }
409
+ function renderTableWrapChildNodes() {
410
+ const childNodes = [];
411
+ if (searchFormConfig) childNodes.push(renderSearchForm());
412
+ if (slots === null || slots === void 0 ? void 0 : slots.header) childNodes.push(slots.header());
413
+ childNodes.push(renderTable());
414
+ if (paginationConfig) childNodes.push(renderPagination());
415
+ return childNodes;
416
+ }
417
+ return () => {
418
+ return h("div", { style: { ...tableWrapStyle || {} } }, renderTableWrapChildNodes());
313
419
  };
314
420
  })];
315
421
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vuetkit/components",
3
3
  "type": "module",
4
- "version": "0.0.10",
4
+ "version": "0.0.11",
5
5
  "description": "Collection of business development components for Vue3 projects",
6
6
  "author": "Kalu5",
7
7
  "license": "MIT",
@@ -21,7 +21,8 @@
21
21
  "vue": "^3.5.35"
22
22
  },
23
23
  "dependencies": {
24
- "@vuetkit/shared": "0.0.10"
24
+ "@vuetkit/core": "0.0.11",
25
+ "@vuetkit/shared": "0.0.11"
25
26
  },
26
27
  "scripts": {
27
28
  "build": "tsdown --config-loader tsx"