Skip to content

Commit 0baa3cd

Browse files
committed
feat: 适配iview 时间日期
1 parent 2df6957 commit 0baa3cd

File tree

8 files changed

+119
-65
lines changed

8 files changed

+119
-65
lines changed

packages/lib/vue2-core/JsonSchemaForm/components/Widget.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default {
170170
// labelPosition left/right
171171
const miniDesModel = self.formProps && self.formProps.labelPosition !== 'top';
172172

173-
const descriptionVnode = (self.description) ? h(
173+
const descriptionVNode = (self.description) ? h(
174174
'p',
175175
{
176176
domProps: {
@@ -184,7 +184,7 @@ export default {
184184

185185
const { COMPONENT_MAP, ICONS_MAP } = self.globalOptions;
186186

187-
const miniDescriptionVNode = (miniDesModel && descriptionVnode) ? h(COMPONENT_MAP.popover, {
187+
const miniDescriptionVNode = (miniDesModel && descriptionVNode) ? h(COMPONENT_MAP.popover, {
188188
style: {
189189
margin: '0 2px',
190190
fontSize: '16px',
@@ -195,7 +195,7 @@ export default {
195195
trigger: 'hover'
196196
}
197197
}, [
198-
descriptionVnode,
198+
descriptionVNode,
199199
h('i', {
200200
slot: 'reference',
201201
class: ICONS_MAP.question
@@ -275,21 +275,21 @@ export default {
275275
},
276276
},
277277
[
278-
h('span', {
278+
self.label ? h('span', {
279279
slot: 'label',
280280
class: {
281281
genFormLabel: true,
282282
genFormItemRequired: self.required,
283283
},
284284
}, [
285-
`${self.label || ''}`,
286-
miniDescriptionVNode || null,
285+
`${self.label}`,
286+
miniDescriptionVNode,
287287
`${(self.formProps && self.formProps.labelSuffix) || ''}`
288-
]),
288+
]) : null,
289289

290290
// description
291291
// 非mini模式显示 description
292-
!miniDesModel ? descriptionVnode : null,
292+
!miniDesModel ? descriptionVNode : null,
293293
h( // 关键输入组件
294294
self.widget,
295295
{

packages/lib/vue2-core/JsonSchemaForm/index.css

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
h2,
1414
h3,
1515
p {
16-
padding: 0;
17-
margin: 0;
1816
font-size: 14px;
1917
}
2018
.genFormBtn {

packages/lib/vue2-form-iview3/src/config/widgets/CheckboxesWidget/index.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<el-checkbox-group v-model="checkList" v-bind="$attrs">
3-
<el-checkbox v-for="(item, index) in enumOptions" :key="index" :label="item.value">{{ item.label }}</el-checkbox>
4-
</el-checkbox-group>
2+
<checkbox-group v-model="checkList" v-bind="$attrs">
3+
<checkbox v-for="(item, index) in enumOptions" :key="index" :label="item.value">{{ item.label }}</checkbox>
4+
</checkbox-group>
55
</template>
66

77
<script>

packages/lib/vue2-form-iview3/src/config/widgets/DatePickerWidget/index.js

+96-16
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,107 @@
22
* Created by Liu.Jun on 2020/7/22 13:21.
33
*/
44

5+
const f = s => `0${s}`.substr(-2);
6+
7+
function formatDateStr(date, isDatetime) {
8+
if (!date) return '';
9+
10+
const dateObj = new Date(date);
11+
12+
if (isDatetime) return dateObj.toISOString();
13+
14+
const { year, month, day } = {
15+
year: dateObj.getFullYear(),
16+
month: dateObj.getMonth() + 1,
17+
day: dateObj.getDate(),
18+
};
19+
return `${year}-${f(month)}-${f(day)}`;
20+
}
21+
22+
function isEmptyValue(value) {
23+
return value === null || value === '' || (Array.isArray(value) && value.every(item => item === ''));
24+
}
25+
526
export default {
627
name: 'DatePickerWidget',
7-
functional: true,
8-
render(h, context) {
9-
const { isNumberValue, isRange, ...otherProps } = context.data.attrs || {};
10-
11-
context.data.attrs = {
12-
type: isRange ? 'daterange' : 'date',
13-
'value-format': isNumberValue ? 'timestamp' : 'yyyy-MM-dd',
14-
...otherProps
28+
props: {
29+
value: {
30+
type: null
31+
},
32+
isNumberValue: {
33+
type: Boolean,
34+
default: false
35+
},
36+
isDatetime: {
37+
type: Boolean,
38+
default: false
39+
},
40+
isRange: {
41+
type: Boolean,
42+
default: false
43+
}
44+
},
45+
data() {
46+
return {
47+
originValue: this.value,
48+
formatValue: this.formatDate(this.value)
1549
};
50+
},
51+
watch: {
52+
value(newVal) {
53+
// 兼容 iview 绑定字符串类型值会导致无限循环的问题
1654

17-
const oldInputCall = context.data.on.input;
18-
context.data.on = {
19-
...context.data.on,
20-
input(val) {
21-
const trueVal = val === null ? (isRange ? [] : undefined) : val;
22-
oldInputCall.apply(context.data.on, [trueVal]);
55+
if (newVal === this.formatValue) {
56+
// 内部date-picker选择框更新
57+
} else {
58+
// 外部更新值
59+
this.originValue = newVal;
2360
}
24-
};
61+
}
62+
},
63+
computed: {
64+
type() {
65+
return this.isDatetime
66+
? (this.isRange ? 'datetimerange' : 'datetime')
67+
: (this.isRange ? 'daterange' : 'date');
68+
}
69+
},
70+
methods: {
71+
formatDate(val) {
72+
const { isRange, isNumberValue, isDatetime } = this.$props;
2573

26-
return h('el-date-picker', context.data, context.children);
74+
let trueVal;
75+
if (isRange) {
76+
trueVal = isEmptyValue(val)
77+
? []
78+
: val.map(
79+
item => (isNumberValue ? (new Date(item)).valueOf() : formatDateStr(item, isDatetime))
80+
);
81+
} else {
82+
trueVal = isEmptyValue(val)
83+
? undefined
84+
: isNumberValue ? (new Date(val)).valueOf() : formatDateStr(val, isDatetime);
85+
}
86+
return trueVal;
87+
}
88+
},
89+
render(h) {
90+
const self = this;
91+
return h('date-picker', {
92+
attrs: {
93+
type: this.type,
94+
value: this.originValue,
95+
...this.$attrs
96+
},
97+
on: {
98+
...this.$listeners,
99+
input(val) {
100+
self.originValue = val;
101+
self.formatValue = self.formatDate(val);
102+
103+
self.$emit('input', self.formatValue);
104+
}
105+
}
106+
});
27107
}
28108
};

packages/lib/vue2-form-iview3/src/config/widgets/DateTimePickerWidget/index.js

+4-23
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,14 @@
22
* Created by Liu.Jun on 2020/7/22 13:21.
33
*/
44

5+
import DatePickerWidget from '../DatePickerWidget';
6+
57
export default {
68
name: 'DateTimePickerWidget',
79
functional: true,
810
render(h, context) {
9-
const { isNumberValue, isRange, ...otherProps } = context.data.attrs || {};
10-
11-
context.data.attrs = {
12-
type: isRange ? 'datetimerange' : 'datetime',
13-
...otherProps
14-
};
15-
16-
// 字符串为 0 时区ISO标准时间
17-
const oldInputCall = context.data.on.input;
18-
context.data.on = {
19-
...context.data.on,
20-
input(val) {
21-
let trueVal;
22-
if (isRange) {
23-
trueVal = (val === null) ? [] : val.map(item => (new Date(item))[isNumberValue ? 'valueOf' : 'toISOString']());
24-
} else {
25-
trueVal = (val === null) ? undefined : (new Date(val))[isNumberValue ? 'valueOf' : 'toISOString']();
26-
}
27-
28-
oldInputCall.apply(context.data.on, [trueVal]);
29-
}
30-
};
11+
context.data.attrs.isDatetime = true;
3112

32-
return h('el-date-picker', context.data, context.children);
13+
return h(DatePickerWidget, context.data, context.children);
3314
}
3415
};

packages/lib/vue2-form-iview3/src/config/widgets/RadioWidget/index.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<el-radio-group v-model="checkList" v-bind="$attrs">
3-
<el-radio v-for="(item, index) in enumOptions" :key="index" :label="item.value">{{ item.label }}</el-radio>
4-
</el-radio-group>
2+
<radio-group v-model="checkList" v-bind="$attrs">
3+
<radio v-for="(item, index) in enumOptions" :key="index" :label="item.value">{{ item.label }}</radio>
4+
</radio-group>
55
</template>
66

77
<script>

packages/lib/vue2-form-iview3/src/config/widgets/SelectWidget/index.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
2-
<el-select v-model="selectList"
3-
v-bind="$attrs"
2+
<i-select v-model="selectList"
3+
v-bind="$attrs"
44
>
5-
<el-option v-for="(item, index) in enumOptions" :key="index" :label="item.label" :value="item.value"></el-option>
6-
</el-select>
5+
<i-option v-for="(item, index) in enumOptions" :key="index" :value="item.value">{{ item.label }}</i-option>
6+
</i-select>
77
</template>
88

99
<script>

packages/lib/vue2-form-iview3/src/config/widgets/TimePickerWidget/index.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ export default {
66
name: 'TimePickerWidget',
77
functional: true,
88
render(h, context) {
9-
context.data.attrs = {
10-
'value-format': 'HH:mm:ss',
11-
...context.data.attrs || {}
12-
};
13-
149
const oldInputCall = context.data.on.input;
1510
context.data.on = {
1611
...context.data.on,
@@ -19,6 +14,6 @@ export default {
1914
}
2015
};
2116

22-
return h('el-time-picker', context.data, context.children);
17+
return h('time-picker', context.data, context.children);
2318
}
2419
};

0 commit comments

Comments
 (0)