Legacy Migration Guide v2 to v3
Historical guide for upgrading from v2.x to v3.x
Legacy Documentation
This guide is for legacy users migrating from version 2.x to 3.x.
If you're using v3.x and want to upgrade to v4.x, please see the Migration Guide v3 to v4.
Breaking Changes
1. CSS Must Be Imported Manually
In v3, CSS is no longer automatically included. You must import it manually.
v2
1// CSS was automatically included2import { TimepickerUI } from 'timepicker-ui';
v3
1// You must import CSS manually2import { TimepickerUI } from 'timepicker-ui';3import 'timepicker-ui/main.css';
2. Event Names Changed
All event names have been prefixed with timepicker: for better namespacing and to avoid conflicts.
v2
1input.addEventListener('show', (e) => {2 console.log('Opened');3});45input.addEventListener('accept', (e) => {6 console.log('Confirmed:', e.detail);7});89input.addEventListener('cancel', (e) => {10 console.log('Cancelled');11});
v3
1input.addEventListener('timepicker:open', (e) => {2 console.log('Opened');3});45input.addEventListener('timepicker:confirm', (e) => {6 console.log('Confirmed:', e.detail);7});89input.addEventListener('timepicker:cancel', (e) => {10 console.log('Cancelled');11});
3. Recommended: Use Callbacks Instead of Events
v3 introduces a new callback-based API which is cleaner and more modern than DOM events.
v2 (DOM Events)
1input.addEventListener('accept', (e) => {2 console.log('Time:', e.detail);3});45input.addEventListener('cancel', () => {6 console.log('Cancelled');7});
v3 (Callbacks - Recommended)
1const picker = new TimepickerUI(input, {2 onConfirm: (data) => {3 console.log('Time:', data);4 },5 onCancel: (data) => {6 console.log('Cancelled');7 },8 onOpen: () => {9 console.log('Opened');10 },11});
4. destroy() Behavior Changed
In v2, destroy() removed the input element from the DOM. In v3, it only destroys the timepicker instance and keeps the input element intact.
v2
1// Removed input from DOM2picker.destroy(); // Input is gone!
v3
1// Only destroys picker, keeps input2picker.destroy(); // Input remains in DOM
Event Name Mapping
| v2 Event | v3 Event |
|---|---|
| show | timepicker:open |
| accept | timepicker:confirm |
| cancel | timepicker:cancel |
| update | timepicker:update |
New in v3
Inline Mode
Display timepicker directly in a container without modal
Instance Management
getById() and destroyAll() methods
Direct Callbacks
Pass callbacks directly in options
5 New Themes
Extended theme options for better customization
getValue() / setValue()
Programmatically get and set time values
Better TypeScript
Improved type definitions and IntelliSense
EventEmitter API
Modern event handling with on(), off(), once()
EventEmitter API (v3.x)
Starting in v3.x, we recommend using the new EventEmitter API instead of DOM events. This will become the standard in future versions.
Old Way (Deprecated)
1// DOM events - will be removed in v42input.addEventListener('timepicker:confirm', (e) => {3 console.log(e.detail);4});56input.addEventListener('timepicker:cancel', (e) => {7 console.log('Cancelled');8});
New Way (Recommended)
1// EventEmitter API - recommended2picker.on('confirm', (data) => {3 console.log(data);4});56picker.on('cancel', (data) => {7 console.log('Cancelled');8});910// One-time listener11picker.once('open', () => {12 console.log('First open only');13});1415// Remove listener16const handler = (data) => console.log(data);17picker.on('confirm', handler);18picker.off('confirm', handler);
Benefits
- Cleaner API without prefixes
- Better TypeScript support
- No DOM pollution
- Memory-efficient (automatic cleanup on destroy)
- Supports
once()for one-time listeners
Instance Management
v3 introduces new static methods for managing multiple timepicker instances.
1// Create instances with IDs2const picker1 = new TimepickerUI('#input1', { id: 'picker1' });3const picker2 = new TimepickerUI('#input2', { id: 'picker2' });45// Get instance by ID6const instance = TimepickerUI.getById('picker1');78// Destroy all instances at once9TimepickerUI.destroyAll();
getValue() and setValue()
New methods for programmatically getting and setting time values.
1const picker = new TimepickerUI('#input');23// Get current value4const currentTime = picker.getValue();5console.log(currentTime); // "14:30"67// Set new value8picker.setValue('09:15');910// Set value with custom format11picker.setValue('3:45 PM');
Inline Mode
Display the timepicker directly in a container without using a modal.
1const picker = new TimepickerUI('#input', {2 inline: {3 enabled: true,4 containerId: 'timepicker-container',5 showButtons: false,6 autoUpdate: true,7 },8});
1<input type="text" id="input" />2<div id="timepicker-container"></div>
Complete Migration Example
v2 Code
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#myInput');45// v2 initialization6const picker = new TimepickerUI(input, {7 clockType: '12h',8 theme: 'basic',9});1011// v2 events12input.addEventListener('show', () => {13 console.log('Opened');14});1516input.addEventListener('accept', (e) => {17 console.log('Time selected:', e.detail);18});1920input.addEventListener('cancel', () => {21 console.log('Cancelled');22});
v3 Code (Migrated)
1import { TimepickerUI } from 'timepicker-ui';2import 'timepicker-ui/main.css'; // Required in v334const input = document.querySelector('#myInput');56// v3 initialization with callbacks7const picker = new TimepickerUI(input, {8 clockType: '12h',9 theme: 'basic',10 // Modern callback-based API11 onOpen: () => {12 console.log('Opened');13 },14 onConfirm: (data) => {15 console.log('Time selected:', data);16 },17 onCancel: () => {18 console.log('Cancelled');19 },20});2122// Or use EventEmitter API (recommended)23picker.on('confirm', (data) => {24 console.log('Confirmed:', data);25});2627picker.once('open', () => {28 console.log('First open only');29});
Migration Checklist
timepicker-ui/main.css in your codetimepicker: prefix to all event namesonConfirm, onCancel, etc.Next Steps
After migrating to v3, consider upgrading to v4 for the latest features and improvements.
View Migration Guide v3 to v4