Example · Advanced
Dynamic Updates
Update timepicker options dynamically without destroying the instance
Update Method
Use the
update() method to change options on an existing timepicker instance without recreating it.Interactive Demo
Change options dynamically and see the timepicker update:
index.tsxtsx
1import { useState, useEffect, useRef, useMemo } from 'react';2import { TimepickerUI } from 'timepicker-ui';34function DynamicTimepicker() {5 const inputRef = useRef<HTMLInputElement>(null);6 const pickerRef = useRef<TimepickerUI | null>(null);7 const [clockType, setClockType] = useState<'12h' | '24h'>('12h');8 const [theme, setTheme] = useState<'basic' | 'dark'>('basic');910 const options = useMemo(() => ({11 clock: { type: clockType },12 ui: { theme },13 }), [clockType, theme]);1415 useEffect(() => {16 if (!inputRef.current) return;1718 if (!pickerRef.current) {19 // Create picker only once20 pickerRef.current = new TimepickerUI(inputRef.current, options);21 pickerRef.current.create();22 }2324 return () => {25 // Cleanup on unmount26 pickerRef.current?.destroy();27 pickerRef.current = null;28 };29 }, []); // Empty deps - run only once3031 // Update picker when options change32 useEffect(() => {33 if (pickerRef.current) {34 pickerRef.current.update({ options });35 }36 }, [options]);3738 return (39 <>40 <button onClick={() => setClockType(41 clockType === '12h' ? '24h' : '12h'42 )}>43 Toggle Format44 </button>45 <input ref={inputRef} type="text" />46 </>47 );48}
Update Method
The update method allows you to change options without destroying the picker:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#timepicker');4const picker = new TimepickerUI(input, {5 clock: { type: '12h' },6 ui: { theme: 'basic' }7});89picker.create();1011// Later, update options12picker.update({13 options: {14 clock: { type: '24h' },15 ui: { theme: 'dark' }16 }17});1819// Or with create option to reinitialize20picker.update({21 options: {22 clock: { type: '12h' }23 },24 create: true25});
Update with Callbacks
Update options including event callbacks:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#timepicker');4const picker = new TimepickerUI(input);5picker.create();67// Update with new callbacks8picker.update({9 options: {10 callbacks: {11 onConfirm: (data) => {12 console.log('New time selected:', data);13 },14 onCancel: () => {15 console.log('Selection cancelled');16 }17 }18 }19});
Conditional Updates
Update picker based on user preferences or app state:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#timepicker');4const picker = new TimepickerUI(input);5picker.create();67// Update based on user preference8function updatePickerTheme(isDarkMode: boolean) {9 picker.update({10 options: {11 ui: { theme: isDarkMode ? 'dark' : 'basic' }12 }13 });14}1516// Update based on device17function updateForMobile(isMobile: boolean) {18 picker.update({19 options: {20 ui: {21 mobile: isMobile,22 animation: !isMobile // Disable animation on mobile23 }24 }25 });26}2728// Listen to system theme changes29window.matchMedia('(prefers-color-scheme: dark)')30 .addEventListener('change', (e) => {31 updatePickerTheme(e.matches);32 });
Update DisabledTime Dynamically
Change disabled times based on business rules or user context:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#timepicker');4const picker = new TimepickerUI(input, {5 clock: {6 type: '24h',7 disabledTime: {8 hours: [9, 10, 11, 12] // Morning hours disabled9 }10 }11});12picker.create();1314// Update to disable evening hours instead15function switchToEveningRestriction() {16 picker.update({17 options: {18 clock: {19 disabledTime: {20 hours: [0, 1, 2, 3, 4, 5, 6, 7, 8]21 }22 }23 },24 create: true // Reinitialize to apply changes25 });26}2728// Clear all restrictions29function clearRestrictions() {30 picker.update({31 options: {32 clock: {33 disabledTime: {}34 }35 },36 create: true37 });38}3940// Update based on day of week41function updateForDayOfWeek() {42 const day = new Date().getDay();43 const isWeekend = day === 0 || day === 6;4445 picker.update({46 options: {47 clock: {48 disabledTime: isWeekend49 ? {} // No restrictions on weekends50 : { hours: [0, 1, 2, 3, 4, 5, 6, 22, 23] } // Business hours only51 }52 },53 create: true54 });55}
Update Disabled Intervals Dynamically
Change disabled time intervals for business hours, shifts, or scheduling:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23const input = document.querySelector('#timepicker');4const picker = new TimepickerUI(input, {5 clock: {6 type: '24h',7 disabledTime: {8 interval: '09:00 - 17:00' // Block business hours9 }10 }11});12picker.create();1314// Update to block after-hours15function switchToAfterHoursBlock() {16 picker.update({17 options: {18 clock: {19 disabledTime: {20 interval: ['00:00 - 08:00', '18:00 - 23:59']21 }22 }23 },24 create: true25 });26}2728// Multiple intervals for break times29function setBreakSchedule() {30 picker.update({31 options: {32 clock: {33 disabledTime: {34 interval: [35 '00:00 - 09:00', // Before work36 '12:00 - 13:00', // Lunch break37 '18:00 - 23:59' // After work38 ]39 }40 }41 },42 create: true43 });44}4546// 12h format intervals47const picker12h = new TimepickerUI('#picker-12h', {48 clock: {49 type: '12h',50 disabledTime: {51 interval: '09:00 AM - 05:00 PM'52 }53 }54});5556// Update 12h format intervals57picker12h.update({58 options: {59 clock: {60 disabledTime: {61 interval: ['06:00 AM - 08:00 AM', '06:00 PM - 10:00 PM']62 }63 }64 },65 create: true66});