import { createMemo, createSignal, For, Show, untrack } from "solid-js"; import Input from "./Input"; import availableOptions, { VARIABLE_OPTIONS, defauts } from "./options"; import { createStore } from "solid-js/store"; export default function App() { const [date, setDate] = createSignal(new Date()); const defaultLocale = navigator.languages[0]; const [locale, setLocale] = createSignal(defaultLocale); const [options, setOptions] = createStore(availableOptions); const [areDefaultsIncluded, setAreDefaultsIncluded] = createSignal(false); const formatOptions = createMemo(() => Object.fromEntries( Object.entries(options).map(([key, option]) => [key, option.selected]) ) ); const formatter = () => Intl.DateTimeFormat(locale(), formatOptions()); const formatted = () => { const value = date(); if (!value) return; return formatter().format(value); }; function update() { setDate(new Date()); requestAnimationFrame(update); } requestAnimationFrame(update); function handleChange(event: Event) { const input = event.target; if (!(input instanceof HTMLInputElement)) return; if (input.name === "locale" || input.name === "defaults") return; const key = input.name as keyof typeof options; let value: string | boolean | undefined | 1 | 2 | 3 = input.value; if (value === "undefined") value = undefined; else if (value === "true") value = true; else if (value === "false") value = false; else if (key === "fractionalSecondDigits") value = parseInt(value) as 1 | 2 | 3; switch (key) { case "dateStyle": case "timeStyle": setOptions( [ "year", "month", "day", "hour", "minute", "second", "dayPeriod", "fractionalSecondDigits", ], "selected", undefined ); break; case "fractionalSecondDigits": case "dayPeriod": case "day": case "month": case "year": case "hour": case "minute": case "second": setOptions(["dateStyle", "timeStyle"], "selected", undefined); break; } const oldValue = options[key].selected; console.debug("Validating"); try { setOptions(key, "selected", value); } catch (error) { console.debug("Invalid value", error); setOptions(key, "selected", oldValue); } } const json = () => JSON.stringify( formatOptions(), areDefaultsIncluded() ? undefined : (key, value) => { const defaultValue = Object.hasOwn(defauts, key) ? defauts[key as keyof typeof defauts] : undefined; if (defaultValue === value) return undefined; return value; }, 2 ); const codeSnippet = () => { const jsome = json(); const withoutQuotes = jsome.replace(/"(\w+)":/g, "$1:"); if (!areDefaultsIncluded()) { const isDefaultJson = jsome === "{}"; const isDefaultLocale = locale() === defaultLocale; if (isDefaultJson && isDefaultLocale) return `Intl.DateTimeFormat();`; if (isDefaultJson && !isDefaultLocale) return `Intl.DateTimeFormat("${locale()}");`; if (!isDefaultJson && isDefaultLocale) return `Intl.DateTimeFormat(undefined, ${withoutQuotes});`; } // Remove key quotes return `Intl.DateTimeFormat("${locale()}", ${withoutQuotes});`; }; function handleLocaleInput( event: InputEvent & { currentTarget: HTMLInputElement } ) { let locale: Intl.Locale; try { locale = new Intl.Locale(event.currentTarget.value); } catch (error) { console.debug("Invalid locale", error); return; } if (!locale.maximize().region) { console.debug("Invalid locale", locale); return; } setLocale(event.currentTarget.value); } return ( <>

Intl.DateTimeFormat playground

Current formatted time {formatted()}

In ISO format: {date().toISOString()}

In ISO format: {date().toLocaleString()}

{([key, option]) => { return ( {(value) => ( )} } >
{key} {(value) => { const asString = String(value); return ( ); }}
); }}
); }