Skip to content

Commit a159c78

Browse files
committed
feat(lib): 添加vue3 naiveui支持
ref #152
1 parent 10cdc08 commit a159c78

File tree

14 files changed

+254
-235
lines changed

14 files changed

+254
-235
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
3+
*/
4+
5+
import { h } from 'vue';
6+
import { resolveComponent, modelValueComponent } from '@lljj/vjsf-utils/vue3Utils';
7+
8+
const baseComponent = {
9+
name: 'CheckboxesWidget',
10+
props: {
11+
enumOptions: {
12+
default: () => [],
13+
type: [Array]
14+
}
15+
},
16+
setup(props, { attrs }) {
17+
return () => h(resolveComponent('n-checkbox-group'), attrs, {
18+
default() {
19+
return h(resolveComponent('n-space'), {
20+
itemStyle: 'display: flex'
21+
}, {
22+
default() {
23+
return props.enumOptions.map((item, index) => h(resolveComponent('n-checkbox'), {
24+
key: index,
25+
value: item.value
26+
}, {
27+
default: () => item.label
28+
}));
29+
}
30+
});
31+
}
32+
});
33+
}
34+
};
35+
36+
const moduleValeComponent = modelValueComponent(baseComponent, {
37+
model: 'value'
38+
});
39+
40+
export default moduleValeComponent;

packages/lib/vue3/vue3-form-naive/src/config/widgets/CheckboxesWidget/index.vue

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
11
/**
2-
* Created by Liu.Jun on 2020/7/22 13:21.
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
33
*/
44

55
import { h } from 'vue';
66
import { resolveComponent } from '@lljj/vjsf-utils/vue3Utils';
77

