mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(TimePicker): support confirm and getSelectedTime methods (#12761)
Co-authored-by: neverland <jait.chen@foxmail.com>
This commit is contained in:
parent
a0e4069969
commit
6b16057d30
@ -283,10 +283,34 @@ export default {
|
|||||||
| columns-top | Custom content above columns | - |
|
| columns-top | Custom content above columns | - |
|
||||||
| columns-bottom | Custom content below columns | - |
|
| columns-bottom | Custom content below columns | - |
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
|
||||||
|
Use [ref](https://vuejs.org/guide/essentials/template-refs.html) to get Picker instance and call instance methods.
|
||||||
|
|
||||||
|
| Name | Description | Attribute | Return value |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| confirm | Stop scrolling and emit confirm event | - | - |
|
||||||
|
| getSelectedTime | Get current selected time | - | _string[] \| undefined_ |
|
||||||
|
|
||||||
### Types
|
### Types
|
||||||
|
|
||||||
The component exports the following type definitions:
|
The component exports the following type definitions:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import type { TimePickerProps, TimePickerColumnType } from 'vant';
|
import type {
|
||||||
|
TimePickerProps,
|
||||||
|
TimePickerColumnType,
|
||||||
|
TimePickerInstance,
|
||||||
|
} from 'vant';
|
||||||
|
```
|
||||||
|
|
||||||
|
`TimePickerInstance` is the type of component instance:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import type { TimePickerInstance } from 'vant';
|
||||||
|
|
||||||
|
const timePickerRef = ref<TimePickerInstance>();
|
||||||
|
|
||||||
|
timePickerRef.value?.confirm();
|
||||||
```
|
```
|
||||||
|
@ -284,6 +284,15 @@ export default {
|
|||||||
| columns-top | 自定义选项上方内容 | - |
|
| columns-top | 自定义选项上方内容 | - |
|
||||||
| columns-bottom | 自定义选项下方内容 | - |
|
| columns-bottom | 自定义选项下方内容 | - |
|
||||||
|
|
||||||
|
### 方法
|
||||||
|
|
||||||
|
通过 ref 可以获取到 Picker 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。
|
||||||
|
|
||||||
|
| 方法名 | 说明 | 参数 | 返回值 |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| confirm | 停止惯性滚动并触发 `confirm` 事件 | - | - |
|
||||||
|
| getSelectedTime | 获取当前选中的时间 | - | _string[] \| undefined_ |
|
||||||
|
|
||||||
### 类型定义
|
### 类型定义
|
||||||
|
|
||||||
组件导出以下类型定义:
|
组件导出以下类型定义:
|
||||||
@ -292,6 +301,17 @@ export default {
|
|||||||
import type { TimePickerProps, TimePickerColumnType } from 'vant';
|
import type { TimePickerProps, TimePickerColumnType } from 'vant';
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`TimePickerInstance` 是组件实例的类型,用法如下:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import type { TimePickerInstance } from 'vant';
|
||||||
|
|
||||||
|
const timePickerRef = ref<TimePickerInstance>();
|
||||||
|
|
||||||
|
timePickerRef.value?.confirm();
|
||||||
|
```
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### 在桌面端无法操作组件?
|
### 在桌面端无法操作组件?
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
watch,
|
watch,
|
||||||
type ExtractPropTypes,
|
type ExtractPropTypes,
|
||||||
type PropType,
|
type PropType,
|
||||||
|
type ComponentPublicInstance,
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
@ -24,7 +25,10 @@ import {
|
|||||||
} from '../utils';
|
} from '../utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Picker } from '../picker';
|
import { Picker, PickerInstance } from '../picker';
|
||||||
|
|
||||||
|
// Composables
|
||||||
|
import { useExpose } from '../composables/use-expose';
|
||||||
|
|
||||||
const [name] = createNamespace('time-picker');
|
const [name] = createNamespace('time-picker');
|
||||||
|
|
||||||
@ -58,6 +62,16 @@ export const timePickerProps = extend({}, sharedProps, {
|
|||||||
|
|
||||||
export type TimePickerProps = ExtractPropTypes<typeof timePickerProps>;
|
export type TimePickerProps = ExtractPropTypes<typeof timePickerProps>;
|
||||||
|
|
||||||
|
export type TimePickerExpose = {
|
||||||
|
confirm: () => void;
|
||||||
|
getSelectedTime: () => string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TimePickerInstance = ComponentPublicInstance<
|
||||||
|
TimePickerProps,
|
||||||
|
TimePickerExpose
|
||||||
|
>;
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name,
|
name,
|
||||||
|
|
||||||
@ -67,6 +81,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const currentValues = ref<string[]>(props.modelValue);
|
const currentValues = ref<string[]>(props.modelValue);
|
||||||
|
const pickerRef = ref<PickerInstance>();
|
||||||
|
|
||||||
const getValidTime = (time: string) => {
|
const getValidTime = (time: string) => {
|
||||||
const timeLimitArr = time.split(':');
|
const timeLimitArr = time.split(':');
|
||||||
@ -75,6 +90,14 @@ export default defineComponent({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const confirm = () => {
|
||||||
|
return pickerRef.value?.confirm();
|
||||||
|
};
|
||||||
|
|
||||||
|
const getSelectedTime = (): string[] => {
|
||||||
|
return currentValues.value;
|
||||||
|
};
|
||||||
|
|
||||||
const columns = computed(() => {
|
const columns = computed(() => {
|
||||||
let { minHour, maxHour, minMinute, maxMinute, minSecond, maxSecond } =
|
let { minHour, maxHour, minMinute, maxMinute, minSecond, maxSecond } =
|
||||||
props;
|
props;
|
||||||
@ -165,8 +188,11 @@ export default defineComponent({
|
|||||||
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
||||||
const onConfirm = (...args: unknown[]) => emit('confirm', ...args);
|
const onConfirm = (...args: unknown[]) => emit('confirm', ...args);
|
||||||
|
|
||||||
|
useExpose<TimePickerExpose>({ confirm, getSelectedTime });
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<Picker
|
<Picker
|
||||||
|
ref={pickerRef}
|
||||||
v-model={currentValues.value}
|
v-model={currentValues.value}
|
||||||
v-slots={slots}
|
v-slots={slots}
|
||||||
columns={columns.value}
|
columns={columns.value}
|
||||||
|
@ -5,7 +5,7 @@ export const TimePicker = withInstall(_TimePicker);
|
|||||||
export default TimePicker;
|
export default TimePicker;
|
||||||
export { timePickerProps } from './TimePicker';
|
export { timePickerProps } from './TimePicker';
|
||||||
export type { TimePickerProps };
|
export type { TimePickerProps };
|
||||||
export type { TimePickerColumnType } from './TimePicker';
|
export type { TimePickerColumnType, TimePickerInstance } from './TimePicker';
|
||||||
|
|
||||||
declare module 'vue' {
|
declare module 'vue' {
|
||||||
export interface GlobalComponents {
|
export interface GlobalComponents {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user