grid icon indicating copy to clipboard operation
grid copied to clipboard

[UX] date picker filled by keyboard

Open kwladyka opened this issue 4 months ago • 1 comments

https://docs.svar.dev/svelte/grid/samples/#/editors/willow

To fill date user have to use mouse and click the date.

It gives better user experience when the user can also fill the date with keyboard. Image

The screen is solution which AI made. Too complex, but can be workaround until change in Svar. It creates new filed type.

<script lang="ts">
	import { clickOutside } from '@svar-ui/lib-dom';
	import { Text, Dropdown, Calendar } from '@svar-ui/svelte-core';
	import type { TEditorConfig } from '@svar-ui/svelte-grid';
	import { parseISODate, toDateInputValue } from '$lib/loan-calculator';

	let {
		editor,
		onsave,
		onapply,
		oncancel
	}: {
		editor: TEditorConfig;
		onsave?: (ignoreFocus: boolean) => void;
		onapply?: (value: Date | null) => void;
		oncancel?: () => void;
	} = $props();

	function parseText(str: string): Date | null {
		const trimmed = str.trim();
		if (!trimmed) return null;
		const date = /^\d{4}-\d{2}-\d{2}$/.test(trimmed) ? parseISODate(trimmed) : new Date(trimmed);
		return isNaN(date.getTime()) ? null : date;
	}

	let value = $state(editor.value instanceof Date ? editor.value : null);
	let text = $state(value ? toDateInputValue(value) : '');

	function commit() {
		const parsed = parseText(text);
		if (parsed) {
			value = parsed;
			text = toDateInputValue(parsed);
			onapply?.(parsed);
		} else {
			text = value ? toDateInputValue(value) : '';
		}
	}

	function onTextChange(ev: { value: string | number; input?: boolean }) {
		if (ev.input) return;
		commit();
	}

	function onKeydown(ev: KeyboardEvent) {
		if (ev.key === 'Enter') {
			commit();
			onsave?.(false);
		} else if (ev.key === 'Escape') {
			oncancel?.();
		}
	}

	function pick({ value: v }: { value: Date | null }) {
		value = v;
		text = v ? toDateInputValue(v) : '';
		onapply?.(v);
		onsave?.(false);
	}
</script>

<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
	class="wx-date-edit"
	onkeydown={onKeydown}
	use:clickOutside={() => {
		commit();
		onsave?.(true);
	}}
>
	<Text bind:value={text} onchange={onTextChange} icon="wxi-calendar" focus select />
	<Dropdown trackScroll width="auto">
		<Calendar value={value ?? undefined} onchange={pick} />
	</Dropdown>
</div>

<style>
	.wx-date-edit {
		width: 100%;
		height: 100%;
	}
</style>
registerInlineEditor('date-edit', GridDateEditor);
...
			{
				id: 'date',
				header: 'Disbursement date',
				editor: 'date-edit',
				flexgrow: 1,
				template: (value: Date) => (value ? toDateInputValue(value) : ''),
				footer: { text: 'Summary' }
			},

It will be nice to let user fill the date using keyboard by default. At least this is frustrating for me when I have to click each date instead of write.

kwladyka avatar Jul 23 '26 17:07 kwladyka

Hello @kwladyka ,

Thank you for your detailed feedback. We have reviewed your request and we are planning to include it in one of the upcoming releases. We will notify you as soon as the feature is available.

Natasha-eng avatar Aug 03 '26 06:08 Natasha-eng