@tmagic/form 1.5.0-beta.0 → 1.5.0-beta.10

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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
- "version": "1.5.0-beta.0",
2
+ "version": "1.5.0-beta.10",
3
3
  "name": "@tmagic/form",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
+ "dist/style.css",
6
7
  "src/theme/*"
7
8
  ],
8
9
  "main": "dist/tmagic-form.umd.cjs",
@@ -36,6 +37,7 @@
36
37
  },
37
38
  "dependencies": {
38
39
  "@element-plus/icons-vue": "^2.3.1",
40
+ "dayjs": "^1.11.11",
39
41
  "lodash-es": "^4.17.21",
40
42
  "sortablejs": "^1.15.2"
41
43
  },
@@ -43,18 +45,18 @@
43
45
  "@types/lodash-es": "^4.17.4",
44
46
  "@types/node": "^18.19.0",
45
47
  "@types/sortablejs": "^1.15.8",
46
- "@vitejs/plugin-vue": "^5.1.1",
47
- "@vue/compiler-sfc": "^3.4.27",
48
+ "@vitejs/plugin-vue": "^5.1.3",
49
+ "@vue/compiler-sfc": "^3.5.0",
48
50
  "@vue/test-utils": "^2.4.6",
49
51
  "rimraf": "^3.0.2",
50
- "sass": "^1.77.0",
51
- "vite": "^5.3.5"
52
+ "sass": "^1.78.0",
53
+ "vite": "^5.4.3"
52
54
  },
53
55
  "peerDependencies": {
54
- "vue": "^3.4.35",
56
+ "vue": "^3.5.0",
55
57
  "typescript": "*",
56
- "@tmagic/design": "1.5.0-beta.0",
57
- "@tmagic/utils": "1.5.0-beta.0"
58
+ "@tmagic/utils": "1.5.0-beta.10",
59
+ "@tmagic/design": "1.5.0-beta.10"
58
60
  },
