mirror of
https://github.com/bitbaum/whenkit.git
synced 2026-12-05 06:58:35 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0173ac305c |
No files matched your search
@@ -5,7 +5,7 @@ length chips, a readable date field over the native picker, and a booking
|
||||
request that resolves to `{ startsAt, endsAt }` in the provider's time zone.
|
||||
|
||||
```bash
|
||||
pnpm add github:bitbaum/whenkit#v0.2.1
|
||||
pnpm add github:bitbaum/whenkit#v0.3.0
|
||||
```
|
||||
|
||||
```tsx
|
||||
@@ -48,6 +48,28 @@ rented by the day. Three decisions replace that:
|
||||
autofill, a11y) transparent on top of a display that reads "Sat, 11 Oct ·
|
||||
14:00", and opens the picker on tap.
|
||||
|
||||
## Replacing the date inputs you already have
|
||||
|
||||
`DateInput` is a drop-in for `<input type="date">` / `type="datetime-local"`:
|
||||
same props, same `onChange(event)`, so a call site changes its tag and nothing
|
||||
else.
|
||||
|
||||
```tsx
|
||||
import { DateInput } from "@bitbaum/whenkit/react";
|
||||
import "@bitbaum/whenkit/styles.css";
|
||||
|
||||
// before: <input type="date" value={due} onChange={(e) => setDue(e.target.value)} className="input" />
|
||||
<DateInput value={due} onChange={(e) => setDue(e.target.value)} className="input" />;
|
||||
```
|
||||
|
||||
Your own `className` lands on the visible box, and it wins: whenkit's look sits
|
||||
in `@layer base`, below Tailwind utilities and plain app CSS. The real input
|
||||
stays on top, invisible, so the picker, keyboard entry, autofill, `required`
|
||||
validation and form submission all keep working. Controlled (`value`) or
|
||||
uncontrolled (`defaultValue` + `name`, e.g. a server-action form). The empty
|
||||
state and weekday/month words follow `<html lang>` (or pass `locale` /
|
||||
`placeholder`).
|
||||
|
||||
## Rules it enforces
|
||||
|
||||
- Dates are `YYYY-MM-DD`, times `HH:MM` — wall-clock strings. They become an
|
||||
@@ -71,7 +93,7 @@ rented by the day. Three decisions replace that:
|
||||
`type BookingChoice`.
|
||||
|
||||
**React** (`@bitbaum/whenkit/react`): `BookingRequestPicker`, `DayStrip`,
|
||||
`ChipGroup`, `DateField`.
|
||||
`ChipGroup`, `DateInput`, `DateField` (a `DateInput` with its own label).
|
||||
|
||||
**Look** (`@bitbaum/whenkit/styles.css`): `--wk-*` variables default to
|
||||
`@bitbaum/design-tokens`; set them once to match any other app.
|
||||
|
||||
+8
-1
@@ -1,7 +1,7 @@
|
||||
// Visual + browser check page. Not shipped.
|
||||
import { useState } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { BookingRequestPicker, DateField } from "../src/react/index.js";
|
||||
import { BookingRequestPicker, DateField, DateInput } from "../src/react/index.js";
|
||||
import { resolveRequest, todayIn, type BookingChoice } from "../src/index.js";
|
||||
|
||||
const ZONE = "Europe/Zurich";
|
||||
@@ -43,3 +43,10 @@ createRoot(document.getElementById("dialog")!).render(
|
||||
</button>
|
||||
</>,
|
||||
);
|
||||
|
||||
createRoot(document.getElementById("own-class")!).render(
|
||||
<DateInput className="app-input" defaultValue="2026-10-11" id="own" />,
|
||||
);
|
||||
createRoot(document.getElementById("util-class")!).render(
|
||||
<DateInput className="util-input" defaultValue="2026-10-11" id="util" />,
|
||||
);
|
||||
@@ -47,6 +47,20 @@
|
||||
background: hsl(var(--surface-base, 0 0% 100%));
|
||||
box-sizing: border-box;
|
||||
}
|
||||
/* An app's OWN input class, unlayered plain CSS and a layered utility: both
|
||||
must beat whenkit's defaults (they live in @layer base). */
|
||||
.app-input {
|
||||
padding: 2px 6px;
|
||||
border: 3px solid rgb(255, 0, 0);
|
||||
border-radius: 0;
|
||||
}
|
||||
@layer utilities {
|
||||
.util-input {
|
||||
border-width: 5px;
|
||||
border-style: dashed;
|
||||
border-color: rgb(0, 0, 255);
|
||||
}
|
||||
}
|
||||
.resolved {
|
||||
font-size: 11px;
|
||||
opacity: 0.5;
|
||||
@@ -71,6 +85,10 @@
|
||||
<div id="field"></div>
|
||||
</section>
|
||||
</div>
|
||||
<div class="grid" style="margin-top: 24px">
|
||||
<section><div id="own-class"></div></section>
|
||||
<section><div id="util-class"></div></section>
|
||||
</div>
|
||||
<div class="dialog-like" id="dialog"></div>
|
||||
</main>
|
||||
<script src="./dist/demo.js"></script>
|
||||
|
||||
Vendored
+3
-1
@@ -1,3 +1,4 @@
|
||||
import { describeValue } from "./DateInput.js";
|
||||
export interface DateFieldProps {
|
||||
label: string;
|
||||
/** `YYYY-MM-DD` (date) or `YYYY-MM-DDTHH:MM` (withTime), or "" when empty. */
|
||||
@@ -13,6 +14,7 @@ export interface DateFieldProps {
|
||||
hint?: string;
|
||||
className?: string;
|
||||
}
|
||||
export declare function describeFieldValue(value: string, withTime: boolean, locale?: string): string;
|
||||
/** Kept for callers of 0.1–0.2: the words for a value, or "" if invalid. */
|
||||
export declare const describeFieldValue: typeof describeValue;
|
||||
export declare function DateField({ label, value, onChange, withTime, min, max, required, name, placeholder, locale, hint, className, }: DateFieldProps): import("react").JSX.Element;
|
||||
//# sourceMappingURL=DateField.d.ts.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"DateField.d.ts","sourceRoot":"","sources":["../../src/react/DateField.tsx"],"names":[],"mappings":"AAoBA,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,SAAO,GAAG,MAAM,CAa1F;AAED,wBAAgB,SAAS,CAAC,EACxB,KAAK,EACL,KAAK,EACL,QAAQ,EACR,QAAgB,EAChB,GAAG,EACH,GAAG,EACH,QAAQ,EACR,IAAI,EACJ,WAAW,EACX,MAAa,EACb,IAAI,EACJ,SAAS,GACV,EAAE,cAAc,+BA0DhB"}
|
||||
{"version":3,"file":"DateField.d.ts","sourceRoot":"","sources":["../../src/react/DateField.tsx"],"names":[],"mappings":"AAWA,OAAO,EAAa,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE1D,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,4EAA4E;AAC5E,eAAO,MAAM,kBAAkB,sBAAgB,CAAC;AAEhD,wBAAgB,SAAS,CAAC,EACxB,KAAK,EACL,KAAK,EACL,QAAQ,EACR,QAAgB,EAChB,GAAG,EACH,GAAG,EACH,QAAQ,EACR,IAAI,EACJ,WAAW,EACX,MAAM,EACN,IAAI,EACJ,SAAS,GACV,EAAE,cAAc,+BA2BhB"}
|
||||
Vendored
+8
-37
@@ -1,48 +1,19 @@
|
||||
"use client";
|
||||
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
/**
|
||||
* A date (or date + time) field that reads like a sentence and picks like the
|
||||
* phone does.
|
||||
*
|
||||
* The native picker is the best one there is on a phone — what looked bad
|
||||
* across the fleet was the raw field around it: the browser's locale
|
||||
* placeholder ("tt.mm.jjjj, --:--"), unstyled, different in every browser.
|
||||
* So the native input stays (keyboard entry, a11y, autofill all keep
|
||||
* working) and sits transparently over a display that says "Sat, 11 Oct ·
|
||||
* 14:00". A tap anywhere opens the platform picker via `showPicker()`.
|
||||
* A labelled date (or date + time) field: `DateInput` with its own label and
|
||||
* an optional hint, for places that do not already have a form-field wrapper.
|
||||
* Apps that do (most) use `DateInput` directly.
|
||||
*
|
||||
* Values are `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM` wall-clock strings — exactly
|
||||
* what the native input speaks — never instants.
|
||||
*/
|
||||
import { useId } from "react";
|
||||
import { dayLabel, formatTime } from "../labels.js";
|
||||
import { isISODate, isWallTime } from "../calendar.js";
|
||||
export function describeFieldValue(value, withTime, locale = "en") {
|
||||
const [date, time] = value.split("T");
|
||||
if (!isISODate(date))
|
||||
return "";
|
||||
const day = dayLabel(date, { locale });
|
||||
const dayText = new Intl.DateTimeFormat(locale, {
|
||||
timeZone: "UTC",
|
||||
weekday: "short",
|
||||
day: "numeric",
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
}).format(new Date(`${date}T00:00:00Z`));
|
||||
if (withTime && isWallTime(time))
|
||||
return `${dayText} · ${formatTime(time, locale)}`;
|
||||
return dayText || day.short;
|
||||
}
|
||||
export function DateField({ label, value, onChange, withTime = false, min, max, required, name, placeholder, locale = "en", hint, className, }) {
|
||||
import { DateInput, describeValue } from "./DateInput.js";
|
||||
/** Kept for callers of 0.1–0.2: the words for a value, or "" if invalid. */
|
||||
export const describeFieldValue = describeValue;
|
||||
export function DateField({ label, value, onChange, withTime = false, min, max, required, name, placeholder, locale, hint, className, }) {
|
||||
const id = useId();
|
||||
const shown = value ? describeFieldValue(value, withTime, locale) : "";
|
||||
return (_jsxs("div", { className: ["wk-field", className].filter(Boolean).join(" "), children: [_jsx("label", { className: "wk-label", htmlFor: id, children: label }), _jsxs("div", { className: "wk-field-box", "data-empty": shown ? undefined : true, children: [_jsx("span", { className: "wk-field-text", "aria-hidden": "true", children: shown || placeholder || (withTime ? "Pick a day and time" : "Pick a day") }), _jsxs("svg", { className: "wk-field-icon", viewBox: "0 0 24 24", "aria-hidden": "true", focusable: "false", children: [_jsx("rect", { x: "3.5", y: "5", width: "17", height: "15", rx: "2.5", fill: "none", stroke: "currentColor", strokeWidth: "1.6" }), _jsx("path", { d: "M3.5 9.5h17M8 3v4M16 3v4", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" })] }), _jsx("input", { id: id, className: "wk-field-input", type: withTime ? "datetime-local" : "date", value: value, min: min, max: max, required: required, name: name, "aria-describedby": hint ? `${id}-hint` : undefined, onChange: (e) => onChange(e.target.value), onClick: (e) => {
|
||||
try {
|
||||
e.currentTarget.showPicker?.();
|
||||
}
|
||||
catch {
|
||||
// Not allowed (e.g. cross-origin iframe): native focus still works.
|
||||
}
|
||||
} })] }), hint && (_jsx("p", { className: "wk-hint", id: `${id}-hint`, children: hint }))] }));
|
||||
return (_jsxs("div", { className: ["wk-field", className].filter(Boolean).join(" "), children: [_jsx("label", { className: "wk-label", htmlFor: id, children: label }), _jsx(DateInput, { id: id, type: withTime ? "datetime-local" : "date", value: value, onChange: (e) => onChange(e.target.value), min: min, max: max, required: required, name: name, placeholder: placeholder, locale: locale, "aria-describedby": hint ? `${id}-hint` : undefined }), hint && (_jsx("p", { className: "wk-hint", id: `${id}-hint`, children: hint }))] }));
|
||||
}
|
||||
//# sourceMappingURL=DateField.js.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"DateField.js","sourceRoot":"","sources":["../../src/react/DateField.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,QA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,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EACzC,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,GAAG,EACR,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE,MAAM,sBACI,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,GACjD,EACD,IAAI,IAAI,CACP,YAAG,SAAS,EAAC,SAAS,EAAC,EAAE,EAAE,GAAG,EAAE,OAAO,YACpC,IAAI,GACH,CACL,IACG,CACP,CAAC;AACJ,CAAC"}
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* A drop-in for `<input type="date">` and `<input type="datetime-local">`.
|
||||
*
|
||||
* Same props, same `onChange(event)` — a call site changes its tag and
|
||||
* nothing else. What changes is what a person sees: the browser's bare field
|
||||
* ("tt.mm.jjjj, --:--", different in every browser) is replaced by the value
|
||||
* in words ("Sat, 11 Oct 2026 · 14:00") in the app's OWN input styling (its
|
||||
* `className` lands on the visible box), while the real input stays on top,
|
||||
* transparent, so the platform picker, keyboard entry, autofill, validation
|
||||
* bubbles and form submission all keep working.
|
||||
*
|
||||
* Controlled (`value`) or uncontrolled (`defaultValue`, e.g. a server-action
|
||||
* form): the visible text follows whichever the app uses.
|
||||
*/
|
||||
import { type ChangeEvent, type InputHTMLAttributes } from "react";
|
||||
export interface DateInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "onChange" | "value" | "defaultValue"> {
|
||||
/** `date` (default) or `datetime-local`. */
|
||||
type?: "date" | "datetime-local";
|
||||
/** `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM`. */
|
||||
value?: string;
|
||||
defaultValue?: string;
|
||||
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
|
||||
/** Words shown while empty. Defaults to a short phrase in the page language. */
|
||||
placeholder?: string;
|
||||
/** BCP 47 tag. Defaults to the page's `<html lang>` (read after mount). */
|
||||
locale?: string;
|
||||
/** Applied to the visible box — give it the same classes as the app's inputs. */
|
||||
className?: string;
|
||||
}
|
||||
export declare function emptyWords(locale: string, withTime: boolean): string;
|
||||
/** "Sat, 11 Oct 2026" or "Sat, 11 Oct 2026 · 14:00"; "" when not a valid value. */
|
||||
export declare function describeValue(value: string, withTime: boolean, locale?: string): string;
|
||||
export declare const DateInput: import("react").ForwardRefExoticComponent<DateInputProps & import("react").RefAttributes<HTMLInputElement>>;
|
||||
//# sourceMappingURL=DateInput.d.ts.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"DateInput.d.ts","sourceRoot":"","sources":["../../src/react/DateInput.tsx"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,mBAAmB,EAEzB,MAAM,OAAO,CAAC;AAIf,MAAM,WAAW,cAAe,SAAQ,IAAI,CAC1C,mBAAmB,CAAC,gBAAgB,CAAC,EACrC,MAAM,GAAG,UAAU,GAAG,OAAO,GAAG,cAAc,CAC/C;IACC,4CAA4C;IAC5C,IAAI,CAAC,EAAE,MAAM,GAAG,gBAAgB,CAAC;IACjC,0CAA0C;IAC1C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAC;IAC1D,gFAAgF;IAChF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,2EAA2E;IAC3E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAUD,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,CAGpE;AAED,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,SAAO,GAAG,MAAM,CAWrF;AAWD,eAAO,MAAM,SAAS,6GAiFpB,CAAC"}
|
||||
Vendored
+83
@@ -0,0 +1,83 @@
|
||||
"use client";
|
||||
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
/**
|
||||
* A drop-in for `<input type="date">` and `<input type="datetime-local">`.
|
||||
*
|
||||
* Same props, same `onChange(event)` — a call site changes its tag and
|
||||
* nothing else. What changes is what a person sees: the browser's bare field
|
||||
* ("tt.mm.jjjj, --:--", different in every browser) is replaced by the value
|
||||
* in words ("Sat, 11 Oct 2026 · 14:00") in the app's OWN input styling (its
|
||||
* `className` lands on the visible box), while the real input stays on top,
|
||||
* transparent, so the platform picker, keyboard entry, autofill, validation
|
||||
* bubbles and form submission all keep working.
|
||||
*
|
||||
* Controlled (`value`) or uncontrolled (`defaultValue`, e.g. a server-action
|
||||
* form): the visible text follows whichever the app uses.
|
||||
*/
|
||||
import { forwardRef, useEffect, useState, } from "react";
|
||||
import { isISODate, isWallTime } from "../calendar.js";
|
||||
import { formatTime } from "../labels.js";
|
||||
const EMPTY_WORDS = {
|
||||
en: ["Pick a date", "Pick a date and time"],
|
||||
de: ["Datum wählen", "Datum und Uhrzeit wählen"],
|
||||
fr: ["Choisir une date", "Choisir la date et l'heure"],
|
||||
it: ["Scegli una data", "Scegli data e ora"],
|
||||
es: ["Elegir fecha", "Elegir fecha y hora"],
|
||||
};
|
||||
export function emptyWords(locale, withTime) {
|
||||
const words = EMPTY_WORDS[locale.toLowerCase().split("-")[0]] ?? EMPTY_WORDS.en;
|
||||
return words[withTime ? 1 : 0];
|
||||
}
|
||||
/** "Sat, 11 Oct 2026" or "Sat, 11 Oct 2026 · 14:00"; "" when not a valid value. */
|
||||
export function describeValue(value, withTime, locale = "en") {
|
||||
const [date, time] = value.split("T");
|
||||
if (!isISODate(date))
|
||||
return "";
|
||||
const day = new Intl.DateTimeFormat(locale, {
|
||||
timeZone: "UTC",
|
||||
weekday: "short",
|
||||
day: "numeric",
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
}).format(new Date(`${date}T00:00:00Z`));
|
||||
return withTime && isWallTime(time) ? `${day} · ${formatTime(time, locale)}` : day;
|
||||
}
|
||||
/** An unknown tag from <html lang> must never throw during render. */
|
||||
function safeDescribe(value, withTime, locale) {
|
||||
try {
|
||||
return describeValue(value, withTime, locale);
|
||||
}
|
||||
catch {
|
||||
return describeValue(value, withTime, "en");
|
||||
}
|
||||
}
|
||||
export const DateInput = forwardRef(function DateInput({ type = "date", value, defaultValue, onChange, placeholder, locale, className, style, onClick, ...rest }, ref) {
|
||||
const withTime = type === "datetime-local";
|
||||
const [inner, setInner] = useState(defaultValue ?? "");
|
||||
const [lang, setLang] = useState(locale ?? "en");
|
||||
useEffect(() => {
|
||||
if (locale)
|
||||
setLang(locale);
|
||||
else
|
||||
setLang(document.documentElement.lang || "en");
|
||||
}, [locale]);
|
||||
const current = value ?? inner;
|
||||
const text = current ? safeDescribe(current, withTime, lang) : "";
|
||||
const empty = !text;
|
||||
const handleClick = (event) => {
|
||||
onClick?.(event);
|
||||
if (event.defaultPrevented)
|
||||
return;
|
||||
try {
|
||||
event.currentTarget.showPicker?.();
|
||||
}
|
||||
catch {
|
||||
// Not permitted here (e.g. a cross-origin frame): native focus still works.
|
||||
}
|
||||
};
|
||||
return (_jsxs("span", { className: ["wk-input", className].filter(Boolean).join(" "), "data-empty": empty || undefined, "data-disabled": rest.disabled || undefined, "data-invalid": rest["aria-invalid"] === true || rest["aria-invalid"] === "true" || undefined, style: style, children: [_jsx("span", { className: "wk-input-text", "aria-hidden": "true", children: text || placeholder || emptyWords(lang, withTime) }), _jsxs("svg", { className: "wk-input-icon", viewBox: "0 0 24 24", "aria-hidden": "true", focusable: "false", children: [_jsx("rect", { x: "3.5", y: "5", width: "17", height: "15", rx: "2.5", fill: "none", stroke: "currentColor", strokeWidth: "1.6" }), _jsx("path", { d: "M3.5 9.5h17M8 3v4M16 3v4", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" })] }), _jsx("input", { ...rest, ref: ref, type: type, className: "wk-input-native", ...(value !== undefined ? { value } : { defaultValue }), onClick: handleClick, onChange: (event) => {
|
||||
setInner(event.target.value);
|
||||
onChange?.(event);
|
||||
} })] }));
|
||||
});
|
||||
//# sourceMappingURL=DateInput.js.map
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"DateInput.js","sourceRoot":"","sources":["../../src/react/DateInput.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb;;;;;;;;;;;;;GAaG;AACH,OAAO,EACL,UAAU,EACV,SAAS,EACT,QAAQ,GAIT,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAoB1C,MAAM,WAAW,GAAqC;IACpD,EAAE,EAAE,CAAC,aAAa,EAAE,sBAAsB,CAAC;IAC3C,EAAE,EAAE,CAAC,cAAc,EAAE,0BAA0B,CAAC;IAChD,EAAE,EAAE,CAAC,kBAAkB,EAAE,4BAA4B,CAAC;IACtD,EAAE,EAAE,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IAC5C,EAAE,EAAE,CAAC,cAAc,EAAE,qBAAqB,CAAC;CAC5C,CAAC;AAEF,MAAM,UAAU,UAAU,CAAC,MAAc,EAAE,QAAiB;IAC1D,MAAM,KAAK,GAAG,WAAW,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC,EAAE,CAAC;IAChF,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CA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eAAe,iBAAa,MAAM,YAC/C,IAAI,IAAI,WAAW,IAAI,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,GAC7C,EACP,eAAK,SAAS,EAAC,eAAe,EAAC,OAAO,EAAC,WAAW,iBAAa,MAAM,EAAC,SAAS,EAAC,OAAO,aACrF,eACE,CAAC,EAAC,KAAK,EACP,CAAC,EAAC,GAAG,EACL,KAAK,EAAC,IAAI,EACV,MAAM,EAAC,IAAI,EACX,EAAE,EAAC,KAAK,EACR,IAAI,EAAC,MAAM,EACX,MAAM,EAAC,cAAc,EACrB,WAAW,EAAC,KAAK,GACjB,EACF,eACE,CAAC,EAAC,0BAA0B,EAC5B,IAAI,EAAC,MAAM,EACX,MAAM,EAAC,cAAc,EACrB,WAAW,EAAC,KAAK,EACjB,aAAa,EAAC,OAAO,GACrB,IACE,EACN,mBACM,IAAI,EACR,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,IAAI,EACV,SAAS,EAAC,iBAAiB,KACvB,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EACxD,OAAO,EAAE,WAAW,EACpB,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;oBAClB,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;oBAC7B,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,GACD,IACG,CACR,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
||||
Vendored
+1
@@ -1,5 +1,6 @@
|
||||
export { ChipGroup, type ChipGroupProps, type ChipOption } from "./ChipGroup.js";
|
||||
export { DateField, describeFieldValue, type DateFieldProps } from "./DateField.js";
|
||||
export { DateInput, describeValue, emptyWords, type DateInputProps } from "./DateInput.js";
|
||||
export { DayStrip, type DayStripProps } from "./DayStrip.js";
|
||||
export { BookingRequestPicker, DEFAULT_PICKER_WORDS, type BookingPickerWords, type BookingRequestPickerProps, } from "./BookingRequestPicker.js";
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACpF,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAC7D,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,KAAK,kBAAkB,EACvB,KAAK,yBAAyB,GAC/B,MAAM,2BAA2B,CAAC"}
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACpF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC3F,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAC7D,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,KAAK,kBAAkB,EACvB,KAAK,yBAAyB,GAC/B,MAAM,2BAA2B,CAAC"}
|
||||
Vendored
+1
@@ -1,5 +1,6 @@
|
||||
export { ChipGroup } from "./ChipGroup.js";
|
||||
export { DateField, describeFieldValue } from "./DateField.js";
|
||||
export { DateInput, describeValue, emptyWords } from "./DateInput.js";
|
||||
export { DayStrip } from "./DayStrip.js";
|
||||
export { BookingRequestPicker, DEFAULT_PICKER_WORDS, } from "./BookingRequestPicker.js";
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAwC,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAuB,MAAM,gBAAgB,CAAC;AACpF,OAAO,EAAE,QAAQ,EAAsB,MAAM,eAAe,CAAC;AAC7D,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAGrB,MAAM,2BAA2B,CAAC"}
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAwC,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAuB,MAAM,gBAAgB,CAAC;AACpF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAuB,MAAM,gBAAgB,CAAC;AAC3F,OAAO,EAAE,QAAQ,EAAsB,MAAM,eAAe,CAAC;AAC7D,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAGrB,MAAM,2BAA2B,CAAC"}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@bitbaum/whenkit",
|
||||
"version": "0.2.1",
|
||||
"version": "0.3.0",
|
||||
"description": "Dates and times people can pick without typing: day chips, time chips, length chips, a readable date field over the native picker, and a low-friction booking request that resolves to {startsAt, endsAt} in the provider's zone.",
|
||||
"license": "MIT",
|
||||
"author": "Cato",
|
||||
|
||||
+20
-70
@@ -1,22 +1,15 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* A date (or date + time) field that reads like a sentence and picks like the
|
||||
* phone does.
|
||||
*
|
||||
* The native picker is the best one there is on a phone — what looked bad
|
||||
* across the fleet was the raw field around it: the browser's locale
|
||||
* placeholder ("tt.mm.jjjj, --:--"), unstyled, different in every browser.
|
||||
* So the native input stays (keyboard entry, a11y, autofill all keep
|
||||
* working) and sits transparently over a display that says "Sat, 11 Oct ·
|
||||
* 14:00". A tap anywhere opens the platform picker via `showPicker()`.
|
||||
* A labelled date (or date + time) field: `DateInput` with its own label and
|
||||
* an optional hint, for places that do not already have a form-field wrapper.
|
||||
* Apps that do (most) use `DateInput` directly.
|
||||
*
|
||||
* Values are `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM` wall-clock strings — exactly
|
||||
* what the native input speaks — never instants.
|
||||
*/
|
||||
import { useId } from "react";
|
||||
import { dayLabel, formatTime } from "../labels.js";
|
||||
import { isISODate, isWallTime } from "../calendar.js";
|
||||
import { DateInput, describeValue } from "./DateInput.js";
|
||||
|
||||
export interface DateFieldProps {
|
||||
label: string;
|
||||
@@ -34,20 +27,8 @@ export interface DateFieldProps {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function describeFieldValue(value: string, withTime: boolean, locale = "en"): string {
|
||||
const [date, time] = value.split("T");
|
||||
if (!isISODate(date)) return "";
|
||||
const day = dayLabel(date, { locale });
|
||||
const dayText = new Intl.DateTimeFormat(locale, {
|
||||
timeZone: "UTC",
|
||||
weekday: "short",
|
||||
day: "numeric",
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
}).format(new Date(`${date}T00:00:00Z`));
|
||||
if (withTime && isWallTime(time)) return `${dayText} · ${formatTime(time, locale)}`;
|
||||
return dayText || day.short;
|
||||
}
|
||||
/** Kept for callers of 0.1–0.2: the words for a value, or "" if invalid. */
|
||||
export const describeFieldValue = describeValue;
|
||||
|
||||
export function DateField({
|
||||
label,
|
||||
@@ -59,60 +40,29 @@ export function DateField({
|
||||
required,
|
||||
name,
|
||||
placeholder,
|
||||
locale = "en",
|
||||
locale,
|
||||
hint,
|
||||
className,
|
||||
}: DateFieldProps) {
|
||||
const id = useId();
|
||||
const shown = value ? describeFieldValue(value, withTime, locale) : "";
|
||||
return (
|
||||
<div className={["wk-field", className].filter(Boolean).join(" ")}>
|
||||
<label className="wk-label" htmlFor={id}>
|
||||
{label}
|
||||
</label>
|
||||
<div className="wk-field-box" data-empty={shown ? undefined : true}>
|
||||
<span className="wk-field-text" aria-hidden="true">
|
||||
{shown || placeholder || (withTime ? "Pick a day and time" : "Pick a day")}
|
||||
</span>
|
||||
<svg className="wk-field-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
||||
<rect
|
||||
x="3.5"
|
||||
y="5"
|
||||
width="17"
|
||||
height="15"
|
||||
rx="2.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
/>
|
||||
<path
|
||||
d="M3.5 9.5h17M8 3v4M16 3v4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
/>
|
||||
</svg>
|
||||
<input
|
||||
id={id}
|
||||
className="wk-field-input"
|
||||
type={withTime ? "datetime-local" : "date"}
|
||||
value={value}
|
||||
min={min}
|
||||
max={max}
|
||||
required={required}
|
||||
name={name}
|
||||
aria-describedby={hint ? `${id}-hint` : undefined}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
onClick={(e) => {
|
||||
try {
|
||||
e.currentTarget.showPicker?.();
|
||||
} catch {
|
||||
// Not allowed (e.g. cross-origin iframe): native focus still works.
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<DateInput
|
||||
id={id}
|
||||
type={withTime ? "datetime-local" : "date"}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
min={min}
|
||||
max={max}
|
||||
required={required}
|
||||
name={name}
|
||||
placeholder={placeholder}
|
||||
locale={locale}
|
||||
aria-describedby={hint ? `${id}-hint` : undefined}
|
||||
/>
|
||||
{hint && (
|
||||
<p className="wk-hint" id={`${id}-hint`}>
|
||||
{hint}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* A drop-in for `<input type="date">` and `<input type="datetime-local">`.
|
||||
*
|
||||
* Same props, same `onChange(event)` — a call site changes its tag and
|
||||
* nothing else. What changes is what a person sees: the browser's bare field
|
||||
* ("tt.mm.jjjj, --:--", different in every browser) is replaced by the value
|
||||
* in words ("Sat, 11 Oct 2026 · 14:00") in the app's OWN input styling (its
|
||||
* `className` lands on the visible box), while the real input stays on top,
|
||||
* transparent, so the platform picker, keyboard entry, autofill, validation
|
||||
* bubbles and form submission all keep working.
|
||||
*
|
||||
* Controlled (`value`) or uncontrolled (`defaultValue`, e.g. a server-action
|
||||
* form): the visible text follows whichever the app uses.
|
||||
*/
|
||||
import {
|
||||
forwardRef,
|
||||
useEffect,
|
||||
useState,
|
||||
type ChangeEvent,
|
||||
type InputHTMLAttributes,
|
||||
type MouseEvent,
|
||||
} from "react";
|
||||
import { isISODate, isWallTime } from "../calendar.js";
|
||||
import { formatTime } from "../labels.js";
|
||||
|
||||
export interface DateInputProps extends Omit<
|
||||
InputHTMLAttributes<HTMLInputElement>,
|
||||
"type" | "onChange" | "value" | "defaultValue"
|
||||
> {
|
||||
/** `date` (default) or `datetime-local`. */
|
||||
type?: "date" | "datetime-local";
|
||||
/** `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM`. */
|
||||
value?: string;
|
||||
defaultValue?: string;
|
||||
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
|
||||
/** Words shown while empty. Defaults to a short phrase in the page language. */
|
||||
placeholder?: string;
|
||||
/** BCP 47 tag. Defaults to the page's `<html lang>` (read after mount). */
|
||||
locale?: string;
|
||||
/** Applied to the visible box — give it the same classes as the app's inputs. */
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const EMPTY_WORDS: Record<string, [string, string]> = {
|
||||
en: ["Pick a date", "Pick a date and time"],
|
||||
de: ["Datum wählen", "Datum und Uhrzeit wählen"],
|
||||
fr: ["Choisir une date", "Choisir la date et l'heure"],
|
||||
it: ["Scegli una data", "Scegli data e ora"],
|
||||
es: ["Elegir fecha", "Elegir fecha y hora"],
|
||||
};
|
||||
|
||||
export function emptyWords(locale: string, withTime: boolean): string {
|
||||
const words = EMPTY_WORDS[locale.toLowerCase().split("-")[0]] ?? EMPTY_WORDS.en;
|
||||
return words[withTime ? 1 : 0];
|
||||
}
|
||||
|
||||
/** "Sat, 11 Oct 2026" or "Sat, 11 Oct 2026 · 14:00"; "" when not a valid value. */
|
||||
export function describeValue(value: string, withTime: boolean, locale = "en"): string {
|
||||
const [date, time] = value.split("T");
|
||||
if (!isISODate(date)) return "";
|
||||
const day = new Intl.DateTimeFormat(locale, {
|
||||
timeZone: "UTC",
|
||||
weekday: "short",
|
||||
day: "numeric",
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
}).format(new Date(`${date}T00:00:00Z`));
|
||||
return withTime && isWallTime(time) ? `${day} · ${formatTime(time, locale)}` : day;
|
||||
}
|
||||
|
||||
/** An unknown tag from <html lang> must never throw during render. */
|
||||
function safeDescribe(value: string, withTime: boolean, locale: string): string {
|
||||
try {
|
||||
return describeValue(value, withTime, locale);
|
||||
} catch {
|
||||
return describeValue(value, withTime, "en");
|
||||
}
|
||||
}
|
||||
|
||||
export const DateInput = forwardRef<HTMLInputElement, DateInputProps>(function DateInput(
|
||||
{
|
||||
type = "date",
|
||||
value,
|
||||
defaultValue,
|
||||
onChange,
|
||||
placeholder,
|
||||
locale,
|
||||
className,
|
||||
style,
|
||||
onClick,
|
||||
...rest
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const withTime = type === "datetime-local";
|
||||
const [inner, setInner] = useState(defaultValue ?? "");
|
||||
const [lang, setLang] = useState(locale ?? "en");
|
||||
useEffect(() => {
|
||||
if (locale) setLang(locale);
|
||||
else setLang(document.documentElement.lang || "en");
|
||||
}, [locale]);
|
||||
|
||||
const current = value ?? inner;
|
||||
const text = current ? safeDescribe(current, withTime, lang) : "";
|
||||
const empty = !text;
|
||||
|
||||
const handleClick = (event: MouseEvent<HTMLInputElement>) => {
|
||||
onClick?.(event);
|
||||
if (event.defaultPrevented) return;
|
||||
try {
|
||||
event.currentTarget.showPicker?.();
|
||||
} catch {
|
||||
// Not permitted here (e.g. a cross-origin frame): native focus still works.
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<span
|
||||
className={["wk-input", className].filter(Boolean).join(" ")}
|
||||
data-empty={empty || undefined}
|
||||
data-disabled={rest.disabled || undefined}
|
||||
data-invalid={rest["aria-invalid"] === true || rest["aria-invalid"] === "true" || undefined}
|
||||
style={style}
|
||||
>
|
||||
<span className="wk-input-text" aria-hidden="true">
|
||||
{text || placeholder || emptyWords(lang, withTime)}
|
||||
</span>
|
||||
<svg className="wk-input-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
||||
<rect
|
||||
x="3.5"
|
||||
y="5"
|
||||
width="17"
|
||||
height="15"
|
||||
rx="2.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
/>
|
||||
<path
|
||||
d="M3.5 9.5h17M8 3v4M16 3v4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
/>
|
||||
</svg>
|
||||
<input
|
||||
{...rest}
|
||||
ref={ref}
|
||||
type={type}
|
||||
className="wk-input-native"
|
||||
{...(value !== undefined ? { value } : { defaultValue })}
|
||||
onClick={handleClick}
|
||||
onChange={(event) => {
|
||||
setInner(event.target.value);
|
||||
onChange?.(event);
|
||||
}}
|
||||
/>
|
||||
</span>
|
||||
);
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
export { ChipGroup, type ChipGroupProps, type ChipOption } from "./ChipGroup.js";
|
||||
export { DateField, describeFieldValue, type DateFieldProps } from "./DateField.js";
|
||||
export { DateInput, describeValue, emptyWords, type DateInputProps } from "./DateInput.js";
|
||||
export { DayStrip, type DayStripProps } from "./DayStrip.js";
|
||||
export {
|
||||
BookingRequestPicker,
|
||||
|
||||
+57
-18
@@ -26,6 +26,7 @@
|
||||
--wk-accent-soft: color-mix(in srgb, var(--wk-accent) 14%, transparent);
|
||||
--wk-radius: var(--radius-control, 0.625rem);
|
||||
--wk-font: var(--font-sans, system-ui, -apple-system, "Segoe UI", sans-serif);
|
||||
--wk-danger: hsl(var(--status-negative, 0 65% 50%));
|
||||
--wk-text: 1rem;
|
||||
--wk-small: 0.8125rem;
|
||||
--wk-target: 2.75rem;
|
||||
@@ -208,7 +209,7 @@
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
/* ---- the readable date field over the native picker ---- */
|
||||
/* ---- the readable date input over the native picker ---- */
|
||||
|
||||
.wk-field {
|
||||
display: flex;
|
||||
@@ -216,47 +217,80 @@
|
||||
gap: 0.375rem;
|
||||
}
|
||||
|
||||
.wk-field-box {
|
||||
/* The visible box, in two parts.
|
||||
STRUCTURE (unlayered): what the stacked native input needs to work.
|
||||
LOOK (@layer base): border, padding, colour, size. A named low layer, so an
|
||||
app's own classes always win — Tailwind v4 utilities and component classes
|
||||
sit in higher layers, plain app CSS is unlayered — and the date input then
|
||||
looks like the app's other inputs, not like ours. (Unlayered defaults would
|
||||
have beaten Tailwind's layered utilities.) No min-height: a compact input
|
||||
stays compact; the picker sets its own 44px where it needs it. */
|
||||
.wk-input {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
min-height: var(--wk-target);
|
||||
padding: 0 0.875rem;
|
||||
border: 1px solid var(--wk-line-2);
|
||||
border-radius: var(--wk-radius);
|
||||
background: var(--wk-bg);
|
||||
box-sizing: border-box;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.wk-field-box:focus-within {
|
||||
@layer base {
|
||||
.wk-input {
|
||||
width: 100%;
|
||||
padding: 0.7rem 0.875rem;
|
||||
border: 1px solid var(--wk-line-2);
|
||||
border-radius: var(--wk-radius);
|
||||
background: var(--wk-bg);
|
||||
color: inherit;
|
||||
font: inherit;
|
||||
font-size: var(--wk-text);
|
||||
line-height: 1.3;
|
||||
}
|
||||
}
|
||||
|
||||
.wk-field .wk-input,
|
||||
.wk-other .wk-input {
|
||||
min-height: var(--wk-target);
|
||||
}
|
||||
|
||||
.wk-input:focus-within {
|
||||
outline: 2px solid var(--wk-accent);
|
||||
outline-offset: 1px;
|
||||
border-color: var(--wk-accent);
|
||||
}
|
||||
|
||||
.wk-field-text {
|
||||
.wk-input[data-invalid] {
|
||||
border-color: var(--wk-danger, #dc2626);
|
||||
}
|
||||
|
||||
.wk-input[data-disabled] {
|
||||
opacity: 0.55;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.wk-input-text {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
font-size: var(--wk-text);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.wk-field-box[data-empty] .wk-field-text {
|
||||
color: var(--wk-muted);
|
||||
.wk-input[data-empty] .wk-input-text {
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.wk-field-icon {
|
||||
.wk-input-icon {
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
flex: 0 0 auto;
|
||||
color: var(--wk-fg-2);
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
/* The real control: full-size, invisible, on top — taps open the native
|
||||
picker, keyboard entry and autofill keep working. 16px so iOS does not zoom. */
|
||||
.wk-field-input {
|
||||
picker, keyboard entry, autofill and validation keep working. 16px so iOS
|
||||
does not zoom the page on focus. */
|
||||
.wk-input-native {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
@@ -270,6 +304,10 @@
|
||||
color-scheme: light dark;
|
||||
}
|
||||
|
||||
.wk-input-native:disabled {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* "Another date…" under the strip is a quiet secondary control. */
|
||||
.wk-other .wk-label {
|
||||
position: absolute;
|
||||
@@ -279,8 +317,9 @@
|
||||
clip: rect(0 0 0 0);
|
||||
}
|
||||
|
||||
.wk-other .wk-field-box {
|
||||
.wk-other .wk-input {
|
||||
align-self: flex-start;
|
||||
width: auto;
|
||||
border-style: dashed;
|
||||
}
|
||||
|
||||
|
||||
+27
-2
@@ -97,9 +97,34 @@ try {
|
||||
`${name}: flexible request without a day`,
|
||||
);
|
||||
|
||||
// The app's own classes win over whenkit's defaults — plain CSS and a
|
||||
// layered (Tailwind-style) utility alike. Without @layer base for the
|
||||
// defaults, whenkit's unlayered rules would beat the layered utility.
|
||||
const own = await page.evaluate(() => {
|
||||
const css = (sel) => getComputedStyle(document.querySelector(sel));
|
||||
return {
|
||||
plainBorder: css("#own-class .wk-input").borderTopColor,
|
||||
plainWidth: css("#own-class .wk-input").borderTopWidth,
|
||||
utilWidth: css("#util-class .wk-input").borderTopWidth,
|
||||
utilStyle: css("#util-class .wk-input").borderTopStyle,
|
||||
};
|
||||
});
|
||||
check(
|
||||
own.plainBorder === "rgb(255, 0, 0)" && own.plainWidth === "3px",
|
||||
`${name}: an app class beats whenkit's input look (${own.plainBorder} ${own.plainWidth})`,
|
||||
);
|
||||
check(
|
||||
own.utilWidth === "5px" && own.utilStyle === "dashed",
|
||||
`${name}: a layered utility beats it too (${own.utilWidth} ${own.utilStyle})`,
|
||||
);
|
||||
|
||||
const small = await page.evaluate(
|
||||
() =>
|
||||
[...document.querySelectorAll(".wk-chip, .wk-day, .wk-field-box")]
|
||||
[
|
||||
...document.querySelectorAll(
|
||||
".wk-chip, .wk-day, .wk-field .wk-input, .wk-other .wk-input",
|
||||
),
|
||||
]
|
||||
.filter((el) => el.getClientRects().length > 0)
|
||||
.map((el) => el.getBoundingClientRect())
|
||||
.filter((r) => r.height < 43.5 || r.width < 43.5).length,
|
||||
@@ -107,7 +132,7 @@ try {
|
||||
check(small === 0, `${name}: every chip, day and field is at least 44px (${small} too small)`);
|
||||
|
||||
const inputSize = await page.evaluate(() =>
|
||||
parseFloat(getComputedStyle(document.querySelector(".wk-field-input")).fontSize),
|
||||
parseFloat(getComputedStyle(document.querySelector(".wk-input-native")).fontSize),
|
||||
);
|
||||
check(inputSize >= 16, `${name}: date input is ${inputSize}px (iOS zooms below 16)`);
|
||||
|
||||
|
||||
+62
-1
@@ -5,7 +5,7 @@ import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createElement as h } from "react";
|
||||
import { renderToString } from "react-dom/server";
|
||||
import { BookingRequestPicker, DateField, ChipGroup } from "../dist/react/index.js";
|
||||
import { BookingRequestPicker, DateField, ChipGroup, DateInput } from "../dist/react/index.js";
|
||||
|
||||
const NOW = new Date("2026-10-10T08:00:00Z"); // 10:00 in Zurich, a Saturday
|
||||
const base = { zone: "Europe/Zurich", locale: "en-GB", now: NOW, onChange: () => {} };
|
||||
@@ -108,3 +108,64 @@ test("a fixed duration shows no length choice and is used for the slot", () => {
|
||||
// 17:00 + 90 min would end after 18:00 closing, so it is not offered.
|
||||
assert.doesNotMatch(html, />17:00</);
|
||||
});
|
||||
|
||||
test("DateInput is a drop-in: every input prop reaches the real input, className the visible box", () => {
|
||||
const html = renderToString(
|
||||
h(DateInput, {
|
||||
id: "due",
|
||||
name: "due",
|
||||
required: true,
|
||||
min: "2026-10-01",
|
||||
disabled: true,
|
||||
"aria-invalid": true,
|
||||
className: "ui-input-compact",
|
||||
value: "2026-10-11",
|
||||
onChange: () => {},
|
||||
locale: "en-GB",
|
||||
}),
|
||||
);
|
||||
assert.match(html, /<span class="wk-input ui-input-compact"/);
|
||||
assert.match(html, /<input[^>]*id="due"/);
|
||||
assert.match(html, /<input[^>]*name="due"/);
|
||||
assert.match(html, /<input[^>]*required/);
|
||||
assert.match(html, /<input[^>]*min="2026-10-01"/);
|
||||
assert.match(html, /<input[^>]*disabled/);
|
||||
assert.match(html, /<input[^>]*aria-invalid="true"/);
|
||||
assert.match(html, /<input[^>]*type="date"/);
|
||||
assert.match(html, /Sun, 11 Oct 2026/);
|
||||
assert.match(html, /data-invalid/);
|
||||
});
|
||||
|
||||
test("DateInput works uncontrolled (a server-action form): defaultValue shows and submits", () => {
|
||||
const html = renderToString(
|
||||
h(DateInput, { name: "date", defaultValue: "2026-03-05", locale: "de" }),
|
||||
);
|
||||
assert.match(html, /Do\., 5\. März 2026|Do, 5\. März 2026/);
|
||||
assert.match(html, /<input[^>]*value="2026-03-05"/);
|
||||
assert.match(html, /<input[^>]*name="date"/);
|
||||
});
|
||||
|
||||
test("DateInput datetime-local shows the time, and an empty one shows words in the page language", () => {
|
||||
const withTime = renderToString(
|
||||
h(DateInput, {
|
||||
type: "datetime-local",
|
||||
value: "2026-10-11T14:30",
|
||||
onChange: () => {},
|
||||
locale: "en-GB",
|
||||
}),
|
||||
);
|
||||
assert.match(withTime, /Sun, 11 Oct 2026 · 14:30/);
|
||||
const empty = renderToString(h(DateInput, { value: "", onChange: () => {}, locale: "de" }));
|
||||
assert.match(empty, /Datum wählen/);
|
||||
assert.match(empty, /data-empty/);
|
||||
const unknown = renderToString(h(DateInput, { value: "", onChange: () => {}, locale: "xx" }));
|
||||
assert.match(unknown, /Pick a date/);
|
||||
});
|
||||
|
||||
test("DateInput never throws on a bad value or a bad locale", () => {
|
||||
assert.doesNotThrow(() =>
|
||||
renderToString(
|
||||
h(DateInput, { value: "not-a-date", onChange: () => {}, locale: "zz-INVALID-tag-!!" }),
|
||||
),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user