@solar-taro/ui-vant 0.0.0
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/CHANGELOG.md +3 -0
- package/README.md +11 -0
- package/eslint.config.cjs +22 -0
- package/package.json +22 -0
- package/project.json +29 -0
- package/src/cascader-picker/index.ts +1 -0
- package/src/cascader-picker/index.vue +210 -0
- package/src/date-range-picker/index.scss +47 -0
- package/src/date-range-picker/index.ts +1 -0
- package/src/date-range-picker/index.vue +201 -0
- package/src/directives/bind-function.ts +22 -0
- package/src/directives/datetime-picker.ts +33 -0
- package/src/directives/index.ts +3 -0
- package/src/directives/readonly.ts +5 -0
- package/src/index.scss +149 -0
- package/src/index.ts +12 -0
- package/src/item/index.scss +37 -0
- package/src/item/index.ts +1 -0
- package/src/item/index.vue +34 -0
- package/src/list/index.ts +1 -0
- package/src/list/index.vue +86 -0
- package/src/modal/index.ts +1 -0
- package/src/modal/index.vue +55 -0
- package/src/opper-controller/index.scss +37 -0
- package/src/opper-controller/index.ts +1 -0
- package/src/opper-controller/index.vue +365 -0
- package/src/overlay/index.ts +1 -0
- package/src/overlay/overlay.ts +76 -0
- package/src/plugin.ts +14 -0
- package/src/select-picker/index.ts +1 -0
- package/src/select-picker/index.vue +80 -0
- package/src/selector/index.scss +16 -0
- package/src/selector/index.ts +1 -0
- package/src/selector/index.vue +252 -0
- package/src/tabbar/index.scss +78 -0
- package/src/tabbar/index.ts +1 -0
- package/src/tabbar/index.vue +56 -0
- package/src/vant.interface.ts +96 -0
- package/tsconfig.json +21 -0
- package/tsconfig.lib.json +24 -0
- package/tsconfig.spec.json +28 -0
- package/vite.config.ts +130 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const baseConfig = require('../../eslint.config.cjs');
|
|
2
|
+
|
|
3
|
+
module.exports = [
|
|
4
|
+
...baseConfig,
|
|
5
|
+
{
|
|
6
|
+
files: ['**/*.json'],
|
|
7
|
+
rules: {
|
|
8
|
+
'@nx/dependency-checks': [
|
|
9
|
+
'error',
|
|
10
|
+
{
|
|
11
|
+
ignoredFiles: [
|
|
12
|
+
'{projectRoot}/eslint.config.{js,cjs,mjs}',
|
|
13
|
+
'{projectRoot}/vite.config.{js,ts,mjs,mts}',
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
languageOptions: {
|
|
19
|
+
parser: require('jsonc-eslint-parser'),
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@solar-taro/ui-vant",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"typings": "./index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"lodash-es": "^4.17.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/lodash-es": "^4.17.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@opper/core": ">=0.5.0",
|
|
19
|
+
"@opper/wx": ">=0.5.0",
|
|
20
|
+
"vue": "^3.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ui-vant",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/ui-vant/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"// targets": "to see all targets run: nx show project ui-vant --web",
|
|
8
|
+
"targets": {
|
|
9
|
+
"postbuild": {
|
|
10
|
+
"executor": "nx:run-commands",
|
|
11
|
+
"dependsOn": [
|
|
12
|
+
"build"
|
|
13
|
+
],
|
|
14
|
+
"options": {
|
|
15
|
+
"commands": [
|
|
16
|
+
"node scripts/copy-assets.js ui-vant **/*.scss"
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"release:dev": {
|
|
21
|
+
"executor": "nx:run-commands",
|
|
22
|
+
"options": {
|
|
23
|
+
"commands": [
|
|
24
|
+
"yalc publish dist/ui-vant"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as SunCascaderPicker } from './index.vue';
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<van-popup :show="show" position="bottom" :round="true" :close-on-click-overlay="true" @close="close()">
|
|
3
|
+
<van-picker
|
|
4
|
+
v-bind="$attrs"
|
|
5
|
+
:title="title"
|
|
6
|
+
:show-toolbar="true"
|
|
7
|
+
:value-key="labelKey"
|
|
8
|
+
:columns="columns"
|
|
9
|
+
:loading="loading"
|
|
10
|
+
@cancel="cancel()"
|
|
11
|
+
@confirm="onConfirm($event)"
|
|
12
|
+
@change="onChange($event)" />
|
|
13
|
+
<van-search
|
|
14
|
+
v-if="searchable && options.length"
|
|
15
|
+
shape="round"
|
|
16
|
+
input-align="center"
|
|
17
|
+
placeholder="请输入搜索关键词"
|
|
18
|
+
@change="onKeywordChange($event)" />
|
|
19
|
+
</van-popup>
|
|
20
|
+
</template>
|
|
21
|
+
|
|
22
|
+
<script lang="ts" setup>
|
|
23
|
+
import { cloneDeep } from 'lodash-es';
|
|
24
|
+
import { computed, ref } from 'vue';
|
|
25
|
+
import { VanPickerColumn } from '../vant.interface';
|
|
26
|
+
|
|
27
|
+
interface Node {
|
|
28
|
+
children?: Node[];
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
defineOptions({
|
|
33
|
+
inheritAttrs: false
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const emit = defineEmits<{
|
|
37
|
+
close: [],
|
|
38
|
+
cancel: [any],
|
|
39
|
+
confirm: [any],
|
|
40
|
+
'update:show': [boolean],
|
|
41
|
+
'update:modelValue': [any]
|
|
42
|
+
}>()
|
|
43
|
+
|
|
44
|
+
const props = withDefaults(
|
|
45
|
+
defineProps<{
|
|
46
|
+
show: boolean,
|
|
47
|
+
labelKey?: string,
|
|
48
|
+
valueKey?: string,
|
|
49
|
+
pidKey?: string,
|
|
50
|
+
searchable?: boolean,
|
|
51
|
+
title: string,
|
|
52
|
+
loading?: boolean
|
|
53
|
+
/** [[level1 list], [level2 list], [level3 list]] */
|
|
54
|
+
options: any[][],
|
|
55
|
+
modelValue: string | number | undefined,
|
|
56
|
+
required?: boolean
|
|
57
|
+
}>(),
|
|
58
|
+
{
|
|
59
|
+
labelKey: 'fullName',
|
|
60
|
+
valueKey: 'id',
|
|
61
|
+
pidKey: 'pid',
|
|
62
|
+
searchable: true,
|
|
63
|
+
options: () => []
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const keyword = ref('');
|
|
68
|
+
|
|
69
|
+
const trees = computed(() => {
|
|
70
|
+
const optionsList: Node[][] = cloneDeep(props.options);
|
|
71
|
+
|
|
72
|
+
// 给每个 node 加上 children
|
|
73
|
+
// 注意,这里跳过最后一个,因为最后一个肯定都是 child,不需要添加 children
|
|
74
|
+
for (let index = 0; index < optionsList.length - 1; index++) {
|
|
75
|
+
const options = optionsList[index];
|
|
76
|
+
|
|
77
|
+
for (let idx = 0; idx < options.length; idx++) {
|
|
78
|
+
const option = options[idx];
|
|
79
|
+
option.children = optionsList[index + 1].filter(child => child[props.pidKey] === option[props.valueKey]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return optionsList[0];
|
|
84
|
+
});
|
|
85
|
+
/** 节点打平的列表 */
|
|
86
|
+
const list = computed(() => cloneDeep(trees.value).map(option => flatTree(option)).flat())
|
|
87
|
+
const columns = computed(() => {
|
|
88
|
+
// 找到选中的值
|
|
89
|
+
const model = props.options[props.options.length - 1].find(o => o[props.valueKey] === props.modelValue);
|
|
90
|
+
return model ? initColumnsByDefaultValue(model) : initColumns();
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
function cancel() {
|
|
94
|
+
props.required || emit('update:modelValue', null);
|
|
95
|
+
emit('cancel', props.modelValue);
|
|
96
|
+
close();
|
|
97
|
+
}
|
|
98
|
+
function close() {
|
|
99
|
+
emit('update:show', false);
|
|
100
|
+
emit('close');
|
|
101
|
+
}
|
|
102
|
+
function onConfirm({ detail }: any) {
|
|
103
|
+
// 拿到最后一个
|
|
104
|
+
const value = detail.value[detail.value.length - 1];
|
|
105
|
+
|
|
106
|
+
emit('update:modelValue', value[props.valueKey]);
|
|
107
|
+
emit('confirm', findParents(value));
|
|
108
|
+
emit('update:show', false);
|
|
109
|
+
}
|
|
110
|
+
function onChange({ detail }: any) {
|
|
111
|
+
const { picker, value, index } = detail;
|
|
112
|
+
|
|
113
|
+
// 没有关键字且不是在滚动最后一行
|
|
114
|
+
if (!keyword.value && index < props.options.length - 1) {
|
|
115
|
+
picker.setColumnValues(
|
|
116
|
+
index + 1,
|
|
117
|
+
props.options[index + 1].filter(o => o[props.pidKey] === value[index][props.valueKey])
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function onKeywordChange({ detail }: any) {
|
|
122
|
+
keyword.value = detail.trim().toLowerCase();
|
|
123
|
+
initColumns();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 找到当前值的所有 parent
|
|
127
|
+
* @param child
|
|
128
|
+
* @returns [parent1, parent2, ..., child]
|
|
129
|
+
*/
|
|
130
|
+
function findParents(child: Record<string, unknown>) {
|
|
131
|
+
const result = [props.options[props.options.length - 1].find(o => o[props.valueKey] === child[props.valueKey] && o[props.pidKey] === child[props.pidKey])];
|
|
132
|
+
for (let index = props.options.length - 2; index >= 0; index--) {
|
|
133
|
+
result.unshift(
|
|
134
|
+
props.options[index].find(o => o[props.valueKey] === result[0]?.[props.pidKey])
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 把 tree 打平成 list
|
|
142
|
+
* ```
|
|
143
|
+
* ↗ b
|
|
144
|
+
* a -> [a/b, a/c]
|
|
145
|
+
* ↘ c
|
|
146
|
+
* ```
|
|
147
|
+
* @param node
|
|
148
|
+
* @param perfix
|
|
149
|
+
*/
|
|
150
|
+
function flatTree(node: Node, perfix: string = '') {
|
|
151
|
+
let result = [] as Node[];
|
|
152
|
+
let name = perfix + node[props.labelKey];
|
|
153
|
+
|
|
154
|
+
if (node.children?.length) {
|
|
155
|
+
node.children.forEach(child => {
|
|
156
|
+
result = result.concat(flatTree(child, name + ' / '))
|
|
157
|
+
})
|
|
158
|
+
} else {
|
|
159
|
+
node[props.labelKey] = name;
|
|
160
|
+
result.push(node);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
function initColumns(): VanPickerColumn<Node>[] {
|
|
166
|
+
if (keyword.value) {
|
|
167
|
+
return [{
|
|
168
|
+
values: list.value.filter(o => o[props.labelKey].toLowerCase().includes(keyword.value))
|
|
169
|
+
}];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const columns = [];
|
|
173
|
+
|
|
174
|
+
let nodes: Node[] | undefined = trees.value;
|
|
175
|
+
|
|
176
|
+
while (nodes) {
|
|
177
|
+
columns.push({ values: nodes });
|
|
178
|
+
// 替换成当前值的第一个的元素的 children
|
|
179
|
+
nodes = nodes[0]?.children;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return columns;
|
|
183
|
+
}
|
|
184
|
+
function initColumnsByDefaultValue(value: Record<string, unknown>): VanPickerColumn<Node>[] {
|
|
185
|
+
if (keyword.value) {
|
|
186
|
+
const values = list.value.filter(o => o[props.labelKey].toLowerCase().includes(keyword.value));
|
|
187
|
+
return [{
|
|
188
|
+
values,
|
|
189
|
+
defaultIndex: values.findIndex(o => o[props.valueKey] === value[props.valueKey])
|
|
190
|
+
}];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const columns = [];
|
|
194
|
+
|
|
195
|
+
// 倒序去找 parent
|
|
196
|
+
for (let index = props.options.length - 1; index >= 0; index--) {
|
|
197
|
+
const values = props.options[index].filter(o => o[props.pidKey] === value[props.pidKey]);
|
|
198
|
+
columns[index] = {
|
|
199
|
+
values,
|
|
200
|
+
defaultIndex: values.findIndex(o => o[props.valueKey] === value[props.valueKey])
|
|
201
|
+
}
|
|
202
|
+
// 找当前值的上一级
|
|
203
|
+
if (index > 0) {
|
|
204
|
+
value = props.options[index - 1].find(o => o[props.valueKey] === value[props.pidKey])!;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return columns
|
|
209
|
+
}
|
|
210
|
+
</script>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
.sun-date-range-picker {
|
|
2
|
+
&-input-group {
|
|
3
|
+
display: flex;
|
|
4
|
+
justify-content: center;
|
|
5
|
+
align-items: center;
|
|
6
|
+
padding: 20rpx calc(24rpx + var(--safe-left)) 40rpx;
|
|
7
|
+
|
|
8
|
+
> van-icon {
|
|
9
|
+
font-size: 36rpx;
|
|
10
|
+
margin: 0 16rpx;
|
|
11
|
+
transform: rotate(-90deg);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
&-input {
|
|
16
|
+
position: relative;
|
|
17
|
+
flex: 1;
|
|
18
|
+
text-align: center;
|
|
19
|
+
padding: 24rpx 0;
|
|
20
|
+
color: var(--color-gray);
|
|
21
|
+
border-radius: 99px;
|
|
22
|
+
border: 1px solid currentColor;
|
|
23
|
+
transition: all ease-out 100ms;
|
|
24
|
+
|
|
25
|
+
&-active {
|
|
26
|
+
color: var(--color-primary);
|
|
27
|
+
font-weight: bold;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
&-label {
|
|
31
|
+
position: absolute;
|
|
32
|
+
top: -16rpx;
|
|
33
|
+
left: 30rpx;
|
|
34
|
+
padding: 0 8rpx;
|
|
35
|
+
font-size: 24rpx;
|
|
36
|
+
background: white;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
&-placeholder {
|
|
40
|
+
font-weight: normal;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
&-btn-wrapper {
|
|
45
|
+
padding: 40rpx calc(24rpx + var(--safe-left)) 24rpx;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as SunDateRangePicker } from './index.vue';
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<slot :present="present">
|
|
3
|
+
<van-field
|
|
4
|
+
v-readonly
|
|
5
|
+
v-bind="$attrs"
|
|
6
|
+
:label="label"
|
|
7
|
+
:value="inputValue"
|
|
8
|
+
:placeholder="internalPlaceholder.join(separator)"
|
|
9
|
+
@click-input="present()">
|
|
10
|
+
<slot-view name="right-icon">
|
|
11
|
+
<van-icon v-if="canClear" name="clear" @tap="clear()" />
|
|
12
|
+
</slot-view>
|
|
13
|
+
</van-field>
|
|
14
|
+
</slot>
|
|
15
|
+
|
|
16
|
+
<van-action-sheet
|
|
17
|
+
:show="show"
|
|
18
|
+
:title="internalTitle"
|
|
19
|
+
:close-on-click-overlay="true"
|
|
20
|
+
@close="close()"
|
|
21
|
+
@cancel="close()">
|
|
22
|
+
<view class="sun-date-range-picker-input-group">
|
|
23
|
+
<view
|
|
24
|
+
class="sun-date-range-picker-input"
|
|
25
|
+
:class="{ 'sun-date-range-picker-input-active': activeIndex === 0 }"
|
|
26
|
+
@tap="activate(0)">
|
|
27
|
+
<view v-if="internalModelValue[0]" class="sun-date-range-picker-input-value">
|
|
28
|
+
{{ date(internalModelValue[0], internalFormat) }}
|
|
29
|
+
</view>
|
|
30
|
+
<view v-else class="sun-date-range-picker-input-placeholder">请选择</view>
|
|
31
|
+
<view class="sun-date-range-picker-input-label">{{ internalPlaceholder[0] }}</view>
|
|
32
|
+
</view>
|
|
33
|
+
<van-icon v-if="true" name="down" color="var(--color-gray)" />
|
|
34
|
+
<view
|
|
35
|
+
class="sun-date-range-picker-input"
|
|
36
|
+
:class="{ 'sun-date-range-picker-input-active': activeIndex === 1 }"
|
|
37
|
+
@tap="activate(1)">
|
|
38
|
+
<view v-if="internalModelValue[1]" class="date-range-picker-input-value">
|
|
39
|
+
{{ date(internalModelValue[1], internalFormat) }}
|
|
40
|
+
</view>
|
|
41
|
+
<view v-else class="sun-date-range-picker-input-placeholder">请选择</view>
|
|
42
|
+
<view class="sun-date-range-picker-input-label">{{ internalPlaceholder[1] }}</view>
|
|
43
|
+
</view>
|
|
44
|
+
</view>
|
|
45
|
+
<van-datetime-picker
|
|
46
|
+
v-datetime-picker
|
|
47
|
+
:id="id"
|
|
48
|
+
:type="type"
|
|
49
|
+
:show-toolbar="false"
|
|
50
|
+
:value="currentDate"
|
|
51
|
+
:min-date="minDate"
|
|
52
|
+
:max-date="maxDate"
|
|
53
|
+
@modify="onValueChange($event.detail)" />
|
|
54
|
+
|
|
55
|
+
<view class="sun-date-range-picker-btn-wrapper">
|
|
56
|
+
<van-button type="primary" round="true" block="true" @tap="submit()">
|
|
57
|
+
{{ internalModelValue[0] && internalModelValue[1] ? '确定' : '继续' }}
|
|
58
|
+
</van-button>
|
|
59
|
+
</view>
|
|
60
|
+
</van-action-sheet>
|
|
61
|
+
</template>
|
|
62
|
+
|
|
63
|
+
<script lang="ts">
|
|
64
|
+
let nextId = 1;
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<script lang="ts" setup>
|
|
68
|
+
import { date } from '@solar-taro/core';
|
|
69
|
+
import { computed, ref } from 'vue';
|
|
70
|
+
import { vDatetimePicker } from '../directives';
|
|
71
|
+
|
|
72
|
+
const props = withDefaults(
|
|
73
|
+
defineProps<{
|
|
74
|
+
id?: string;
|
|
75
|
+
type?: 'date' | 'datetime',
|
|
76
|
+
label?: string;
|
|
77
|
+
title?: string;
|
|
78
|
+
placeholder?: [string, string];
|
|
79
|
+
modelValue: [number?, number?];
|
|
80
|
+
required?: boolean;
|
|
81
|
+
format?: string;
|
|
82
|
+
minDate?: number;
|
|
83
|
+
maxDate?: number;
|
|
84
|
+
}>(),
|
|
85
|
+
{
|
|
86
|
+
id: () => `van-datetime-picker-${nextId++}`,
|
|
87
|
+
type: 'date',
|
|
88
|
+
minDate: () => new Date(new Date().getFullYear() - 5, 0, 0, 0, 0, 0).getTime(),
|
|
89
|
+
maxDate: () => new Date(new Date().getFullYear() + 5, 0, 0, 0, 0, 0).getTime(),
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const emit = defineEmits<{
|
|
94
|
+
(e: 'update:modelValue', v: [number?, number?]): void;
|
|
95
|
+
(e: 'confirm', v: [number, number]): void;
|
|
96
|
+
(e: 'clear', v: [number?, number?]): void;
|
|
97
|
+
(e: 'close'): void;
|
|
98
|
+
}>();
|
|
99
|
+
|
|
100
|
+
const separator = ' ~ ';
|
|
101
|
+
|
|
102
|
+
// 内部的模型值,用户操作确定后才同步出去
|
|
103
|
+
const internalModelValue = ref<[number?, number?]>([...props.modelValue]);
|
|
104
|
+
const currentDate = ref(props.modelValue[0] ?? defaultDate());
|
|
105
|
+
const show = ref(true);
|
|
106
|
+
const activeIndex = ref<0 | 1>(0);
|
|
107
|
+
|
|
108
|
+
const inputValue = computed(() => {
|
|
109
|
+
const [start, end] = props.modelValue;
|
|
110
|
+
|
|
111
|
+
if (start && end) {
|
|
112
|
+
return date(start, internalFormat.value) + separator + date(end, internalFormat.value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return '';
|
|
116
|
+
});
|
|
117
|
+
const canClear = computed(() => props.modelValue.length && !props.required);
|
|
118
|
+
// eslint-disable-next-line vue/return-in-computed-property
|
|
119
|
+
const internalFormat = computed(() => {
|
|
120
|
+
if (props.format) {
|
|
121
|
+
return props.format;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
switch (props.type) {
|
|
125
|
+
case 'date': return 'yyyy-MM-dd';
|
|
126
|
+
case 'datetime': return 'yyyy-MM-dd HH:mm';
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
// eslint-disable-next-line vue/return-in-computed-property
|
|
130
|
+
const internalTitle = computed(() => {
|
|
131
|
+
if (props.title) {
|
|
132
|
+
return props.title;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
switch (props.type) {
|
|
136
|
+
case 'date': return '请选择日期';
|
|
137
|
+
case 'datetime': return '请选择时间';
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
// eslint-disable-next-line vue/return-in-computed-property
|
|
141
|
+
const internalPlaceholder = computed(() => {
|
|
142
|
+
if (props.placeholder) {
|
|
143
|
+
return props.placeholder;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
switch (props.type) {
|
|
147
|
+
case 'date': return ['开始日期', '结束日期'];
|
|
148
|
+
case 'datetime': return ['开始时间', '结束时间'];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
function defaultDate() {
|
|
153
|
+
const today = new Date();
|
|
154
|
+
const currentMin = today.getMinutes();
|
|
155
|
+
|
|
156
|
+
switch (props.type) {
|
|
157
|
+
case 'date':
|
|
158
|
+
return today.setHours(0, 0, 0, 0);
|
|
159
|
+
|
|
160
|
+
case 'datetime':
|
|
161
|
+
return today.setMinutes(currentMin - currentMin % 5); // 处理不能被5整除的数
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function present() {
|
|
166
|
+
show.value = true;
|
|
167
|
+
activate(0);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function close() {
|
|
171
|
+
show.value = false;
|
|
172
|
+
emit('close');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function clear() {
|
|
176
|
+
internalModelValue.value = [];
|
|
177
|
+
emit('clear', props.modelValue);
|
|
178
|
+
emit('update:modelValue', []);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function activate(index: 0 | 1) {
|
|
182
|
+
activeIndex.value = index;
|
|
183
|
+
currentDate.value = internalModelValue.value[index] ||= defaultDate();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function onValueChange(value: number) {
|
|
187
|
+
internalModelValue.value[activeIndex.value] = value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function submit() {
|
|
191
|
+
const [start, end] = internalModelValue.value;
|
|
192
|
+
// 检查有没有值还没填
|
|
193
|
+
if (!start || !end) {
|
|
194
|
+
return activate(start ? 1 : 0);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
emit('update:modelValue', [...internalModelValue.value]);
|
|
198
|
+
emit('confirm', internalModelValue.value as [number, number]);
|
|
199
|
+
close();
|
|
200
|
+
}
|
|
201
|
+
</script>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getCurrentInstance, nextTick, PageInstance } from '@tarojs/taro';
|
|
2
|
+
import { Directive } from 'vue';
|
|
3
|
+
|
|
4
|
+
function setComponentData(page: PageInstance, id: string, prop: string, value: unknown) {
|
|
5
|
+
const cmp = page.selectComponent!('#' + id);
|
|
6
|
+
cmp.setData!({ [prop]: value });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const vBindFn: Directive<{ id: string }, (...args: any[]) => any> = {
|
|
10
|
+
mounted(element, binding) {
|
|
11
|
+
const { page } = getCurrentInstance();
|
|
12
|
+
nextTick(() => {
|
|
13
|
+
nextTick(() => {
|
|
14
|
+
setComponentData(page!, element.id, binding.arg!, binding.value);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
updated(element, binding) {
|
|
19
|
+
const { page } = getCurrentInstance();
|
|
20
|
+
setComponentData(page!, element.id, binding.arg!, binding.value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getCurrentInstance, nextTick } from '@tarojs/taro';
|
|
2
|
+
import { Directive } from 'vue';
|
|
3
|
+
|
|
4
|
+
export const vDatetimePicker: Directive<{ id: string }> = {
|
|
5
|
+
mounted(element) {
|
|
6
|
+
const { page } = getCurrentInstance();
|
|
7
|
+
nextTick(() => {
|
|
8
|
+
nextTick(() => {
|
|
9
|
+
const cmp = page!.selectComponent!(`#${element.id}`);
|
|
10
|
+
|
|
11
|
+
cmp.setData!({
|
|
12
|
+
formatter: (type: 'year' | 'month' | 'day' | 'hour' | 'minute', value: string) => {
|
|
13
|
+
switch (type) {
|
|
14
|
+
case 'year': return `${value}年`;
|
|
15
|
+
case 'month': return `${value}月`;
|
|
16
|
+
case 'day': return `${value}日`;
|
|
17
|
+
case 'hour': return `${value}时`;
|
|
18
|
+
case 'minute': return `${value}分`;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
filter: (type: 'year' | 'month' | 'day' | 'hour' | 'minute', values: string[]) => {
|
|
22
|
+
if (type === 'minute') {
|
|
23
|
+
return values.filter(option => +option % 5 === 0);
|
|
24
|
+
}
|
|
25
|
+
return values;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
// @ts-expect-error van-datetime-picker
|
|
29
|
+
cmp.updateValue();
|
|
30
|
+
});
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
}
|