59
61
  "peerDependenciesMeta": {
60
62
  "typescript": {
@@ -1,8 +1,8 @@
1
1
  <template>
2
2
  <div
3
3
  v-if="config"
4
- :id="config.id"
5
- :data-magic-id="config.id"
4
+ :data-tmagic-id="config.id"
5
+ :data-tmagic-form-item-prop="itemProp"
6
6
  :style="config.tip ? 'display: flex;align-items: baseline;' : ''"
7
7
  :class="`m-form-container m-container-${type || ''} ${config.className || ''}`"
8
8
  >
@@ -37,7 +37,7 @@
37
37
  <template v-else-if="type && display && !showDiff">
38
38
  <TMagicFormItem
39
39
  :style="config.tip ? 'flex: 1' : ''"
40
- :class="{ hidden: `${itemLabelWidth}` === '0' || !text }"
40
+ :class="{ 'tmagic-form-hidden': `${itemLabelWidth}` === '0' || !text }"
41
41
  :prop="itemProp"
42
42
  :label-width="itemLabelWidth"
43
43
  :rules="rule"
@@ -93,7 +93,7 @@
93
93
  <!-- 上次内容 -->
94
94
  <TMagicFormItem
95
95
  :style="config.tip ? 'flex: 1' : ''"
96
- :class="{ hidden: `${itemLabelWidth}` === '0' || !text }"
96
+ :class="{ 'tmagic-form-hidden': `${itemLabelWidth}` === '0' || !text }"
97
97
  :prop="itemProp"
98
98
  :label-width="itemLabelWidth"
99
99
  :rules="rule"
@@ -142,7 +142,7 @@
142
142
  <!-- 当前内容 -->
143
143
  <TMagicFormItem
144
144
  :style="config.tip ? 'flex: 1' : ''"
145
- :class="{ hidden: `${itemLabelWidth}` === '0' || !text }"
145
+ :class="{ 'tmagic-form-hidden': `${itemLabelWidth}` === '0' || !text }"
146
146
  :prop="itemProp"
147
147
  :label-width="itemLabelWidth"
148
148
  :rules="rule"
@@ -206,7 +206,7 @@ import {
206
206
  } from '@tmagic/design';
207
207
  import { asyncLoadJs, sleep } from '@tmagic/utils';
208
208
 
209
- import { ColumnConfig, FormState, SortProp, TableConfig } from '../schema';
209
+ import { FormState, SortProp, TableColumnConfig, TableConfig } from '../schema';
210
210
  import { display as displayFunc, initValue } from '../utils/form';
211
211
 
212
212
  import Container from './Container.vue';
@@ -484,7 +484,7 @@ const toggleRowSelection = (row: any, selected: boolean) => {
484
484
  tMagicTable.value?.toggleRowSelection.call(tMagicTable.value, row, selected);
485
485
  };
486
486
 
487
- const makeConfig = (config: ColumnConfig, row: any) => {
487
+ const makeConfig = (config: TableColumnConfig, row: any) => {
488
488
  const newConfig = cloneDeep(config);
489
489
  if (typeof config.itemsFunction === 'function') {
490
490
  newConfig.items = config.itemsFunction(row);
@@ -500,6 +500,7 @@ const upHandler = (index: number) => {
500
500
 
501
501
  timer = setTimeout(() => {
502
502
  swapArray(index, index - 1);
503
+ timer = undefined;
503
504
  }, 300);
504
505
  };
505
506
 
@@ -525,6 +526,7 @@ const downHandler = (index: number) => {
525
526
 
526
527
  timer = setTimeout(() => {
527
528
  swapArray(index, index + 1);
529
+ timer = undefined;
528
530
  }, 300);
529
531
  };
530
532
 
@@ -73,7 +73,7 @@
73
73
  import { computed, inject, ref, watchEffect } from 'vue';
74
74
  import { cloneDeep, isEmpty } from 'lodash-es';
75
75
 
76
- import { getConfig, TMagicBadge } from '@tmagic/design';
76
+ import { getDesignConfig, TMagicBadge } from '@tmagic/design';
77
77
 
78
78
  import { FormState, TabConfig, TabPaneConfig } from '../schema';
79
79
  import { display as displayFunc, filterFunction } from '../utils/form';
@@ -88,8 +88,8 @@ type DiffCount = {
88
88
  [tabIndex: number]: number;
89
89
  };
90
90
 
91
- const tabPaneComponent = getConfig('components')?.tabPane;
92
- const tabsComponent = getConfig('components')?.tabs;
91
+ const tabPaneComponent = getDesignConfig('components')?.tabPane;
92
+ const tabsComponent = getDesignConfig('components')?.tabs;
93
93
 
94
94
  const getActive = (mForm: FormState | undefined, props: any, activeTabName: string) => {
95
95
  const { config, model, prop } = props;
@@ -5,17 +5,17 @@
5
5
  :size="size"
6
6
  :placeholder="config.placeholder"
7
7
  :disabled="disabled"
8
- :format="config.format"
9
- :value-format="config.valueFormat || 'YYYY/MM/DD HH:mm:ss'"
8
+ :format="config.format || 'YYYY/MM/DD'"
9
+ :value-format="config.valueFormat || 'YYYY/MM/DD'"
10
10
  @change="changeHandler"
11
11
  ></TMagicDatePicker>
12
12
  </template>
13
13
 
14
14
  <script lang="ts" setup>
15
15
  import { TMagicDatePicker } from '@tmagic/design';
16
- import { datetimeFormatter } from '@tmagic/utils';
17
16
 
18
17
  import type { DateConfig, FieldProps } from '../schema';
18
+ import { datetimeFormatter } from '../utils/form';
19
19
  import { useAddField } from '../utils/useAddField';
20
20
 
21
21
  defineOptions({
@@ -30,7 +30,7 @@ const emit = defineEmits<{
30
30
 
31
31
  useAddField(props.prop);
32
32
 
33
- props.model[props.name] = datetimeFormatter(props.model[props.name], '');
33
+ props.model[props.name] = datetimeFormatter(props.model[props.name], '', props.config.valueFormat || 'YYYY/MM/DD');
34
34
 
35
35
  const changeHandler = (v: string) => {
36
36
  emit('change', v);
@@ -15,9 +15,9 @@
15
15
 
16
16
  <script lang="ts" setup>
17
17
  import { TMagicDatePicker } from '@tmagic/design';
18
- import { datetimeFormatter } from '@tmagic/utils';
19
18
 
20
19
  import type { DateTimeConfig, FieldProps } from '../schema';
20
+ import { datetimeFormatter } from '../utils/form';
21
21
  import { useAddField } from '../utils/useAddField';
22
22
 
23
23
  defineOptions({
@@ -37,7 +37,11 @@ if (props.model) {
37
37
  if (value === 'Invalid Date') {
38
38
  props.model[props.name] = '';
39
39
  } else {
40
- props.model[props.name] = datetimeFormatter(props.model[props.name], '', props.config.valueFormat);
40
+ props.model[props.name] = datetimeFormatter(
41
+ props.model[props.name],
42
+ '',
43
+ props.config.valueFormat || 'YYYY/MM/DD HH:mm:ss',
44
+ );
41
45
  }
42
46
  }
43
47
 
@@ -9,6 +9,9 @@
9
9
  :unlink-panels="true"
10
10
  :disabled="disabled"
11
11
  :default-time="config.defaultTime"
12
+ :value-format="config.valueFormat || 'YYYY/MM/DD HH:mm:ss'"
13
+ :date-format="config.dateFormat || 'YYYY/MM/DD'"
14
+ :time-format="config.timeFormat || 'HH:mm:ss'"
12
15
  @change="changeHandler"
13
16
  ></TMagicDatePicker>
14
17
  </template>
@@ -17,9 +20,9 @@
17
20
  import { ref, watch } from 'vue';
18
21
 
19
22
  import { TMagicDatePicker } from '@tmagic/design';
20
- import { datetimeFormatter } from '@tmagic/utils';
21
23
 
22
24
  import type { DaterangeConfig, FieldProps } from '../schema';
25
+ import { datetimeFormatter } from '../utils/form';
23
26
  import { useAddField } from '../utils/useAddField';
24
27
 
25
28
  defineOptions({
@@ -32,9 +35,8 @@ const emit = defineEmits(['change']);
32
35
 
33
36
  useAddField(props.prop);
34
37
 
35
- // eslint-disable-next-line vue/no-setup-props-destructure
36
38
  const { names } = props.config;
37
- const value = ref<(Date | undefined)[] | null>([]);
39
+ const value = ref<(Date | string | undefined)[] | null>([]);
38
40
 
39
41
  if (props.model !== undefined) {
40
42
  if (names?.length) {
@@ -44,9 +46,11 @@ if (props.model !== undefined) {
44
46
  if (!value.value) {
45
47
  value.value = [];
46
48
  }
49
+
50
+ const format = `${props.config.dateFormat || 'YYYY/MM/DD'} ${props.config.timeFormat || 'HH:mm:ss'}`;
47
51
  if (!start || !end) value.value = [];
48
- if (start !== preStart) value.value[0] = new Date(start);
49
- if (end !== preEnd) value.value[1] = new Date(end);
52
+ if (start !== preStart) value.value[0] = datetimeFormatter(start, '', format) as string;
53
+ if (end !== preEnd) value.value[1] = datetimeFormatter(end, '', format) as string;
50
54
  },
51
55
  {
52
56
  immediate: true,
@@ -56,7 +60,12 @@ if (props.model !== undefined) {
56
60
  watch(
57
61
  () => props.model[props.name],
58
62
  (start, preStart) => {
59
- if (start !== preStart) value.value = start.map((item: string) => (item ? new Date(item) : undefined));
63
+ const format = `${props.config.dateFormat || 'YYYY/MM/DD'} ${props.config.timeFormat || 'HH:mm:ss'}`;
64
+
65
+ if (start !== preStart)
66
+ value.value = start.map((item: string) =>
67
+ item ? (datetimeFormatter(item, '', format) as string) : undefined,
68
+ );
60
69
  },
61
70
  {
62
71
  immediate: true,
@@ -71,7 +80,7 @@ const setValue = (v: Date[] | Date) => {
71
80
  return;
72
81
  }
73
82
  if (Array.isArray(v)) {
74
- props.model[item] = datetimeFormatter(v[index]?.toString(), '');
83
+ props.model[item] = v[index];
75
84
  } else {
76
85
  props.model[item] = undefined;
77
86
  }
@@ -82,18 +91,12 @@ const changeHandler = (v: Date[]) => {
82
91
  const value = v || [];
83
92
 
84
93
  if (props.name) {
85
- emit(
86
- 'change',
87
- value.map((item?: Date) => {
88
- if (item) return datetimeFormatter(item, '');
89
- return undefined;
90
- }),
91
- );
94
+ emit('change', value);
92
95
  } else {
93
96
  if (names?.length) {
94
97
  setValue(value);
95
98
  }
96
- emit('change', value);
99
+ emit('change', props.model);
97
100
  }
98
101
  };
99
102
  </script>
@@ -80,7 +80,7 @@
80
80
  <script lang="ts" setup>
81
81
  import { inject, nextTick, onBeforeMount, ref, watch, watchEffect } from 'vue';
82
82
 
83
- import { getConfig as getDesignConfig, TMagicSelect } from '@tmagic/design';
83
+ import { getDesignConfig, TMagicSelect } from '@tmagic/design';
84
84
  import { getValueByKeyPath } from '@tmagic/utils';
85
85
 
86
86
  import type { FieldProps, FormState, SelectConfig, SelectGroupOption, SelectOption } from '../schema';
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <TMagicPopover :visible="popoverVisible" width="220px">
2
+ <TMagicPopover width="220px" :visible="popoverVisible" :destroy-on-close="true">
3
3
  <template #reference>
4
4
  <TMagicInput
5
5
  v-model="model[name]"
@@ -17,7 +17,6 @@
17
17
  import { ref, watch } from 'vue';
18
18
 
19
19
  import { TMagicTimePicker } from '@tmagic/design';
20
- import { datetimeFormatter } from '@tmagic/utils';
21
20
 
22
21
  import type { DaterangeConfig, FieldProps } from '../schema';
23
22
  import { useAddField } from '../utils/useAddField';
@@ -59,7 +58,7 @@ const setValue = (v: Date[] | Date) => {
59
58
  return;
60
59
  }
61
60
  if (Array.isArray(v)) {
62
- props.model[item] = datetimeFormatter(v[index]?.toString(), '');
61
+ props.model[item] = v[index];
63
62
  } else {
64
63
  props.model[item] = undefined;
65
64
  }
package/src/index.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { App } from 'vue';
19
+ import type { App } from 'vue';
20
20
 
21
21
  import Container from './containers/Container.vue';
22
22
  import Fieldset from './containers/Fieldset.vue';
@@ -91,15 +91,15 @@ export { default as MDynamicField } from './fields/DynamicField.vue';
91
91
 
92
92
  export const createForm = <T extends [] = []>(config: FormConfig | T) => config;
93
93
 
94
- export interface InstallOptions {
94
+ export interface FormInstallOptions {
95
95
  [key: string]: any;
96
96
  }
97
97
 
98
- const defaultInstallOpt: InstallOptions = {};
98
+ const defaultInstallOpt: FormInstallOptions = {};
99
99
 
100
100
  export default {
101
- install(app: App, opt: InstallOptions = {}) {
102
- const option = Object.assign(defaultInstallOpt, opt);
101
+ install(app: App, opt?: FormInstallOptions) {
102
+ const option = Object.assign(defaultInstallOpt, opt || {});
103
103
 
104
104
  // eslint-disable-next-line no-param-reassign
105
105
  app.config.globalProperties.$MAGIC_FORM = option;
package/src/schema.ts CHANGED
@@ -296,6 +296,9 @@ export interface DaterangeConfig extends FormItem {
296
296
  type: 'daterange';
297
297
  defaultTime?: Date[];
298
298
  names?: string[];
299
+ valueFormat?: string;
300
+ dateFormat?: string;
301
+ timeFormat?: string;
299
302
  }
300
303
 
301
304
  /**
@@ -632,7 +635,7 @@ export interface PanelConfig extends FormItem, ContainerCommonConfig {
632
635
  schematic?: string;
633
636
  }
634
637
 
635
- export interface ColumnConfig extends FormItem {
638
+ export interface TableColumnConfig extends FormItem {
636
639
  name?: string;
637
640
  label: string;
638
641
  width?: string | number;
@@ -645,9 +648,9 @@ export interface ColumnConfig extends FormItem {
645
648
  */
646
649
  export interface TableConfig extends FormItem {
647
650
  type: 'table' | 'groupList' | 'group-list';
648
- items: ColumnConfig[];
649
- tableItems?: ColumnConfig[];
650
- groupItems?: ColumnConfig[];
651
+ items: TableColumnConfig[];
652
+ tableItems?: TableColumnConfig[];
653
+ groupItems?: TableColumnConfig[];
651
654
  enableToggleMode?: boolean;
652
655
  /** 最大行数 */
653
656
  max?: number;
@@ -29,13 +29,13 @@
29
29
  margin-bottom: 10px;
30
30
  }
31
31
 
32
- .el-form-item.hidden {
32
+ .el-form-item.tmagic-form-hidden {
33
33
  > .el-form-item__label {
34
34
  display: none;
35
35
  }
36
36
  }
37
37
 
38
- .t-form__item.hidden {
38
+ .t-form__item.tmagic-form-hidden {
39
39
  > .t-form__label {
40
40
  display: none;
41
41
  }
package/src/utils/form.ts CHANGED
@@ -18,6 +18,8 @@
18
18
 
19
19
  /* eslint-disable no-param-reassign */
20
20
  import { toRaw } from 'vue';
21
+ import dayjs from 'dayjs';
22
+ import utc from 'dayjs/plugin/utc';
21
23
  import { cloneDeep } from 'lodash-es';
22
24
 
23
25
  import {
@@ -271,3 +273,28 @@ export const initValue = async (
271
273
 
272
274
  return valuesTmp || {};
273
275
  };
276
+
277
+ export const datetimeFormatter = (
278
+ v: string | Date,
279
+ defaultValue = '-',
280
+ format = 'YYYY-MM-DD HH:mm:ss',
281
+ ): string | number => {
282
+ if (v) {
283
+ let time: string | number;
284
+ if (['x', 'timestamp'].includes(format)) {
285
+ time = dayjs(v).valueOf();
286
+ } else if ((typeof v === 'string' && v.includes('Z')) || v.constructor === Date) {
287
+ dayjs.extend(utc);
288
+ // UTC字符串时间或Date对象格式化为北京时间
289
+ time = dayjs(v).utcOffset(8).format(format);
290
+ } else {
291
+ time = dayjs(v).format(format);
292
+ }
293
+
294
+ if (time !== 'Invalid Date') {
295
+ return time;
296
+ }
297
+ return defaultValue;
298
+ }
299
+ return defaultValue;
300
+ };