@thekestongroup/ui 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/README.md +98 -31
  2. package/dist/components/Age/Age.vue.d.ts +19 -0
  3. package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
  4. package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
  5. package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
  6. package/dist/components/Badge/Badge.vue.d.ts +41 -0
  7. package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
  8. package/dist/components/Button/Button.vue.d.ts +69 -0
  9. package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
  10. package/dist/components/Card/Card.vue.d.ts +56 -0
  11. package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
  12. package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
  13. package/dist/components/Chip/Chip.vue.d.ts +44 -0
  14. package/dist/components/Divider/Divider.vue.d.ts +36 -0
  15. package/dist/components/EmptyState/EmptyState.vue.d.ts +55 -0
  16. package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
  17. package/dist/components/FormField/FormField.vue.d.ts +61 -0
  18. package/dist/components/Header/Header.vue.d.ts +104 -0
  19. package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
  20. package/dist/components/Input/Input.vue.d.ts +56 -0
  21. package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
  22. package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
  23. package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
  24. package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
  25. package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
  26. package/dist/components/Link/Link.vue.d.ts +39 -0
  27. package/dist/components/Notification/Notification.vue.d.ts +62 -0
  28. package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
  29. package/dist/components/Notification/Toaster.vue.d.ts +51 -0
  30. package/dist/components/Notification/notification.samples.d.ts +3 -0
  31. package/dist/components/Notification/notifications.d.ts +115 -0
  32. package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
  33. package/dist/components/Popover/Popover.vue.d.ts +83 -0
  34. package/dist/components/Popover/popover.d.ts +16 -0
  35. package/dist/components/Progress/Progress.vue.d.ts +26 -0
  36. package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
  37. package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
  38. package/dist/components/Select/Select.vue.d.ts +110 -0
  39. package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
  40. package/dist/components/Sidebar/sidebar.d.ts +10 -0
  41. package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
  42. package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
  43. package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
  44. package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
  45. package/dist/components/Switch/Switch.vue.d.ts +39 -0
  46. package/dist/components/Table/Table.vue.d.ts +82 -0
  47. package/dist/components/Table/table.d.ts +36 -0
  48. package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
  49. package/dist/index.d.ts +91 -25
  50. package/dist/keston-ui.js +5030 -14786
  51. package/dist/lib/cn.d.ts +4 -0
  52. package/dist/lib/dates.d.ts +27 -0
  53. package/dist/lib/initials.d.ts +2 -0
  54. package/dist/lib/parseDate.d.ts +44 -0
  55. package/dist/lib/useNow.d.ts +3 -0
  56. package/package.json +57 -24
  57. package/src/components/.gitkeep +0 -0
  58. package/src/components/Age/Age.stories.ts +24 -0
  59. package/src/components/Age/Age.vue +56 -0
  60. package/src/components/AppShell/AppShell.stories.ts +124 -0
  61. package/src/components/AppShell/AppShell.vue +221 -0
  62. package/src/components/Avatar/Avatar.stories.ts +56 -0
  63. package/src/components/Avatar/Avatar.vue +118 -0
  64. package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
  65. package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
  66. package/src/components/Badge/Badge.stories.ts +92 -0
  67. package/src/components/Badge/Badge.vue +67 -0
  68. package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
  69. package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
  70. package/src/components/Button/Button.mdx +79 -0
  71. package/src/components/Button/Button.stories.ts +207 -0
  72. package/src/components/Button/Button.vue +266 -0
  73. package/src/components/Calendar/Calendar.stories.ts +51 -0
  74. package/src/components/Calendar/Calendar.vue +364 -0
  75. package/src/components/Card/Card.stories.ts +109 -0
  76. package/src/components/Card/Card.vue +115 -0
  77. package/src/components/Checkbox/Checkbox.stories.ts +102 -0
  78. package/src/components/Checkbox/Checkbox.vue +102 -0
  79. package/src/components/Checklist/Checklist.stories.ts +67 -0
  80. package/src/components/Checklist/Checklist.vue +38 -0
  81. package/src/components/Chip/Chip.stories.ts +71 -0
  82. package/src/components/Chip/Chip.vue +86 -0
  83. package/src/components/Divider/Divider.stories.ts +57 -0
  84. package/src/components/Divider/Divider.vue +58 -0
  85. package/src/components/EmptyState/EmptyState.stories.ts +99 -0
  86. package/src/components/EmptyState/EmptyState.vue +87 -0
  87. package/src/components/FormField/FieldMessage.vue +81 -0
  88. package/src/components/FormField/FormField.stories.ts +55 -0
  89. package/src/components/FormField/FormField.vue +160 -0
  90. package/src/components/Header/Header.stories.ts +168 -0
  91. package/src/components/Header/Header.vue +232 -0
  92. package/src/components/InfoTip/InfoTip.stories.ts +63 -0
  93. package/src/components/InfoTip/InfoTip.vue +50 -0
  94. package/src/components/Input/Input.stories.ts +206 -0
  95. package/src/components/Input/Input.vue +90 -0
  96. package/src/components/InputCode/InputCode.stories.ts +85 -0
  97. package/src/components/InputCode/InputCode.vue +145 -0
  98. package/src/components/InputDate/InputDate.stories.ts +58 -0
  99. package/src/components/InputDate/InputDate.vue +258 -0
  100. package/src/components/InputNumber/InputNumber.stories.ts +63 -0
  101. package/src/components/InputNumber/InputNumber.vue +109 -0
  102. package/src/components/InputPassword/InputPassword.stories.ts +105 -0
  103. package/src/components/InputPassword/InputPassword.vue +132 -0
  104. package/src/components/InputRichText/InputRichText.stories.ts +100 -0
  105. package/src/components/InputRichText/InputRichText.vue +194 -0
  106. package/src/components/Link/Link.stories.ts +73 -0
  107. package/src/components/Link/Link.vue +56 -0
  108. package/src/components/Notification/Notification.stories.ts +196 -0
  109. package/src/components/Notification/Notification.vue +136 -0
  110. package/src/components/Notification/NotificationPane.vue +186 -0
  111. package/src/components/Notification/Toaster.vue +446 -0
  112. package/src/components/Notification/notification.samples.ts +45 -0
  113. package/src/components/Notification/notifications.test.ts +99 -0
  114. package/src/components/Notification/notifications.ts +172 -0
  115. package/src/components/PageHeader/PageHeader.stories.ts +79 -0
  116. package/src/components/PageHeader/PageHeader.vue +66 -0
  117. package/src/components/Popover/Popover.stories.ts +135 -0
  118. package/src/components/Popover/Popover.vue +192 -0
  119. package/src/components/Popover/popover.ts +20 -0
  120. package/src/components/Progress/Progress.stories.ts +74 -0
  121. package/src/components/Progress/Progress.vue +75 -0
  122. package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
  123. package/src/components/RadioGroup/RadioGroup.vue +132 -0
  124. package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
  125. package/src/components/RelativeTime/RelativeTime.vue +51 -0
  126. package/src/components/Select/Select.mdx +202 -0
  127. package/src/components/Select/Select.stories.ts +223 -0
  128. package/src/components/Select/Select.vue +630 -0
  129. package/src/components/Sidebar/Sidebar.stories.ts +192 -0
  130. package/src/components/Sidebar/Sidebar.vue +382 -0
  131. package/src/components/Sidebar/sidebar.ts +20 -0
  132. package/src/components/Slideover/Slideover.stories.ts +225 -0
  133. package/src/components/Slideover/Slideover.vue +365 -0
  134. package/src/components/Spinner/Spinner.stories.ts +79 -0
  135. package/src/components/Spinner/Spinner.vue +97 -0
  136. package/src/components/StatTile/StatTile.stories.ts +75 -0
  137. package/src/components/StatTile/StatTile.vue +89 -0
  138. package/src/components/SummaryList/SummaryList.stories.ts +103 -0
  139. package/src/components/SummaryList/SummaryList.vue +127 -0
  140. package/src/components/Switch/Switch.stories.ts +100 -0
  141. package/src/components/Switch/Switch.vue +96 -0
  142. package/src/components/Table/Table.stories.ts +212 -0
  143. package/src/components/Table/Table.vue +223 -0
  144. package/src/components/Table/table.test.ts +65 -0
  145. package/src/components/Table/table.ts +70 -0
  146. package/src/components/Tooltip/Tooltip.stories.ts +57 -0
  147. package/src/components/Tooltip/Tooltip.vue +86 -0
  148. package/src/examples/AppPages.stories.ts +500 -0
  149. package/src/examples/FormLayout.stories.ts +185 -0
  150. package/src/examples/KestonIdPages.stories.ts +415 -0
  151. package/src/examples/SchoolPages.stories.ts +665 -0
  152. package/src/index.ts +116 -36
  153. package/src/lib/cn.ts +4 -0
  154. package/src/lib/dates.test.ts +68 -0
  155. package/src/lib/dates.ts +86 -0
  156. package/src/lib/initials.ts +8 -0
  157. package/src/lib/parseDate.test.ts +331 -0
  158. package/src/lib/parseDate.ts +561 -0
  159. package/src/lib/useNow.ts +20 -0
  160. package/src/styles/theme.css +101 -0
  161. package/dist/components/AppLauncher.vue.d.ts +0 -10
  162. package/dist/components/Avatar.vue.d.ts +0 -16
  163. package/dist/components/Button.vue.d.ts +0 -27
  164. package/dist/components/Checkbox.vue.d.ts +0 -18
  165. package/dist/components/CloseButton.vue.d.ts +0 -6
  166. package/dist/components/Dialog.vue.d.ts +0 -23
  167. package/dist/components/Drawer.vue.d.ts +0 -34
  168. package/dist/components/DropdownMenu.vue.d.ts +0 -21
  169. package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
  170. package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
  171. package/dist/components/Input.vue.d.ts +0 -14
  172. package/dist/components/Label.vue.d.ts +0 -16
  173. package/dist/components/NotificationsMenu.vue.d.ts +0 -3
  174. package/dist/components/Popover.vue.d.ts +0 -23
  175. package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
  176. package/dist/components/Select.vue.d.ts +0 -28
  177. package/dist/components/Separator.vue.d.ts +0 -11
  178. package/dist/components/ToastRenderer.vue.d.ts +0 -3
  179. package/dist/components/Toaster.vue.d.ts +0 -3
  180. package/dist/components/Tooltip.vue.d.ts +0 -23
  181. package/dist/components/TopBar.vue.d.ts +0 -34
  182. package/dist/favicon-old.svg +0 -1
  183. package/dist/favicon.svg +0 -1
  184. package/dist/keston-ui.cjs +0 -15
  185. package/dist/lib/toast.d.ts +0 -29
  186. package/dist/lib/types.d.ts +0 -5
  187. package/src/components/AppLauncher.vue +0 -63
  188. package/src/components/Avatar.vue +0 -51
  189. package/src/components/Button.vue +0 -54
  190. package/src/components/Checkbox.vue +0 -47
  191. package/src/components/CloseButton.vue +0 -24
  192. package/src/components/Dialog.vue +0 -65
  193. package/src/components/Drawer.vue +0 -202
  194. package/src/components/DropdownMenu.vue +0 -41
  195. package/src/components/DropdownMenuItem.vue +0 -28
  196. package/src/components/DropdownMenuSeparator.vue +0 -12
  197. package/src/components/Input.vue +0 -21
  198. package/src/components/Label.vue +0 -15
  199. package/src/components/NotificationsMenu.vue +0 -115
  200. package/src/components/Popover.vue +0 -46
  201. package/src/components/ProfileSwitcher.vue +0 -113
  202. package/src/components/Select.vue +0 -97
  203. package/src/components/Separator.vue +0 -30
  204. package/src/components/ToastItem.vue +0 -158
  205. package/src/components/ToastRenderer.vue +0 -113
  206. package/src/components/Toaster.vue +0 -11
  207. package/src/components/Tooltip.vue +0 -48
  208. package/src/components/TopBar.vue +0 -52
  209. package/src/lib/toast.ts +0 -95
  210. package/src/lib/types.ts +0 -5
