JSFiddle - React, Tailwind, and code Playground
HTML
<input class="time"/>
<input class="time"/>
<input class="time"/>
<div id="timepicker">
<div class="top">
<span class="cell">
<span class="arrow-up" id="timepicker-hour-up"></span>
</span>
<span class="cell">
<span class="arrow-up" id="timepicker-minute-up"></span>
</span>
</div>
<div class="middle">
<span class="cell">
<input id="hour" type="text" data-min="1" data-max="12" maxlength="2" value="12" />
</span>
<span class="cell">
<input id="minute" type="text" data-min="00" data-max="59" maxlength="2" value="00" />
</span>
</div>
<div class="bottom">
<span class="cell">
<span class="arrow-down" id="timepicker-hour-down"></span>
</span>
<span class="cell">
<span class="arrow-down" id="timepicker-minute-down"></span>
</span>
</div>
</div>
CSS
#timepicker {
display: none;
border: 1px solid #ddd;
width: 140px;
border-radius: 4px;
position: relative;
}
#timepicker .top,
#timepicker .middle,
#timepicker .bottom {
margin: 0 auto;
padding: 0px 13px;
}
#timepicker .top {
padding-bottom: 15px;
}
#timepicker .bottom {
padding-top: 15px;
}
#timepicker .top .cell, #timepicker .bottom .cell {
height: 0px;
}
#timepicker .cell {
width: 55px;
display: inline-block;
text-align: center;
}
#timepicker .middle .cell {
height: 45px;
line-height: 45px;
}
#timepicker .cell input {
width: 100%;
border: none;
text-align: center;
height: 100%;
border: 1px solid #ddd;
}
.arrow-up {
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #3d3d3d;
cursor: pointer;
}
.arrow-down {
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #3d3d3d;
cursor: pointer;
}
#timepicker:after, #timepicker:before {
bottom: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
#timepicker:after {
border-bottom-color: #fff;
border-width: 4px;
left: 10%;
margin-left: -4px;
}
#timepicker:before {
border-bottom-color: #bbb;
border-width: 5px;
left: 10%;
margin-left: -5px;
}
.time {
width: 100px;
height: 40px;
padding: 4px 6px;
border: 1px solid #ccc;
margin: 3px;
font-size: 14px;
text-align: center;
}
JavaScript
(function($) {
$.fn.timepicker = function() {
return this.each(function() {
var $input = $(this);
var timepickerOpen = false;
$input.on("focus", function() {
var position = $(this).position();
var height = $(this).outerHeight(true);
timepickerOpen = true;
$("#timepicker").css({
display : 'inline-block',
position : 'absolute',
top : (position.top + height),
left: position.left
});
});
$(document).on('mousedown.#timepicker', function (e) {
// Clicked outside the timepicker, hide it
if ( $(e.target).closest('#timepicker').length === 0 && $(e.target).closest('input').length === 0 && timepickerOpen) {
$($input).val( $("#hour").val()+":"+$("#minute").val());
$("#hour").val("12");
$("#minute").val("00");
$("#timepicker").hide();
}
});
$("#timepicker-hour-up").on("click", function() {
incHour( $("#hour") );
});
$("#timepicker-minute-up").on("click", function() {
incMinute( $("#minute") );
});
$("#timepicker-hour-down").on("click", function() {
decHour( $("#hour") );
});
$("#timepicker-minute-down").on("click", function() {
decMinute( $("#minute") );
});
});
function prettyNumber(num) {
return (num + "").length < 2 ? "0"+num : num;
}
function incHour($ele) {
var oldNum = $ele.val();
var newNum = (parseInt(oldNum) + 1);
$ele.val( newNum > 23 ? prettyNumber(0) : prettyNumber(newNum) );
}
function decHour($ele) {
var oldNum = $ele.val();
var newNum = (parseInt(oldNum) - 1);
$ele.val( newNum < 0 ? 23 : prettyNumber(newNum) );
}
function incMinute($ele) {
var oldNum = $ele.val();
var newNum = (parseInt(oldNum) + 15);
if( newNum > 59) {
$ele.val( prettyNumber(0) );
incHour( $("#hour") );
} else {
$ele.val( prettyNumber(newNum) );
}
}
function decMinute($ele) {
var oldNum = $ele.val();
var newNum = (parseInt(oldNum) - 15);
if( newNum < 0)...