Skip to content

Commit 443ac92

Browse files
fix: 🐛 修复 DateTimePicker 设置为 time 类型时绑定值无法设置为空数组的问题
Closes: #706
1 parent 5aa1c00 commit 443ac92

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

docs/component/datetime-picker-view.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const filter = (type, values) => {
120120

121121
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
122122
|-----|------|-----|-------|-------|---------|
123-
| v-model | 选中项,当 type 为 time 时,类型为字符串,否则为 Date | string / date | - | - |
123+
| v-model | 选中项,当 type 为 time 时,类型为字符串,否则为 `timestamp` | `string` / `timestamp` | - | - |
124124
| type | 选择器类型 | string | date / year-month / time / year | datetime | - |
125125
| loading | 加载中 | boolean | - | false | - |
126126
| loading-color | 加载的颜色,只能使用十六进制的色值写法,且不能使用缩写 | string | - | #4D80F0 | - |

docs/component/datetime-picker.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ const displayFormatTabLabel = (items) => {
259259

260260
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
261261
|-----|------|-----|-------|-------|---------|
262-
| v-model | 选中项,当 type 为 time 时,类型为字符串;当 type 为 Array 时,类型为范围选择;否则为 Date | string / date / array | - | - | - |
263-
| default-value | 默认日期,类型保持与 value 一致,打开面板时面板自动选到默认日期 | string / date / array | - | - | - |
262+
| v-model | 选中项,当 type 为 time 时,类型为字符串;当 type 为 Array 时,类型为范围选择;否则为 `timestamp` | `string` / `timestamp` / `array` | - | - |
263+
| default-value | 默认日期,类型保持与 value 一致,打开面板时面板自动选到默认日期 | `string` / `timestamp` / `array` | - | - | - |
264264
| type | 选择器类型 | string | date / year-month / time / year | datetime | - |
265265
| loading | 加载中 | boolean | - | false | - |
266266
| loading-color | 加载的颜色,只能使用十六进制的色值写法,且不能使用缩写 | string | - | #4D80F0 | - |

src/uni_modules/wot-design-uni/components/wd-datetime-picker-view/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const datetimePickerViewProps = {
3131
/**
3232
* 选中项,当 type 为 time 时,类型为字符串,否则为 时间戳
3333
*/
34-
modelValue: makeRequiredProp([String, Number, Date]),
34+
modelValue: makeRequiredProp([String, Number]),
3535
/**
3636
* 加载中
3737
*/

src/uni_modules/wot-design-uni/components/wd-datetime-picker/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const datetimePickerProps = {
9090
/**
9191
* 选中项,当 type 为 time 时,类型为字符串;当 type 为 Array 时,类型为范围选择;否则为 时间戳
9292
*/
93-
modelValue: makeRequiredProp([String, Number, Date, Array] as PropType<string | number | Date | Array<string | number | Date>>),
93+
modelValue: makeRequiredProp([String, Number, Array] as PropType<string | number | Array<string | number>>),
9494
/**
9595
* 选择器类型,可选值为:date / year-month / time
9696
*/
@@ -102,7 +102,7 @@ export const datetimePickerProps = {
102102
/**
103103
* 最大日期
104104
*/
105-
maxDate: makeNumberProp(new Date(new Date().getFullYear() + 10, 11, 31).getTime()),
105+
maxDate: makeNumberProp(new Date(new Date().getFullYear() + 10, 11, 31, 23, 59, 59).getTime()),
106106
/**
107107
* 最小小时,time类型时生效
108108
*/

src/uni_modules/wot-design-uni/components/wd-datetime-picker/wd-datetime-picker.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ import { FORM_KEY, type FormItemRule } from '../wd-form/types'
167167
import { useParent } from '../composables/useParent'
168168
import { useTranslate } from '../composables/useTranslate'
169169
import { datetimePickerProps, type DatetimePickerExpose } from './types'
170+
import { dayjs } from '../common/dayjs'
170171
171172
const props = defineProps(datetimePickerProps)
172173
const emit = defineEmits(['change', 'open', 'toggle', 'cancel', 'confirm', 'update:modelValue'])
@@ -391,18 +392,14 @@ function getSelects(picker: 'before' | 'after') {
391392
function noop() {}
392393
393394
function getDefaultInnerValue(isRegion?: boolean, isEnd?: boolean): string | number {
394-
const { modelValue: value, defaultValue } = props
395-
395+
const { modelValue: value, defaultValue, maxDate, minDate, type } = props
396396
if (isRegion) {
397-
if (isEnd) {
398-
return (
399-
(isArray(value) ? (value[1] as string) : '') || (defaultValue && isArray(defaultValue) ? (defaultValue[1] as string) : '') || props.maxDate
400-
)
401-
} else {
402-
return (
403-
(isArray(value) ? (value[0] as string) : '') || (defaultValue && isArray(defaultValue) ? (defaultValue[0] as string) : '') || props.minDate
404-
)
405-
}
397+
const index = isEnd ? 1 : 0
398+
const targetValue = isArray(value) ? (value[index] as string) : ''
399+
const targetDefault = isArray(defaultValue) ? (defaultValue[index] as string) : ''
400+
const maxValue = type === 'time' ? dayjs(maxDate).format('HH:mm') : maxDate
401+
const minValue = type === 'time' ? dayjs(minDate).format('HH:mm') : minDate
402+
return targetValue || targetDefault || (isEnd ? maxValue : minValue)
406403
} else {
407404
return isDef(value || defaultValue) ? (value as string) || (defaultValue as string) : ''
408405
}

0 commit comments

Comments
 (0)