@ftjs/core 1.1.1 → 1.2.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.
@@ -1,5 +1,5 @@
1
1
  import { MaybeRefOrGetter, VNodeChild } from 'vue';
2
- import { RecordPath } from '../type-helper';
2
+ import { MaybeRefOrFormGetter, RecordPath } from '../type-helper';
3
3
  type WatchHandler<F extends Record<string, any>> = (params: {
4
4
  val: any;
5
5
  oldVal: any;
@@ -17,7 +17,7 @@ export interface FtFormColumnBase<F extends Record<string, any>> {
17
17
  /**
18
18
  * 字段名 `fields` 和 `field` 至少有一个存在
19
19
  *
20
- * `field` 优先级高于 `fields`
20
+ * `fields` 优先级高于 `field`
21
21
  *
22
22
  * 如果是在 TableColumns 中,则默认继承其中的 field
23
23
  */
@@ -41,15 +41,21 @@ export interface FtFormColumnBase<F extends Record<string, any>> {
41
41
  /**
42
42
  * 是否隐藏
43
43
  */
44
- hide?: MaybeRefOrGetter<boolean>;
44
+ hide?: MaybeRefOrFormGetter<boolean, F>;
45
45
  /**
46
46
  * 监听字段值变化,如果是 `fields` ,则只会监听第一个字段的值变化
47
47
  */
48
48
  watch?: Watch<F>;
49
49
  /**
50
50
  * 字段默认值
51
+ *
52
+ * @deprecated 请使用 `defaultValue` 代替
51
53
  */
52
54
  value?: any;
55
+ /**
56
+ * 字段默认值
57
+ */
58
+ defaultValue?: any;
53
59
  /**
54
60
  * 控制其他字段基于此值的显示规则
55
61
  *
@@ -93,7 +99,7 @@ export interface FtFormColumnBase<F extends Record<string, any>> {
93
99
  /**
94
100
  * 是否查看模式
95
101
  */
96
- isView?: MaybeRefOrGetter<boolean>;
102
+ isView?: MaybeRefOrFormGetter<boolean, F>;
97
103
  /**
98
104
  * 自定义查看模式下的渲染
99
105
  */
@@ -1,8 +1,14 @@
1
1
  import { ComputedRef } from 'vue';
2
2
  import { FtFormColumnBase } from './columns';
3
+ import { MaybeRefOrFormGetter } from '../type-helper';
3
4
  interface FormInject<F extends Record<string, any>> {
4
5
  form: ComputedRef<F>;
5
6
  }
7
+ export declare function isFormGetter<T, F extends Record<string, any>>(fn: MaybeRefOrFormGetter<T, F>): fn is (form: F) => T;
8
+ /**
9
+ * 将 MaybeRefOrFormGetter 转换为普通值
10
+ */
11
+ export declare function toValueWithForm<T extends Record<string, any>, R>(fn: MaybeRefOrFormGetter<R, T>, form: ComputedRef<T>): R;
6
12
  export declare const useFormInject: <F extends Record<string, any> = Record<string, any>>() => FormInject<F> | undefined;
7
13
  export interface FtBaseFormProps<F extends Record<string, any>> {
8
14
  /**
package/dist/index.js CHANGED
@@ -4,6 +4,9 @@ const getField = (column) => {
4
4
  var _a;
5
5
  return ((_a = column.fields) == null ? void 0 : _a[0]) ?? column.field;
6
6
  };
7
+ const getDefaultValues = (column) => {
8
+ return column.defaultValue ?? column.value;
9
+ };
7
10
  const isEmptyStrOrNull = (val) => {
8
11
  return val === "" || val == null;
9
12
  };
@@ -134,16 +137,25 @@ const isEqual = (a, b) => {
134
137
  (key) => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key])
135
138
  );
136
139
  };
140
+ function isFormGetter(fn) {
141
+ return typeof fn === "function";
142
+ }
137
143
  const provideFormKey = Symbol("@ftjs/core-form-provide");
144
+ function toValueWithForm(fn, form) {
145
+ if (isFormGetter(fn)) {
146
+ return fn(form.value);
147
+ }
148
+ return unref(fn);
149
+ }
138
150
  const useFormInject = () => {
139
151
  return inject(provideFormKey);
140
152
  };
141
- const useColumnsChecked = (columns, cache) => {
153
+ const useColumnsChecked = (columns, cache, form) => {
142
154
  const storageKey = `ftjs-form-columns-checked-obj`;
143
155
  const columnsV = computed(() => {
144
156
  const entries = columns.value.map((e) => {
145
157
  const field = getField(e);
146
- return [field, !toValue(e.hide)];
158
+ return [field, !toValueWithForm(e.hide, form)];
147
159
  });
148
160
  return Object.fromEntries(entries);
149
161
  });
@@ -168,7 +180,7 @@ const useColumnsChecked = (columns, cache) => {
168
180
  const entries = columns.value.map((e) => {
169
181
  const field = getField(e);
170
182
  const show = v.includes(field);
171
- if (show !== !toValue(e.hide)) storageV2[field] = show;
183
+ if (show !== !toValueWithForm(e.hide, form)) storageV2[field] = show;
172
184
  return [field, show];
173
185
  });
174
186
  vRef.value = Object.fromEntries(entries);
@@ -176,7 +188,7 @@ const useColumnsChecked = (columns, cache) => {
176
188
  }
177
189
  });
178
190
  const resetColumnsChecked = () => {
179
- columnsChecked.value = columns.value.filter((e) => !toValue(e.hide)).map((e) => getField(e));
191
+ columnsChecked.value = columns.value.filter((e) => !toValueWithForm(e.hide, form)).map((e) => getField(e));
180
192
  };
181
193
  return {
182
194
  columnsChecked,
@@ -237,7 +249,7 @@ const useColumnsSorted = (columns, cache) => {
237
249
  };
238
250
  const getFieldsAndValues = (column) => {
239
251
  const fields = column.fields || [column.field];
240
- const values = column.fields ? column.value || [] : [column.value];
252
+ const values = column.fields ? getDefaultValues(column) || [] : [getDefaultValues(column)];
241
253
  return { fields, values };
242
254
  };
243
255
  const useForm = (props) => {
@@ -259,7 +271,8 @@ const useForm = (props) => {
259
271
  });
260
272
  const { columnsChecked, resetColumnsChecked } = useColumnsChecked(
261
273
  columns,
262
- () => props.cache
274
+ () => props.cache,
275
+ form
263
276
  );
264
277
  const { columnsSort, resetColumnsSort } = useColumnsSorted(
265
278
  columns,
@@ -542,14 +555,17 @@ const useTable = (props) => {
542
555
  export {
543
556
  cloneDeep,
544
557
  get,
558
+ getDefaultValues,
545
559
  getField,
546
560
  getStorage,
547
561
  has,
548
562
  isBrowser,
549
563
  isEmptyStrOrNull,
550
564
  isEqual,
565
+ isFormGetter,
551
566
  set,
552
567
  setStorage,
568
+ toValueWithForm,
553
569
  unrefs,
554
570
  useForm,
555
571
  useFormInject,
@@ -35,4 +35,5 @@ export type Refs<T> = {
35
35
  export type ExtractColumnType<T> = T extends {
36
36
  type: infer U;
37
37
  } ? U : never;
38
+ export type MaybeRefOrFormGetter<T, F> = MaybeRef<T> | ((form: F) => T);
38
39
  export {};
package/dist/utils.d.ts CHANGED
@@ -2,6 +2,7 @@ import { RecordPath, Unrefs } from './type-helper';
2
2
  import { FtFormColumnBase } from './form/columns';
3
3
  export declare const isBrowser: boolean;
4
4
  export declare const getField: <T extends Record<string, any>>(column: FtFormColumnBase<T>) => RecordPath<T>;
5
+ export declare const getDefaultValues: <T extends Record<string, any>>(column: FtFormColumnBase<T>) => any;
5
6
  export declare const isEmptyStrOrNull: (val: any) => boolean;
6
7
  /**
7
8
  * 深拷贝(简化版)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftjs/core",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "keywords": [],
5
5
  "author": "",
6
6
  "license": "MIT",