@@ -0,0 +1,85 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { ref } from "vue";
3
+ import InputCode, { type InputCodeProps } from "./InputCode.vue";
4
+
5
+ const meta = {
6
+ title: "Components/Form/InputCode",
7
+ component: InputCode,
8
+ tags: ["autodocs"],
9
+ argTypes: {
10
+ length: { control: { type: "number", min: 4, max: 8 } },
11
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
12
+ status: { options: [undefined, "error", "success", "loading"], control: { type: "inline-radio" } },
13
+ },
14
+ args: {
15
+ label: "Code from your authenticator app",
16
+ helper: "",
17
+ helperPlacement: "below",
18
+ length: 6,
19
+ alphanumeric: false,
20
+ disabled: false,
21
+ },
22
+ render: (args) => ({
23
+ components: { InputCode },
24
+ setup: () => ({ args, value: ref("") }),
25
+ template: `<InputCode v-bind="args" v-model="value" />`,
26
+ }),
27
+ } satisfies Meta<InputCodeProps>;
28
+
29
+ export default meta;
30
+ type Story = StoryObj<typeof meta>;
31
+
32
+ /** Playground: type a code, or paste one (e.g. 042317) to fill every box. */
33
+ export const Default: Story = {};
34
+
35
+ export const EmailCode: Story = {
36
+ args: {
37
+ label: "Code from your email",
38
+ helper: "We sent a 6-digit code to b•••@keston.school. It expires in 10 minutes.",
39
+ },
40
+ };
41
+
42
+ /** Letters and digits, upper-cased as they're typed, e.g. a backup code. */
43
+ export const Alphanumeric: Story = {
44
+ args: { label: "Backup code", length: 8, alphanumeric: true },
45
+ };
46
+
47
+ export const WithErrorText: Story = {
48
+ args: { errorText: "That code didn't work. Check it and try again." },
49
+ };
50
+
51
+ export const Disabled: Story = { args: { disabled: true } };
52
+
53
+ /**
54
+ * Checked as soon as the last box is filled, using `@complete`. Try 123456
55
+ * for a code that works; anything else fails.
56
+ */
57
+ export const CheckOnComplete: Story = {
58
+ render: (args) => ({
59
+ components: { InputCode },
60
+ setup() {
61
+ const value = ref("");
62
+ const status = ref<InputCodeProps["status"]>();
63
+ const statusText = ref<string>();
64
+ async function check(code: string) {
65
+ status.value = "loading";
66
+ statusText.value = "Checking code…";
67
+ await new Promise((resolve) => setTimeout(resolve, 900));
68
+ const ok = code === "123456";
69
+ status.value = ok ? "success" : "error";
70
+ statusText.value = ok ? "Code accepted" : "That code didn't work. Check it and try again.";
71
+ if (!ok) value.value = "";
72
+ }
73
+ return { args, value, status, statusText, check };
74
+ },
75
+ template: `
76
+ <InputCode
77
+ v-bind="args"
78
+ v-model="value"
79
+ :status="status"
80
+ :status-text="statusText"
81
+ :disabled="status === 'loading'"
82
+ @complete="check"
83
+ />`,
84
+ }),
85
+ };
@@ -0,0 +1,145 @@
1
+ <script setup lang="ts">
2
+ import { computed, useId, type HTMLAttributes } from 'vue'
3
+ import { PinInputInput, PinInputRoot } from 'reka-ui'
4
+ import { cn } from '../../lib/cn'
5
+ import InfoTip from '../InfoTip/InfoTip.vue'
6
+ import FieldMessage, { type FieldStatus } from '../FormField/FieldMessage.vue'
7
+
8
+ /**
9
+ * A one-time code, e.g. from an authenticator app, an email or a text
10
+ * message: one box per character. Typing moves to the next box, Backspace to
11
+ * the one before, and pasting (or the code suggested by the phone's keyboard)
12
+ * fills every box at once. Built on Reka UI's PinInput.
13
+ */
14
+
15
+ export type InputCodeStatus = FieldStatus
16
+
17
+ export interface InputCodeProps {
18
+ /** Visible label, e.g. "Code from your authenticator app". */
19
+ label: string
20
+ /** Extra guidance, e.g. where the code was sent. */
21
+ helper?: string
22
+ /** Show helper text under the label, or behind a `?` icon next to it. */
23
+ helperPlacement?: 'below' | 'icon'
24
+ /** Number of characters. */
25
+ length?: number
26
+ /** Allow letters as well as digits. Codes are digits only by default. */
27
+ alphanumeric?: boolean
28
+ /**
29
+ * Error message, e.g. "That code didn't work". When set (and not empty)
30
+ * every box shows as an error with this text. Overrides `status` and
31
+ * `statusText`.
32
+ */
33
+ errorText?: string
34
+ /** Colours the boxes and the status text; `loading` while the code is checked. */
35
+ status?: InputCodeStatus
36
+ /** Message under the boxes, e.g. "Checking code…". */
37
+ statusText?: string
38
+ /** Form field name, so the code is sent with a native form. */
39
+ name?: string
40
+ disabled?: boolean
41
+ class?: HTMLAttributes['class']
42
+ }
43
+
44
+ const props = withDefaults(defineProps<InputCodeProps>(), {
45
+ helperPlacement: 'below',
46
+ length: 6
47
+ })
48
+
49
+ /** The code as one string, e.g. "042317". Shorter while it's being typed. */
50
+ const model = defineModel<string>({ default: '' })
51
+
52
+ const emit = defineEmits<{
53
+ /** Every box is filled: a good moment to submit the code. */
54
+ complete: [code: string]
55
+ }>()
56
+
57
+ const id = useId()
58
+ const labelId = `${id}-label`
59
+ const helperId = `${id}-helper`
60
+ const statusId = `${id}-status`
61
+
62
+ const currentStatus = computed<InputCodeStatus | undefined>(() => (props.errorText ? 'error' : props.status))
63
+ const currentStatusText = computed(() => props.errorText || props.statusText)
64
+ const describedBy = computed(
65
+ () => [props.helper && helperId, currentStatusText.value && statusId].filter(Boolean).join(' ') || undefined
66
+ )
67
+
68
+ // Reka keeps one entry per box (numbers in digit mode); the app gets a plain
69
+ // string. Letters are upper-cased, as codes are shown that way and aren't
70
+ // case sensitive.
71
+ type Box = string | number | undefined
72
+ const boxes = computed<Box[]>(() =>
73
+ Array.from({ length: props.length }, (_, i) => {
74
+ const char = model.value[i]
75
+ if (props.alphanumeric) return char ?? ''
76
+ return char === undefined ? undefined : Number(char)
77
+ })
78
+ )
79
+ function toCode(value: Box[]) {
80
+ return value
81
+ .map((char) => (char == null ? '' : String(char)))
82
+ .join('')
83
+ .replace(props.alphanumeric ? /[^a-z0-9]/gi : /\D/g, '')
84
+ .toUpperCase()
85
+ .slice(0, props.length)
86
+ }
87
+ const onUpdate = (value: Box[]) => (model.value = toCode(value))
88
+ const onComplete = (value: Box[]) => emit('complete', toCode(value))
89
+
90
+ // Each box looks like FormField's box (a medium basic Button), with the
91
+ // focus ring on the box being typed in.
92
+ const box = [
93
+ 'size-10 shrink-0 rounded border border-black/15 bg-white text-center text-lg tabular-nums text-black transition',
94
+ 'outline-2 outline-offset-1 outline-transparent focus:outline-focus forced-colors:not-focus:outline-0',
95
+ 'placeholder:text-black/25 disabled:cursor-not-allowed'
96
+ ]
97
+ const statusBorder: Record<InputCodeStatus, string> = {
98
+ error: 'border-red',
99
+ success: 'border-green',
100
+ loading: ''
101
+ }
102
+ </script>
103
+
104
+ <template>
105
+ <div :class="cn('flex flex-col gap-1.5 text-black', props.class)">
106
+ <div class="flex flex-col gap-0.5">
107
+ <div class="flex items-center gap-1">
108
+ <label :id="labelId" :for="`${id}-0`" class="text-sm">{{ label }}</label>
109
+ <InfoTip v-if="helper && helperPlacement === 'icon'" :text="helper" :label="`About ${label}`" />
110
+ </div>
111
+ <!-- Behind the ?: hidden, but still the boxes' description. -->
112
+ <p v-if="helper" :id="helperId" :hidden="helperPlacement === 'icon'" class="text-xs text-black/65">
113
+ {{ helper }}
114
+ </p>
115
+ </div>
116
+
117
+ <PinInputRoot
118
+ :model-value="boxes as any"
119
+ :type="alphanumeric ? 'text' : 'number'"
120
+ :name="name"
121
+ :disabled="disabled"
122
+ otp
123
+ placeholder="·"
124
+ role="group"
125
+ :aria-labelledby="labelId"
126
+ :class="cn('flex gap-2', disabled && 'opacity-40')"
127
+ @update:model-value="onUpdate"
128
+ @complete="onComplete"
129
+ >
130
+ <PinInputInput
131
+ v-for="(_, i) in length"
132
+ :id="`${id}-${i}`"
133
+ :key="i"
134
+ :index="i"
135
+ :aria-label="i === 0 ? undefined : `Character ${i + 1} of ${length}`"
136
+ :aria-labelledby="i === 0 ? labelId : undefined"
137
+ :aria-describedby="describedBy"
138
+ :aria-invalid="currentStatus === 'error' || undefined"
139
+ :class="cn(box, currentStatus && statusBorder[currentStatus])"
140
+ />
141
+ </PinInputRoot>
142
+
143
+ <FieldMessage :id="statusId" :status="currentStatus" :text="currentStatusText" />
144
+ </div>
145
+ </template>
@@ -0,0 +1,58 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { ref } from "vue";
3
+ import InputDate from "./InputDate.vue";
4
+
5
+ const meta = {
6
+ title: "Components/Form/InputDate",
7
+ component: InputDate,
8
+ tags: ["autodocs"],
9
+ argTypes: {
10
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
11
+ status: { options: [undefined, "error", "success"], control: { type: "inline-radio" } },
12
+ },
13
+ args: {
14
+ label: "Date of birth",
15
+ helperPlacement: "below",
16
+ locale: "en-GB",
17
+ disabled: false,
18
+ },
19
+ decorators: [() => ({ template: '<div class="max-w-60 min-h-95"><story /></div>' })],
20
+ render: (args) => ({
21
+ components: { InputDate },
22
+ setup() {
23
+ const value = ref<string | undefined>();
24
+ return { args, value };
25
+ },
26
+ template: `
27
+ <InputDate v-bind="args" v-model="value" />
28
+ <small class="mt-3 block text-xs text-black/60">
29
+ Value: {{ value ?? "undefined" }}
30
+ </small>`,
31
+ }),
32
+ } satisfies Meta<typeof InputDate>;
33
+
34
+ export default meta;
35
+ type Story = StoryObj<typeof meta>;
36
+
37
+ /** Playground: type a date or use the calendar button. */
38
+ export const Default: Story = {};
39
+
40
+ /** Only dates in the 2026–27 school year can be picked. */
41
+ export const MinAndMax: Story = {
42
+ args: {
43
+ label: "Absence date",
44
+ min: "2026-09-01",
45
+ max: "2027-07-23",
46
+ helper: "Within the 2026–27 school year.",
47
+ },
48
+ };
49
+
50
+ export const USOrder: Story = {
51
+ args: { label: "Date (US order)", locale: "en-US" },
52
+ };
53
+
54
+ export const WithError: Story = {
55
+ args: { label: "Start date", errorText: "Start date must be after today" },
56
+ };
57
+
58
+ export const Disabled: Story = { args: { disabled: true } };
@@ -0,0 +1,258 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, watch, type HTMLAttributes } from 'vue'
3
+ import { DatePickerContent, DatePickerRoot, DatePickerTrigger } from 'reka-ui'
4
+ import { getLocalTimeZone, parseDate, today, type DateValue } from '@internationalized/date'
5
+ import { CalendarDays } from '@lucide/vue'
6
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
7
+ import Calendar from '../Calendar/Calendar.vue'
8
+ import { popoverContent } from '../Popover/popover'
9
+ import { parseDateText, type DatePart, type DatePreference } from '../../lib/parseDate'
10
+
11
+ // Attributes like name and placeholder go to the text <input>, not the wrapper.
12
+ defineOptions({ inheritAttrs: false })
13
+
14
+ export interface InputDateProps {
15
+ /** Visible label. Always set it: it is also the field's accessible name. */
16
+ label: string
17
+ /** Extra guidance about what to enter. */
18
+ helper?: string
19
+ /** Show helper text under the label, or behind a `?` icon next to it. */
20
+ helperPlacement?: 'below' | 'icon'
21
+ /** Error message, e.g. from a Zod schema. Overrides `status` / `statusText`. */
22
+ errorText?: string
23
+ /** Colours the field and the status text. */
24
+ status?: FormFieldStatus
25
+ /** Message under the field. */
26
+ statusText?: string
27
+ /** Earliest allowed date, as `YYYY-MM-DD`. */
28
+ min?: string
29
+ /** Latest allowed date, as `YYYY-MM-DD`. */
30
+ max?: string
31
+ /** Sets the order of day / month / year and the calendar's language. */
32
+ locale?: string
33
+ /**
34
+ * How typed dates that don't say which year, month or week are read
35
+ * ("5th march", "the 5th", "tuesday", "christmas"): 'future' (default)
36
+ * picks the next one, 'past' the last one, 'nearest' the closest. Use
37
+ * 'past' for things that already happened, e.g. an incident date.
38
+ */
39
+ prefer?: DatePreference
40
+ disabled?: boolean
41
+ class?: HTMLAttributes['class']
42
+ }
43
+
44
+ const props = withDefaults(defineProps<InputDateProps>(), {
45
+ helperPlacement: 'below',
46
+ locale: 'en-GB',
47
+ prefer: 'future'
48
+ })
49
+
50
+ /** The date as `YYYY-MM-DD`, or undefined when empty or incomplete. */
51
+ const model = defineModel<string | undefined>()
52
+
53
+ // Reka works with date objects; the component's value is a plain ISO string,
54
+ // which is easy to validate (e.g. with Zod) and send to a server.
55
+ function toDate(iso: string | undefined): DateValue | undefined {
56
+ if (!iso) return undefined
57
+ try {
58
+ return parseDate(iso)
59
+ } catch {
60
+ return undefined
61
+ }
62
+ }
63
+
64
+ const dateValue = computed(() => toDate(model.value))
65
+ const minValue = computed(() => toDate(props.min))
66
+ const maxValue = computed(() => toDate(props.max))
67
+
68
+ // ── Range check ─────────────────────────────────────────────────────────────
69
+ // A typed date outside min / max is still passed to the model (so the form
70
+ // can see it), but the field explains what is allowed. errorText and
71
+ // status / statusText override this.
72
+ function formatDate(date: DateValue) {
73
+ return new Intl.DateTimeFormat(props.locale, { dateStyle: 'long' }).format(
74
+ new Date(date.year, date.month - 1, date.day)
75
+ )
76
+ }
77
+
78
+ const rangeError = computed(() => {
79
+ const value = dateValue.value
80
+ if (!value) return undefined
81
+ const min = minValue.value
82
+ const max = maxValue.value
83
+ const tooEarly = min && value.compare(min) < 0
84
+ const tooLate = max && value.compare(max) > 0
85
+ if (!tooEarly && !tooLate) return undefined
86
+ if (min && max) return `Choose a date from ${formatDate(min)} to ${formatDate(max)}`
87
+ if (min) return `Choose a date on or after ${formatDate(min)}`
88
+ return `Choose a date on or before ${formatDate(max!)}`
89
+ })
90
+
91
+ // ── Typed text ──────────────────────────────────────────────────────────────
92
+ // The field is a plain text box. A date typed in almost any form ("26/01/2009",
93
+ // "5th of december", "tmrw", "monday week" …, see src/lib/parseDate.ts) sets
94
+ // the value straight away; on leaving the field it is tidied to 26/01/2009.
95
+ // Anything else leaves the value undefined and, once the user leaves the
96
+ // field, shows an example.
97
+ const shortFormat = computed(
98
+ () => new Intl.DateTimeFormat(props.locale, { day: '2-digit', month: '2-digit', year: 'numeric' })
99
+ )
100
+ // Day / month / year order and separator for the locale, e.g. en-GB → d/m/y.
101
+ const sample = computed(() => shortFormat.value.formatToParts(new Date(2009, 0, 26)))
102
+ const order = computed(
103
+ () => sample.value.filter((part) => part.type === 'day' || part.type === 'month' || part.type === 'year').map((part) => part.type) as DatePart[]
104
+ )
105
+ const separator = computed(() => sample.value.find((part) => part.type === 'literal')?.value ?? '/')
106
+ const placeholderText = computed(() =>
107
+ order.value.map((part) => ({ day: 'dd', month: 'mm', year: 'yyyy' })[part]).join(separator.value)
108
+ )
109
+ const example = computed(() => shortFormat.value.format(new Date(2009, 0, 26)))
110
+
111
+ function formatShort(date: DateValue) {
112
+ return shortFormat.value.format(new Date(date.year, date.month - 1, date.day))
113
+ }
114
+
115
+ // The words and formats understood are listed in src/lib/parseDate.ts.
116
+ function parseText(input: string) {
117
+ return parseDateText(input, {
118
+ today: today(getLocalTimeZone()),
119
+ order: order.value,
120
+ prefer: props.prefer
121
+ })
122
+ }
123
+
124
+ const text = ref(dateValue.value ? formatShort(dateValue.value) : '')
125
+
126
+ // While words are typed ("tmrw", "26 jan 2009"), show the date they mean
127
+ // inside the field. Purely numeric text doesn't need it.
128
+ const phrasePreview = computed(() => {
129
+ if (!/[a-z]/i.test(text.value)) return undefined
130
+ const date = parseText(text.value)
131
+ return date ? formatShort(date) : undefined
132
+ })
133
+ const formatError = ref<string>()
134
+ // The last value this text box set, so its own updates don't rewrite the text
135
+ // mid-typing; changes from elsewhere (calendar, parent) do update it.
136
+ let lastFromText: string | undefined = model.value
137
+
138
+ watch(model, (value) => {
139
+ if (value === lastFromText) return
140
+ lastFromText = value
141
+ const date = toDate(value)
142
+ text.value = date ? formatShort(date) : ''
143
+ formatError.value = undefined
144
+ })
145
+
146
+ function onTextInput() {
147
+ const date = parseText(text.value)
148
+ const value = date?.toString()
149
+ lastFromText = value
150
+ model.value = value
151
+ // Once an error is showing, clear it as soon as the text becomes valid.
152
+ if (formatError.value && (date || !text.value.trim())) formatError.value = undefined
153
+ }
154
+
155
+ function onTextBlur() {
156
+ if (!text.value.trim()) {
157
+ formatError.value = undefined
158
+ return
159
+ }
160
+ const date = parseText(text.value)
161
+ if (date) {
162
+ text.value = formatShort(date)
163
+ formatError.value = undefined
164
+ } else {
165
+ formatError.value = `Enter a date like ${example.value}`
166
+ }
167
+ }
168
+
169
+ // Enter first settles the text, like leaving the field: "tmrw" or "26/1"
170
+ // becomes the full date it means, and the form isn't submitted. Once the text
171
+ // already shows that date (or is empty), Enter does its usual job, e.g.
172
+ // submitting the form. Text that isn't a date shows the example and stays.
173
+ function onTextEnter(event: KeyboardEvent) {
174
+ if (event.isComposing || !text.value.trim()) return
175
+ const date = parseText(text.value)
176
+ if (date && text.value === formatShort(date)) return
177
+ event.preventDefault()
178
+ onTextBlur()
179
+ }
180
+
181
+ // errorText is handled by FormField; then status / statusText, the format check
182
+ // and the range check.
183
+ const fieldError = computed(() => formatError.value ?? rangeError.value)
184
+ const status = computed(() => props.status ?? (fieldError.value ? 'error' : undefined))
185
+ const statusText = computed(() => props.statusText ?? fieldError.value)
186
+
187
+ // ── Calendar ────────────────────────────────────────────────────────────────
188
+ // Opens on the chosen date (or today, or the nearest allowed date); choosing
189
+ // a day closes it. Reka's DatePicker is used only for the popover, so the
190
+ // chosen day (or today) gets focus when it opens.
191
+ const open = ref(false)
192
+
193
+ function onCalendarPick(value: string | undefined) {
194
+ model.value = value
195
+ open.value = false
196
+ }
197
+
198
+ // Calendar button: a full-height square segment at the right of the box, like
199
+ // InputNumber's + button. Negative margins cancel the box's padding.
200
+ const trigger = [
201
+ 'flex w-9 shrink-0 -my-2 -mr-3 self-stretch items-center justify-center',
202
+ 'rounded-r border-l border-black/15 bg-white cursor-pointer transition',
203
+ 'not-disabled:hover:brightness-90 not-disabled:active:brightness-80 [&_svg]:opacity-100!',
204
+ 'data-[state=open]:brightness-90 disabled:cursor-not-allowed',
205
+ 'outline-2 -outline-offset-2 outline-transparent focus-visible:outline-focus'
206
+ ]
207
+ </script>
208
+
209
+ <template>
210
+ <FormField
211
+ :label="label"
212
+ :helper="helper"
213
+ :helper-placement="helperPlacement"
214
+ :error-text="errorText"
215
+ :status="status"
216
+ :status-text="statusText"
217
+ :disabled="disabled"
218
+ :class="props.class"
219
+ >
220
+ <template #default="{ id, describedBy, invalid }">
221
+ <DatePickerRoot v-model:open="open" :disabled="disabled">
222
+ <!-- Plain text box: type or paste the whole date, e.g. 26/01/2009. -->
223
+ <input
224
+ :id="id"
225
+ v-model="text"
226
+ type="text"
227
+ autocomplete="off"
228
+ :placeholder="placeholderText"
229
+ v-bind="$attrs"
230
+ :disabled="disabled"
231
+ :aria-invalid="invalid"
232
+ :aria-describedby="describedBy"
233
+ class="h-5 min-w-0 flex-1 bg-transparent tabular-nums outline-none placeholder:text-black/55 disabled:cursor-not-allowed"
234
+ @input="onTextInput"
235
+ @blur="onTextBlur"
236
+ @keydown.enter="onTextEnter"
237
+ />
238
+
239
+ <!-- The date a typed phrase means, e.g. "tmrw" → 11/11/2026. -->
240
+ <span v-if="phrasePreview" aria-hidden="true" class="shrink-0 tabular-nums text-black/55">
241
+ {{ phrasePreview }}
242
+ </span>
243
+
244
+ <DatePickerTrigger aria-label="Open calendar" :class="trigger">
245
+ <CalendarDays />
246
+ </DatePickerTrigger>
247
+
248
+ <DatePickerContent
249
+ :side-offset="6"
250
+ align="end"
251
+ :class="[popoverContent, 'p-3']"
252
+ >
253
+ <Calendar :model-value="model" :min="min" :max="max" :locale="locale" @update:model-value="onCalendarPick" />
254
+ </DatePickerContent>
255
+ </DatePickerRoot>
256
+ </template>
257
+ </FormField>
258
+ </template>
@@ -0,0 +1,63 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { ref } from "vue";
3
+ import InputNumber from "./InputNumber.vue";
4
+
5
+ const meta = {
6
+ title: "Components/Form/InputNumber",
7
+ component: InputNumber,
8
+ tags: ["autodocs"],
9
+ argTypes: {
10
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
11
+ status: { options: [undefined, "error", "success"], control: { type: "inline-radio" } },
12
+ },
13
+ args: {
14
+ label: "Quantity",
15
+ helperPlacement: "below",
16
+ step: 1,
17
+ disabled: false,
18
+ },
19
+ decorators: [() => ({ template: '<div class="max-w-50"><story /></div>' })],
20
+ render: (args) => ({
21
+ components: { InputNumber },
22
+ setup() {
23
+ const value = ref<number | undefined>(1);
24
+ return { args, value };
25
+ },
26
+ template: `
27
+ <InputNumber v-bind="args" v-model="value" />
28
+ <small class="mt-3 block text-xs text-black/60">
29
+ Value: {{ value === undefined ? "undefined" : value }}
30
+ </small>`,
31
+ }),
32
+ } satisfies Meta<typeof InputNumber>;
33
+
34
+ export default meta;
35
+ type Story = StoryObj<typeof meta>;
36
+
37
+ /** Playground: use the controls below. */
38
+ export const Default: Story = {};
39
+
40
+ /** − is disabled at 0 and + at 10. */
41
+ export const MinAndMax: Story = {
42
+ args: { label: "Siblings at the school", min: 0, max: 10, helper: "Between 0 and 10." },
43
+ };
44
+
45
+ export const StepOfFive: Story = {
46
+ args: { label: "Lesson length (minutes)", min: 0, max: 120, step: 5 },
47
+ };
48
+
49
+ export const Currency: Story = {
50
+ args: {
51
+ label: "Trip cost",
52
+ min: 0,
53
+ step: 0.5,
54
+ locale: "en-GB",
55
+ formatOptions: { style: "currency", currency: "GBP" },
56
+ },
57
+ };
58
+
59
+ export const WithError: Story = {
60
+ args: { label: "Year group", min: 7, max: 13, errorText: "Choose a year group from 7 to 13" },
61
+ };
62
+
63
+ export const Disabled: Story = { args: { disabled: true } };