front/Field: add support for datetime-local field type

This commit is contained in:
Alice Gaudon 2021-11-24 18:15:53 +01:00
parent aa1484749e
commit 59491d63ab
2 changed files with 39 additions and 1 deletions

View File

@ -0,0 +1,25 @@
export function dateToDatetimeLocal(date: Date): string {
function ten(i: number) {
return (i < 10 ? '0' : '') + i;
}
const YYYY = date.getFullYear();
const MM = ten(date.getMonth() + 1);
const DD = ten(date.getDate());
const HH = ten(date.getHours());
const II = ten(date.getMinutes());
const SS = ten(date.getSeconds());
return YYYY + '-' + MM + '-' + DD + 'T' +
HH + ':' + II + ':' + SS;
}
export const dateToIsoString = (function (BST) {
// BST should not be present as UTC time
if (new Date(BST).toISOString().slice(0, 16) === BST) {
return (date: Date): string => {
return new Date(date.getTime() + date.getTimezoneOffset() * 60000)
.toISOString();
};
} else {
return (date: Date) => date.toISOString();
}
}('2006-06-06T06:06'));

View File

@ -4,11 +4,13 @@
import Message from "../components/Message.svelte"; import Message from "../components/Message.svelte";
import Icon from "./Icon.svelte"; import Icon from "./Icon.svelte";
import {getContext} from "svelte"; import {getContext} from "svelte";
import {dateToDatetimeLocal, dateToIsoString} from "../../ts/datetime-local.js";
export let type: string; export let type: string;
export let name: string; export let name: string;
type FieldValue = string | number | Record<string, FieldValue>; type FieldValue = string | number | Record<string, FieldValue>;
export let value: FieldValue | undefined = undefined; export let value: FieldValue | undefined = undefined;
export let initialValue: FieldValue | undefined = undefined;
export let placeholder: string | undefined = undefined; export let placeholder: string | undefined = undefined;
export let hint: string | undefined = undefined; export let hint: string | undefined = undefined;
export let extraData: string[] | undefined = undefined; export let extraData: string[] | undefined = undefined;
@ -19,8 +21,12 @@
const fieldId = `${formId}-${name}-field`; const fieldId = `${formId}-${name}-field`;
const previousFormData = $locals.previousFormData() || []; const previousFormData = $locals.previousFormData() || [];
let previousFieldData = previousFormData[name];
if (typeof value === 'number' && previousFieldData) previousFieldData = Number(previousFieldData);
value = type !== 'hidden' && previousFormData[name] || value || validation?.value || ''; value = type !== 'hidden' && previousFieldData || value || initialValue || validation?.value || '';
$: initialDatetimeLocalValue = type === 'datetime-local' && typeof value === 'string' ? dateToDatetimeLocal(new Date(value)) : undefined;
function durationValue(f: string): number { function durationValue(f: string): number {
if (previousFormData[name]) { if (previousFormData[name]) {
@ -61,6 +67,10 @@
if (this.type === 'file') { if (this.type === 'file') {
handleFileInput(); handleFileInput();
} }
if (this.type === 'datetime-local') {
value = dateToIsoString(new Date(this.value));
}
} }
let input: HTMLInputElement; let input: HTMLInputElement;
@ -414,6 +424,9 @@
</div> </div>
{:else if type === 'checkbox'} {:else if type === 'checkbox'}
<input {type} {name} id={fieldId} checked={value === 'on'} {...$$restProps} bind:this={input}> <input {type} {name} id={fieldId} checked={value === 'on'} {...$$restProps} bind:this={input}>
{:else if type === 'datetime-local'}
<input {type} bind:this={input} on:input={handleInput} value={initialDatetimeLocalValue}>
<input type="hidden" {name} {value}>
{:else} {:else}
<input {type} {name} id={fieldId} {value} {...$$restProps} bind:this={input} on:input={handleInput} <input {type} {name} id={fieldId} {value} {...$$restProps} bind:this={input} on:input={handleInput}
tabindex={type === 'file' ? '-1' : undefined}> tabindex={type === 'file' ? '-1' : undefined}>