Example · Advanced
Multiple Timepickers
Use multiple timepicker instances on the same page
Multiple Instances
Each timepicker instance is independent. You can have as many pickers as needed with different configurations.
Different Themes
Two pickers with different themes on the same page:
index.tsxtsx
1import { useEffect, useRef, useMemo } from 'react';2import { TimepickerUI } from 'timepicker-ui';34function MultipleTimepickers() {5 const startTimeRef = useRef<HTMLInputElement>(null);6 const endTimeRef = useRef<HTMLInputElement>(null);7 const picker1Ref = useRef<TimepickerUI | null>(null);8 const picker2Ref = useRef<TimepickerUI | null>(null);910 const options1 = useMemo(() => ({11 ui: { theme: 'basic' },12 }), []);1314 const options2 = useMemo(() => ({15 ui: { theme: 'crane-straight' },16 }), []);1718 useEffect(() => {19 if (startTimeRef.current && endTimeRef.current) {20 picker1Ref.current = new TimepickerUI(21 startTimeRef.current,22 options123 );24 picker1Ref.current.create();2526 picker2Ref.current = new TimepickerUI(27 endTimeRef.current,28 options229 );30 picker2Ref.current.create();31 }3233 return () => {34 picker1Ref.current?.destroy();35 picker2Ref.current?.destroy();36 };37 }, []);3839 return (40 <>41 <input ref={startTimeRef} type="text" />42 <input ref={endTimeRef} type="text" />43 </>44 );45}
Array of Pickers
Managing multiple pickers using an array pattern:
index.tsxtsx
1import { useEffect, useRef } from 'react';2import { TimepickerUI } from 'timepicker-ui';34function MultiplePickersArray() {5 const inputRefs = useRef<HTMLInputElement[]>([]);6 const pickersRef = useRef<TimepickerUI[]>([]);78 useEffect(() => {9 // Initialize all pickers10 inputRefs.current.forEach((input, index) => {11 if (input) {12 const picker = new TimepickerUI(input, {13 ui: { theme: index % 2 === 0 ? 'basic' : 'dark' },14 });15 picker.create();16 pickersRef.current.push(picker);17 }18 });1920 // Cleanup all pickers21 return () => {22 pickersRef.current.forEach(picker => picker.destroy());23 pickersRef.current = [];24 };25 }, []);2627 return (28 <div>29 {['Morning', 'Afternoon', 'Evening'].map((label, i) => (30 <input31 key={label}32 ref={el => inputRefs.current[i] = el!}33 type="text"34 placeholder={`Select ${label} time`}35 />36 ))}37 </div>38 );39}
Vanilla JavaScript
Multiple pickers in vanilla JavaScript:
index.tstypescript
1import { TimepickerUI } from 'timepicker-ui';23// Select all timepicker inputs4const inputs = document.querySelectorAll('.timepicker-input');56// Store pickers for cleanup7const pickers = [];89// Initialize each picker10inputs.forEach((input, index) => {11 const picker = new TimepickerUI(input, {12 ui: { theme: index % 2 === 0 ? 'basic' : 'dark' },13 clock: { type: '24h' }14 });1516 picker.create();17 pickers.push(picker);18});1920// Cleanup when needed21function destroyAllPickers() {22 pickers.forEach(picker => picker.destroy());23 pickers.length = 0;24}