UITimePicker
UITimePicker
UITimePicker is a component that allows users to input or select a time value. It provides a dropdown panel with selectable hour, minute, and optionally second inputs, as well as AM/PM selection when in 12-hour format.
<template> <UITimePicker v-model="selectedTime" format="HH:mm" placeholder="Select time" /></template>
<script setup>import { ref } from 'vue';
const selectedTime = ref(null);</script>Props
modelValue(string | Date | null): The selected time value, to be used with v-modelformat(string, default: ‘HH:mm:ss’): Time format stringplaceholder(string, default: ‘Select time’): Input placeholder textdisabled(boolean, default: false): Whether the component is disabledclearable(boolean, default: true): Whether to show a clear buttonsize(‘small’ | ‘medium’ | ‘large’, default: ‘medium’): Size of the componentinputReadOnly(boolean, default: false): Whether the input is read-onlyuse12Hours(boolean, default: false): Whether to use 12-hour format with AM/PMhourStep(number, default: 1): Interval between hours in the pickerminuteStep(number, default: 1): Interval between minutes in the pickersecondStep(number, default: 1): Interval between seconds in the pickerdisabledHours(Function): Function that returns disabled hour optionsdisabledMinutes(Function): Function that returns disabled minute optionsdisabledSeconds(Function): Function that returns disabled second optionshideDisabledOptions(boolean, default: false): Whether to hide disabled optionsallowClear(boolean, default: true): Whether to allow clearing the selected timeopen(boolean): Whether the dropdown panel is visibleplacement(‘bottomLeft’ | ‘bottomRight’ | ‘topLeft’ | ‘topRight’, default: ‘bottomLeft’): Placement of the dropdown panel
Events
update:modelValue(value: string | Date | null): Emitted when the selected time changesfocus(event: FocusEvent): Emitted when the component gains focusblur(event: FocusEvent): Emitted when the component loses focuschange(value: string | Date | null): Emitted when the user confirms a time selectionopen-change(open: boolean): Emitted when the dropdown panel is opened or closedclear(): Emitted when the clear button is clicked
Slots
prefix: Custom prefix content in the inputsuffix: Custom suffix content in the inputrenderExtraFooter: Additional footer content for the dropdown panel
Usage Examples
- Basic Usage:
<template> <UITimePicker v-model="time" /></template>
<script setup>import { ref } from 'vue';
const time = ref('13:30:00');</script>- Custom Format:
<template> <UITimePicker v-model="time" format="h:mm a" :use12Hours="true" /></template>
<script setup>import { ref } from 'vue';
const time = ref('1:30 pm');</script>- Time Steps:
<template> <UITimePicker v-model="time" format="HH:mm" :minuteStep="15" :hourStep="2" /></template>
<script setup>import { ref } from 'vue';
const time = ref('14:00');</script>- Disabled Options:
<template> <UITimePicker v-model="time" format="HH:mm" :disabledHours="disabledHours" :disabledMinutes="disabledMinutes" /></template>
<script setup>import { ref } from 'vue';
const time = ref('14:30');
const disabledHours = () => { return [0, 1, 2, 3, 4, 5, 6, 7, 20, 21, 22, 23];};
const disabledMinutes = (selectedHour) => { if (selectedHour === 8) { return Array.from({ length: 30 }, (_, i) => i); // Disable first 30 minutes for 8 AM } return [];};</script>- With Date Picker:
<template> <div> <UIDatePicker v-model:value="date" valueType="date" /> <UITimePicker v-model="time" style="margin-left: 8px;" /> <div class="selected-datetime"> Selected: {{ formatDateTime(date, time) }} </div> </div></template>
<script setup>import { ref, computed } from 'vue';import dayjs from 'dayjs';
const date = ref(new Date());const time = ref('12:00:00');
const formatDateTime = (date, time) => { if (!date || !time) return '';
const [hours, minutes, seconds] = time.split(':'); const datetime = new Date(date); datetime.setHours(parseInt(hours)); datetime.setMinutes(parseInt(minutes)); datetime.setSeconds(parseInt(seconds || 0));
return dayjs(datetime).format('YYYY-MM-DD HH:mm:ss');};</script>Best Practices
-
Time Format:
- Use appropriate time format for your locale
- Consider 12-hour vs 24-hour format based on user preference
- Clearly indicate which format is being used
- Use consistent time format across your application
-
User Experience:
- Use clear input placeholders
- Consider step intervals for specific use cases
- Use input read-only mode to prevent keyboard errors
- Provide clear validation messages for invalid times
-
Validation:
- Validate time inputs appropriately
- Disable hours/minutes that are not valid selections
- Consider time range validations when needed
- Use the combined value appropriately in forms
-
Accessibility:
- Ensure keyboard navigation works properly
- Include appropriate ARIA attributes
- Provide sufficient color contrast
- Ensure focus states are clearly visible