8-
import { parseDateString } from '@lljj/vjsf-utils/utils';
9-
10-
function isEmptyValue(value) {
11-
return value === null || value === '' || (Array.isArray(value) && value.every(item => item === ''));
12-
}
13-
14-
const formatDateStr = (dateString) => {
15-
const {
16-
year,
17-
month,
18-
day
19-
} = parseDateString(dateString, false);
20-
return `${year}-${month}-${day}`;
21-
};
22-
23-
export default {
8+
const baseComponent = {
249
name: 'DatePickerWidget',
2510
inheritAttrs: false,
26-
setup(props, { attrs, slots }) {
11+
setup(props, { attrs }) {
2712
return () => {
28-
const { isNumberValue, isRange, ...otherProps } = attrs || {};
29-
return h(resolveComponent('el-date-picker'), {
13+
const {
14+
isNumberValue, isRange, modelValue, 'onUpdate:modelValue': onUpdateFormattedValue, ...otherAttrs
15+
} = attrs;
16+
const trueValue = isRange ? (modelValue && modelValue.length === 0 ? null : modelValue) : modelValue;
17+
18+
return h(resolveComponent('n-date-picker'), {
3019
type: isRange ? 'daterange' : 'date',
31-
...otherProps,
32-
'onUpdate:modelValue': (val) => {
33-
let trueVal;
34-
if (isRange) {
35-
trueVal = isEmptyValue(val)
36-
? []
37-
: val.map(
38-
item => (isNumberValue ? (new Date(item)).valueOf() : formatDateStr(item, isNumberValue))
39-
);
40-
} else {
41-
trueVal = isEmptyValue(val)
42-
? undefined
43-
: isNumberValue ? (new Date(val)).valueOf() : formatDateStr(val, isNumberValue);
44-
}
45-
attrs['onUpdate:modelValue'].apply(attrs, [trueVal]);
20+
...otherAttrs,
21+
...isNumberValue ? {
22+
value: trueValue,
23+
onUpdateValue: onUpdateFormattedValue
24+
} : {
25+
valueFormat: isNumberValue ? 'T' : 'yyyy-MM-dd',
26+
formattedValue: trueValue,
27+
onUpdateFormattedValue,
4628
}
47-
}, slots);
29+
});
4830
};
4931
}
5032
};
33+
34+
export default baseComponent;
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
/**
2-
* Created by Liu.Jun on 2020/7/22 13:21.
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
33
*/
44

55
import { h } from 'vue';
66
import { resolveComponent } from '@lljj/vjsf-utils/vue3Utils';
77

8-
export default {
9-
name: 'DateTimePickerWidget',
8+
const baseComponent = {
9+
name: 'DatePickerWidget',
1010
inheritAttrs: false,
11-
setup(props, { attrs, slots }) {
12-
const trueValue = (isRange, isNumberValue, val) => {
13-
if (isRange) {
14-
return (val === null) ? [] : val.map(item => (new Date(item))[isNumberValue ? 'valueOf' : 'toISOString']());
15-
}
16-
return (val === null) ? undefined : (new Date(val))[isNumberValue ? 'valueOf' : 'toISOString']();
17-
};
18-
11+
setup(props, { attrs }) {
1912
return () => {
20-
const { isNumberValue, isRange, ...otherProps } = attrs || {};
21-
return h(resolveComponent('el-date-picker'), {
22-
type: isRange ? 'datetimerange' : 'datetime',
23-
...otherProps,
13+
const {
14+
isNumberValue, isRange, modelValue, 'onUpdate:modelValue': onUpdateFormattedValue, ...otherAttrs
15+
} = attrs;
16+
const trueValue = isRange ? (modelValue && modelValue.length === 0 ? null : modelValue) : modelValue;
2417

25-
'onUpdate:modelValue': (val) => {
26-
const trueVal = trueValue(isRange, isNumberValue, val);
27-
28-
attrs['onUpdate:modelValue'].apply(attrs, [trueVal]);
18+
return h(resolveComponent('n-date-picker'), {
19+
type: isRange ? 'datetimerange' : 'datetime',
20+
...otherAttrs,
21+
...isNumberValue ? {
22+
value: trueValue,
23+
onUpdateValue: onUpdateFormattedValue
24+
} : {
25+
valueFormat: isNumberValue ? 'T' : 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'',
26+
formattedValue: trueValue,
27+
onUpdateFormattedValue,
2928
}
30-
}, slots);
29+
});
3130
};
3231
}
3332
};
33+
34+
export default baseComponent;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
3+
*/
4+
5+
import { h } from 'vue';
6+
import { modelValueComponent, resolveComponent } from '@lljj/vjsf-utils/vue3Utils';
7+
8+
const baseComponent = {
9+
name: 'RadioWidget',
10+
props: {
11+
enumOptions: {
12+
default: () => [],
13+
type: [Array]
14+
}
15+
},
16+
setup(props, { attrs }) {
17+
return () => h(resolveComponent('n-radio-group'), attrs, {
18+
default() {
19+
return props.enumOptions.map((item, index) => h(resolveComponent('n-radio'), {
20+
key: index,
21+
value: item.value
22+
}, {
23+
default: () => item.label
24+
}));
25+
}
26+
});
27+
}
28+
};
29+
30+
const moduleValeComponent = modelValueComponent(baseComponent, {
31+
model: 'value'
32+
});
33+
34+
export default moduleValeComponent;

packages/lib/vue3/vue3-form-naive/src/config/widgets/RadioWidget/index.vue

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
3+
*/
4+
5+
import { h } from 'vue';
6+
import { modelValueComponent, resolveComponent } from '@lljj/vjsf-utils/vue3Utils';
7+
8+
const baseComponent = {
9+
name: 'SelectWidget',
10+
props: {
11+
enumOptions: {
12+
default: () => [],
13+
type: [Array]
14+
}
15+
},
16+
setup(props, { attrs }) {
17+
return () => h(resolveComponent('n-select'), {
18+
options: props.enumOptions,
19+
...attrs,
20+
});
21+
}
22+
};
23+
24+
const moduleValeComponent = modelValueComponent(baseComponent, {
25+
model: 'value'
26+
});
27+
28+
export default moduleValeComponent;

packages/lib/vue3/vue3-form-naive/src/config/widgets/SelectWidget/index.vue

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,27 @@
11
/**
2-
* Created by Liu.Jun on 2020/7/22 13:22.
2+
* Created by Liu.Jun on 2021/2/23 10:21 下午.
33
*/
44

5-
import { h, ref, watch } from 'vue';
5+
import { h } from 'vue';
66
import { resolveComponent } from '@lljj/vjsf-utils/vue3Utils';
7-
import { parseDateString } from '@lljj/vjsf-utils/utils';
87

9-
const formatTimeStr = (dateString) => {
10-
const { hour, minute, second } = parseDateString(dateString, true);
11-
return `${hour}:${minute}:${second}`;
12-
};
13-
14-
const formatTimeObj = (timeStr) => {
15-
if (timeStr instanceof Date) {
16-
return timeStr;
17-
}
18-
19-
// 取当前时间 改时分秒
20-
if (typeof timeStr === 'string') {
21-
const [hours, minutes, seconds] = timeStr.split(':');
22-
const curTime = new Date();
23-
curTime.setHours(+hours);
24-
curTime.setMinutes(+minutes);
25-
curTime.setSeconds(+seconds);
26-
return curTime;
27-
}
28-
29-
// 其它格式清空
30-
return undefined;
31-
};
32-
33-
export default {
8+
const baseComponent = {
349
name: 'TimePickerWidget',
3510
inheritAttrs: false,
36-
props: {
37-
modelValue: {
38-
default: null,
39-
type: null
40-
}
41-
},
42-
setup(props, { attrs, slots }) {
43-
// hack element plus timePicker 变为object类型
44-
const originValue = ref(formatTimeObj(props.modelValue));
45-
46-
// 不需要响应式
47-
let formatValue = props.modelValue;
48-
49-
// 如果外部修改了值
50-
watch(() => props.modelValue, (newVal) => {
51-
if (newVal !== formatValue) {
52-
// 更新内部值
53-
originValue.value = formatTimeObj(newVal);
54-
}
55-
});
56-
57-
return () => h(resolveComponent('el-time-picker'), {
58-
...attrs,
59-
modelValue: originValue.value,
60-
'onUpdate:modelValue': (val) => {
61-
originValue.value = val;
62-
63-
// 更新并缓存内部 timeStr
64-
formatValue = val === null ? undefined : formatTimeStr(val);
65-
66-
// 更新外部的值
67-
attrs['onUpdate:modelValue'].apply(attrs, [formatValue]);
68-
}
69-
}, slots);
11+
setup(props, { attrs }) {
12+
return () => {
13+
const {
14+
modelValue, 'onUpdate:modelValue': onUpdateFormattedValue, ...otherAttrs
15+
} = attrs;
16+
17+
return h(resolveComponent('n-time-picker'), {
18+
...otherAttrs,
19+
valueFormat: 'HH:mm:ss',
20+
formattedValue: modelValue,
21+
onUpdateFormattedValue,
22+
});
23+
};
7024
}
7125
};
26+
27+
export default baseComponent;

0 commit comments

Comments
 (0)