improve time picker ux

This commit is contained in:
vcoppe
2024-06-13 19:59:55 +02:00
parent 758ad283d7
commit 4eaa45d28c
2 changed files with 44 additions and 1 deletions

View File

@@ -11,6 +11,14 @@
bind:value bind:value
on:input on:input
on:change on:change
on:keypress
on:focusin={() => {
let input = document.activeElement;
if (input instanceof HTMLInputElement) {
input.select();
}
}}
on:focusin
class="w-[22px] {$$props.class ?? ''}" class="w-[22px] {$$props.class ?? ''}"
{...$$restProps} {...$$restProps}
/> />
@@ -21,7 +29,11 @@
@apply px-0.5; @apply px-0.5;
@apply text-right; @apply text-right;
@apply border-none; @apply border-none;
@apply focus:ring-0;
@apply focus:ring-offset-0;
@apply focus:outline-none;
@apply focus-visible:ring-0; @apply focus-visible:ring-0;
@apply focus-visible:ring-offset-0; @apply focus-visible:ring-offset-0;
@apply focus-visible:outline-none;
} }
</style> </style>

View File

@@ -42,15 +42,32 @@
} }
seconds = (rounded % 60).toString().padStart(2, '0'); seconds = (rounded % 60).toString().padStart(2, '0');
} }
let container: HTMLDivElement;
let countKeyPress = 0;
function onKeyPress(e) {
if (['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(e.key)) {
countKeyPress++;
if (countKeyPress === 2) {
if (e.target.id === 'hours') {
container.querySelector('#minutes')?.focus();
} else if (e.target.id === 'minutes') {
container.querySelector('#seconds')?.focus();
}
}
}
}
</script> </script>
<div <div
class="flex flex-row items-center w-fit border rounded-md px-3 {disabled bind:this={container}
class="flex flex-row items-center w-fit border rounded-md px-3 focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 {disabled
? 'opacity-50 cursor-not-allowed' ? 'opacity-50 cursor-not-allowed'
: ''}" : ''}"
> >
{#if showHours} {#if showHours}
<TimeComponentInput <TimeComponentInput
id="hours"
bind:value={hours} bind:value={hours}
{disabled} {disabled}
class="w-[30px]" class="w-[30px]"
@@ -65,11 +82,16 @@
hours = 0; hours = 0;
} }
}} }}
on:keypress={onKeyPress}
on:focusin={() => {
countKeyPress = 0;
}}
on:change on:change
/> />
<span class="text-sm">:</span> <span class="text-sm">:</span>
{/if} {/if}
<TimeComponentInput <TimeComponentInput
id="minutes"
bind:value={minutes} bind:value={minutes}
{disabled} {disabled}
on:input={() => { on:input={() => {
@@ -86,10 +108,15 @@
} }
minutes = minutes.toString().padStart(showHours ? 2 : 1, '0'); minutes = minutes.toString().padStart(showHours ? 2 : 1, '0');
}} }}
on:keypress={onKeyPress}
on:focusin={() => {
countKeyPress = 0;
}}
on:change on:change
/> />
<span class="text-sm">:</span> <span class="text-sm">:</span>
<TimeComponentInput <TimeComponentInput
id="seconds"
bind:value={seconds} bind:value={seconds}
{disabled} {disabled}
on:input={() => { on:input={() => {
@@ -106,6 +133,10 @@
} }
seconds = seconds.toString().padStart(2, '0'); seconds = seconds.toString().padStart(2, '0');
}} }}
on:keypress={onKeyPress}
on:focusin={() => {
countKeyPress = 0;
}}
on:change on:change
/> />
</div> </div>