@thekestongroup/ui 0.2.0 → 0.3.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.
- package/README.md +90 -32
- package/dist/components/Age/Age.vue.d.ts +19 -0
- package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
- package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
- package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
- package/dist/components/Badge/Badge.vue.d.ts +41 -0
- package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
- package/dist/components/Button/Button.vue.d.ts +69 -0
- package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
- package/dist/components/Card/Card.vue.d.ts +56 -0
- package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
- package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
- package/dist/components/Chip/Chip.vue.d.ts +44 -0
- package/dist/components/Divider/Divider.vue.d.ts +36 -0
- package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
- package/dist/components/FormField/FormField.vue.d.ts +61 -0
- package/dist/components/Header/Header.vue.d.ts +104 -0
- package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
- package/dist/components/Input/Input.vue.d.ts +56 -0
- package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
- package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
- package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
- package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
- package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
- package/dist/components/Link/Link.vue.d.ts +39 -0
- package/dist/components/Notification/Notification.vue.d.ts +62 -0
- package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
- package/dist/components/Notification/Toaster.vue.d.ts +51 -0
- package/dist/components/Notification/notification.samples.d.ts +3 -0
- package/dist/components/Notification/notifications.d.ts +115 -0
- package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
- package/dist/components/Popover/Popover.vue.d.ts +83 -0
- package/dist/components/Popover/popover.d.ts +16 -0
- package/dist/components/Progress/Progress.vue.d.ts +26 -0
- package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
- package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
- package/dist/components/Select/Select.vue.d.ts +110 -0
- package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
- package/dist/components/Sidebar/sidebar.d.ts +10 -0
- package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
- package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
- package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
- package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
- package/dist/components/Switch/Switch.vue.d.ts +39 -0
- package/dist/components/Table/Table.vue.d.ts +82 -0
- package/dist/components/Table/table.d.ts +36 -0
- package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
- package/dist/index.d.ts +89 -25
- package/dist/keston-ui.js +4963 -14785
- package/dist/lib/cn.d.ts +4 -0
- package/dist/lib/dates.d.ts +27 -0
- package/dist/lib/initials.d.ts +2 -0
- package/dist/lib/parseDate.d.ts +44 -0
- package/dist/lib/useNow.d.ts +3 -0
- package/package.json +53 -24
- package/src/components/.gitkeep +0 -0
- package/src/components/Age/Age.stories.ts +24 -0
- package/src/components/Age/Age.vue +56 -0
- package/src/components/AppShell/AppShell.stories.ts +124 -0
- package/src/components/AppShell/AppShell.vue +212 -0
- package/src/components/Avatar/Avatar.stories.ts +56 -0
- package/src/components/Avatar/Avatar.vue +118 -0
- package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
- package/src/components/Badge/Badge.stories.ts +92 -0
- package/src/components/Badge/Badge.vue +67 -0
- package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
- package/src/components/Button/Button.mdx +79 -0
- package/src/components/Button/Button.stories.ts +207 -0
- package/src/components/Button/Button.vue +266 -0
- package/src/components/Calendar/Calendar.stories.ts +51 -0
- package/src/components/Calendar/Calendar.vue +364 -0
- package/src/components/Card/Card.stories.ts +109 -0
- package/src/components/Card/Card.vue +115 -0
- package/src/components/Checkbox/Checkbox.stories.ts +102 -0
- package/src/components/Checkbox/Checkbox.vue +102 -0
- package/src/components/Checklist/Checklist.stories.ts +67 -0
- package/src/components/Checklist/Checklist.vue +38 -0
- package/src/components/Chip/Chip.stories.ts +71 -0
- package/src/components/Chip/Chip.vue +86 -0
- package/src/components/Divider/Divider.stories.ts +57 -0
- package/src/components/Divider/Divider.vue +58 -0
- package/src/components/FormField/FieldMessage.vue +81 -0
- package/src/components/FormField/FormField.stories.ts +55 -0
- package/src/components/FormField/FormField.vue +160 -0
- package/src/components/Header/Header.stories.ts +168 -0
- package/src/components/Header/Header.vue +232 -0
- package/src/components/InfoTip/InfoTip.stories.ts +63 -0
- package/src/components/InfoTip/InfoTip.vue +50 -0
- package/src/components/Input/Input.stories.ts +206 -0
- package/src/components/Input/Input.vue +90 -0
- package/src/components/InputCode/InputCode.stories.ts +85 -0
- package/src/components/InputCode/InputCode.vue +145 -0
- package/src/components/InputDate/InputDate.stories.ts +58 -0
- package/src/components/InputDate/InputDate.vue +258 -0
- package/src/components/InputNumber/InputNumber.stories.ts +63 -0
- package/src/components/InputNumber/InputNumber.vue +109 -0
- package/src/components/InputPassword/InputPassword.stories.ts +105 -0
- package/src/components/InputPassword/InputPassword.vue +132 -0
- package/src/components/InputRichText/InputRichText.stories.ts +100 -0
- package/src/components/InputRichText/InputRichText.vue +194 -0
- package/src/components/Link/Link.stories.ts +73 -0
- package/src/components/Link/Link.vue +56 -0
- package/src/components/Notification/Notification.stories.ts +196 -0
- package/src/components/Notification/Notification.vue +136 -0
- package/src/components/Notification/NotificationPane.vue +187 -0
- package/src/components/Notification/Toaster.vue +446 -0
- package/src/components/Notification/notification.samples.ts +45 -0
- package/src/components/Notification/notifications.test.ts +99 -0
- package/src/components/Notification/notifications.ts +172 -0
- package/src/components/PageHeader/PageHeader.stories.ts +79 -0
- package/src/components/PageHeader/PageHeader.vue +66 -0
- package/src/components/Popover/Popover.stories.ts +135 -0
- package/src/components/Popover/Popover.vue +192 -0
- package/src/components/Popover/popover.ts +20 -0
- package/src/components/Progress/Progress.stories.ts +74 -0
- package/src/components/Progress/Progress.vue +75 -0
- package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
- package/src/components/RadioGroup/RadioGroup.vue +132 -0
- package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
- package/src/components/RelativeTime/RelativeTime.vue +51 -0
- package/src/components/Select/Select.mdx +202 -0
- package/src/components/Select/Select.stories.ts +223 -0
- package/src/components/Select/Select.vue +630 -0
- package/src/components/Sidebar/Sidebar.stories.ts +192 -0
- package/src/components/Sidebar/Sidebar.vue +382 -0
- package/src/components/Sidebar/sidebar.ts +20 -0
- package/src/components/Slideover/Slideover.stories.ts +225 -0
- package/src/components/Slideover/Slideover.vue +365 -0
- package/src/components/Spinner/Spinner.stories.ts +79 -0
- package/src/components/Spinner/Spinner.vue +97 -0
- package/src/components/StatTile/StatTile.stories.ts +75 -0
- package/src/components/StatTile/StatTile.vue +89 -0
- package/src/components/SummaryList/SummaryList.stories.ts +103 -0
- package/src/components/SummaryList/SummaryList.vue +127 -0
- package/src/components/Switch/Switch.stories.ts +100 -0
- package/src/components/Switch/Switch.vue +96 -0
- package/src/components/Table/Table.stories.ts +206 -0
- package/src/components/Table/Table.vue +220 -0
- package/src/components/Table/table.test.ts +65 -0
- package/src/components/Table/table.ts +70 -0
- package/src/components/Tooltip/Tooltip.stories.ts +57 -0
- package/src/components/Tooltip/Tooltip.vue +86 -0
- package/src/examples/AppPages.stories.ts +500 -0
- package/src/examples/FormLayout.stories.ts +185 -0
- package/src/examples/KestonIdPages.stories.ts +415 -0
- package/src/examples/SchoolPages.stories.ts +665 -0
- package/src/index.ts +114 -36
- package/src/lib/cn.ts +4 -0
- package/src/lib/dates.test.ts +68 -0
- package/src/lib/dates.ts +86 -0
- package/src/lib/initials.ts +8 -0
- package/src/lib/parseDate.test.ts +331 -0
- package/src/lib/parseDate.ts +561 -0
- package/src/lib/useNow.ts +20 -0
- package/src/styles/theme.css +101 -0
- package/dist/components/AppLauncher.vue.d.ts +0 -10
- package/dist/components/Avatar.vue.d.ts +0 -16
- package/dist/components/Button.vue.d.ts +0 -27
- package/dist/components/Checkbox.vue.d.ts +0 -18
- package/dist/components/CloseButton.vue.d.ts +0 -6
- package/dist/components/Dialog.vue.d.ts +0 -23
- package/dist/components/Drawer.vue.d.ts +0 -34
- package/dist/components/DropdownMenu.vue.d.ts +0 -21
- package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
- package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
- package/dist/components/Input.vue.d.ts +0 -14
- package/dist/components/Label.vue.d.ts +0 -16
- package/dist/components/NotificationsMenu.vue.d.ts +0 -3
- package/dist/components/Popover.vue.d.ts +0 -23
- package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
- package/dist/components/Select.vue.d.ts +0 -28
- package/dist/components/Separator.vue.d.ts +0 -11
- package/dist/components/ToastRenderer.vue.d.ts +0 -3
- package/dist/components/Toaster.vue.d.ts +0 -3
- package/dist/components/Tooltip.vue.d.ts +0 -23
- package/dist/components/TopBar.vue.d.ts +0 -34
- package/dist/favicon-old.svg +0 -1
- package/dist/favicon.svg +0 -1
- package/dist/keston-ui.cjs +0 -15
- package/dist/lib/toast.d.ts +0 -29
- package/dist/lib/types.d.ts +0 -5
- package/src/components/AppLauncher.vue +0 -63
- package/src/components/Avatar.vue +0 -51
- package/src/components/Button.vue +0 -54
- package/src/components/Checkbox.vue +0 -47
- package/src/components/CloseButton.vue +0 -24
- package/src/components/Dialog.vue +0 -65
- package/src/components/Drawer.vue +0 -202
- package/src/components/DropdownMenu.vue +0 -41
- package/src/components/DropdownMenuItem.vue +0 -28
- package/src/components/DropdownMenuSeparator.vue +0 -12
- package/src/components/Input.vue +0 -21
- package/src/components/Label.vue +0 -15
- package/src/components/NotificationsMenu.vue +0 -115
- package/src/components/Popover.vue +0 -46
- package/src/components/ProfileSwitcher.vue +0 -113
- package/src/components/Select.vue +0 -97
- package/src/components/Separator.vue +0 -30
- package/src/components/ToastItem.vue +0 -158
- package/src/components/ToastRenderer.vue +0 -113
- package/src/components/Toaster.vue +0 -11
- package/src/components/Tooltip.vue +0 -48
- package/src/components/TopBar.vue +0 -52
- package/src/lib/toast.ts +0 -95
- package/src/lib/types.ts +0 -5
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
+
import { AtSign, Lock, Mail, Search, User, X } from "@lucide/vue";
|
|
3
|
+
import { computed, ref, watch } from "vue";
|
|
4
|
+
import Button from "../Button/Button.vue";
|
|
5
|
+
import Input, { type InputProps } from "./Input.vue";
|
|
6
|
+
|
|
7
|
+
// Icons offered in the Controls panel. Add any Lucide icon here.
|
|
8
|
+
const icons = { None: null, Search, User, Mail, AtSign, Lock, X };
|
|
9
|
+
type IconName = keyof typeof icons;
|
|
10
|
+
|
|
11
|
+
// Story-only controls on top of the real props.
|
|
12
|
+
type InputStoryArgs = InputProps & {
|
|
13
|
+
placeholder: string;
|
|
14
|
+
leadingIcon: IconName;
|
|
15
|
+
trailingIcon: IconName;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const iconControl = {
|
|
19
|
+
options: Object.keys(icons),
|
|
20
|
+
control: { type: "select" },
|
|
21
|
+
table: { category: "Content" },
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
// Splits the story-only args from the real props and resolves the icons.
|
|
25
|
+
function useStoryInput(args: InputStoryArgs) {
|
|
26
|
+
const inputProps = computed(() => {
|
|
27
|
+
const { leadingIcon: _leading, trailingIcon: _trailing, ...rest } = args;
|
|
28
|
+
return rest;
|
|
29
|
+
});
|
|
30
|
+
const leading = computed(() => icons[args.leadingIcon]);
|
|
31
|
+
const trailing = computed(() => icons[args.trailingIcon]);
|
|
32
|
+
const value = ref("");
|
|
33
|
+
return { args, inputProps, leading, trailing, value };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const inputTemplate = `
|
|
37
|
+
<Input v-bind="inputProps" v-model="value">
|
|
38
|
+
<template v-if="leading" #leading><component :is="leading" /></template>
|
|
39
|
+
<template v-if="trailing" #trailing><component :is="trailing" /></template>
|
|
40
|
+
</Input>`;
|
|
41
|
+
|
|
42
|
+
const meta = {
|
|
43
|
+
title: "Components/Form/Input",
|
|
44
|
+
component: Input,
|
|
45
|
+
tags: ["autodocs"],
|
|
46
|
+
argTypes: {
|
|
47
|
+
placeholder: { control: "text", table: { category: "Content" } },
|
|
48
|
+
leadingIcon: iconControl,
|
|
49
|
+
trailingIcon: iconControl,
|
|
50
|
+
helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
|
|
51
|
+
status: { options: [undefined, "error", "success", "loading"], control: { type: "inline-radio" } },
|
|
52
|
+
},
|
|
53
|
+
args: {
|
|
54
|
+
label: "First name",
|
|
55
|
+
placeholder: "",
|
|
56
|
+
helper: "",
|
|
57
|
+
helperPlacement: "below",
|
|
58
|
+
leadingIcon: "None",
|
|
59
|
+
trailingIcon: "None",
|
|
60
|
+
disabled: false,
|
|
61
|
+
},
|
|
62
|
+
decorators: [() => ({ template: '<div class="max-w-80"><story /></div>' })],
|
|
63
|
+
render: (args) => ({
|
|
64
|
+
components: { Input },
|
|
65
|
+
setup: () => useStoryInput(args),
|
|
66
|
+
template: inputTemplate,
|
|
67
|
+
}),
|
|
68
|
+
} satisfies Meta<InputStoryArgs>;
|
|
69
|
+
|
|
70
|
+
export default meta;
|
|
71
|
+
type Story = StoryObj<typeof meta>;
|
|
72
|
+
|
|
73
|
+
/** Playground: use the controls below. */
|
|
74
|
+
export const Default: Story = {};
|
|
75
|
+
|
|
76
|
+
export const HelperBelowLabel: Story = {
|
|
77
|
+
args: {
|
|
78
|
+
label: "Student ID",
|
|
79
|
+
helper: "Found on the student's enrolment letter, e.g. KS-20491.",
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const HelperBehindIcon: Story = {
|
|
84
|
+
args: {
|
|
85
|
+
label: "Student ID",
|
|
86
|
+
helper: "Found on the student's enrolment letter, e.g. KS-20491.",
|
|
87
|
+
helperPlacement: "icon",
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Type fewer than 3 characters and leave the field, or more than 20. */
|
|
92
|
+
export const LengthCheck: Story = {
|
|
93
|
+
args: { label: "Username", minLength: 3, maxLength: 20, leadingIcon: "AtSign" },
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const WithIcons: Story = {
|
|
97
|
+
args: { label: "Search students", placeholder: "Name or ID", leadingIcon: "Search" },
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** `errorText` alone puts the field in its error state. */
|
|
101
|
+
export const WithErrorText: Story = {
|
|
102
|
+
args: { label: "Email", errorText: "Enter a valid email address", leadingIcon: "Mail" },
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* How a schema (Zod, VeeValidate…) drives the Input: the form validates and
|
|
107
|
+
* passes the message, or `undefined` when valid, to `errorText`. Checked when
|
|
108
|
+
* you leave the field, then again as you type once an error is showing.
|
|
109
|
+
* In an app this would be e.g. `:error-text="errors.email"` from VeeValidate.
|
|
110
|
+
*/
|
|
111
|
+
export const SchemaValidation: Story = {
|
|
112
|
+
args: { label: "Email", placeholder: "name@school.org", leadingIcon: "Mail" },
|
|
113
|
+
render: (args) => ({
|
|
114
|
+
components: { Input },
|
|
115
|
+
setup() {
|
|
116
|
+
const story = useStoryInput(args);
|
|
117
|
+
const error = ref<string>();
|
|
118
|
+
// Stand-in for z.string().min(1, …).email(…)
|
|
119
|
+
const validate = (value: string) =>
|
|
120
|
+
!value ? "Enter an email address"
|
|
121
|
+
: !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? "Enter a valid email address"
|
|
122
|
+
: undefined;
|
|
123
|
+
const onBlur = () => (error.value = validate(story.value.value));
|
|
124
|
+
const onInput = () => { if (error.value) error.value = validate(story.value.value); };
|
|
125
|
+
return { ...story, error, onBlur, onInput };
|
|
126
|
+
},
|
|
127
|
+
template: `
|
|
128
|
+
<Input v-bind="inputProps" v-model="value" :error-text="error" @blur="onBlur" @input="onInput">
|
|
129
|
+
<template v-if="leading" #leading><component :is="leading" /></template>
|
|
130
|
+
</Input>`,
|
|
131
|
+
}),
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const ErrorStatus: Story = {
|
|
135
|
+
args: { label: "Email", status: "error", statusText: "Enter a valid email address", leadingIcon: "Mail" },
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export const SuccessStatus: Story = {
|
|
139
|
+
args: { label: "Username", status: "success", statusText: "Username is available", leadingIcon: "AtSign" },
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Checking a username as you type: a small spinner shows "Checking
|
|
144
|
+
* availability…" until typing pauses and the check comes back, then the result. Here the check is faked with a
|
|
145
|
+
* short delay; try "admin", "ben" or "keston" for one that's taken.
|
|
146
|
+
*/
|
|
147
|
+
export const UsernameAvailability: Story = {
|
|
148
|
+
args: { label: "Username", placeholder: "e.g. jsmith", leadingIcon: "AtSign", minLength: 3 },
|
|
149
|
+
render: (args) => ({
|
|
150
|
+
components: { Input },
|
|
151
|
+
setup() {
|
|
152
|
+
const story = useStoryInput(args);
|
|
153
|
+
const taken = ["admin", "ben", "keston", "jsmith"];
|
|
154
|
+
const status = ref<InputProps["status"]>();
|
|
155
|
+
const statusText = ref<string>();
|
|
156
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
157
|
+
let check = 0;
|
|
158
|
+
|
|
159
|
+
watch(story.value, (value) => {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
// Only the latest check counts, in case an older one comes back late.
|
|
162
|
+
const id = ++check;
|
|
163
|
+
const name = value.trim().toLowerCase();
|
|
164
|
+
// Too short: nothing to check yet (the length check says why on blur).
|
|
165
|
+
if (name.length < 3) {
|
|
166
|
+
status.value = statusText.value = undefined;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// Show it's checking straight away, so an earlier result is never
|
|
170
|
+
// shown for a different name, then "ask the server" once typing pauses.
|
|
171
|
+
status.value = "loading";
|
|
172
|
+
statusText.value = "Checking availability…";
|
|
173
|
+
timer = setTimeout(async () => {
|
|
174
|
+
await new Promise((resolve) => setTimeout(resolve, 1200));
|
|
175
|
+
if (id !== check) return;
|
|
176
|
+
const free = !taken.includes(name);
|
|
177
|
+
status.value = free ? "success" : "error";
|
|
178
|
+
statusText.value = free ? "Username available" : "Username unavailable";
|
|
179
|
+
}, 400);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
return { ...story, status, statusText };
|
|
183
|
+
},
|
|
184
|
+
template: `
|
|
185
|
+
<Input v-bind="inputProps" v-model="value" :status="status" :status-text="statusText" autocomplete="username">
|
|
186
|
+
<template v-if="leading" #leading><component :is="leading" /></template>
|
|
187
|
+
</Input>`,
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export const Disabled: Story = { args: { disabled: true, placeholder: "Not editable" } };
|
|
192
|
+
|
|
193
|
+
/** Lined up with a medium basic Button to compare height, border and padding. */
|
|
194
|
+
export const NextToButton: Story = {
|
|
195
|
+
args: { label: "Search students", placeholder: "Name or ID", leadingIcon: "Search" },
|
|
196
|
+
decorators: [() => ({ template: '<div class="max-w-120"><story /></div>' })],
|
|
197
|
+
render: (args) => ({
|
|
198
|
+
components: { Input, Button },
|
|
199
|
+
setup: () => useStoryInput(args),
|
|
200
|
+
template: `
|
|
201
|
+
<div class="flex items-end gap-2">
|
|
202
|
+
<div class="flex-1">${inputTemplate}</div>
|
|
203
|
+
<Button><template #leading><component :is="leading" /></template>Search</Button>
|
|
204
|
+
</div>`,
|
|
205
|
+
}),
|
|
206
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, type HTMLAttributes } from 'vue'
|
|
3
|
+
import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
|
|
4
|
+
|
|
5
|
+
// Attributes like placeholder, type, name and autocomplete go to the <input>,
|
|
6
|
+
// not the wrapper.
|
|
7
|
+
defineOptions({ inheritAttrs: false })
|
|
8
|
+
|
|
9
|
+
export type InputStatus = FormFieldStatus
|
|
10
|
+
|
|
11
|
+
export interface InputProps {
|
|
12
|
+
/** Visible label. Always set it: it is also the input's accessible name. */
|
|
13
|
+
label: string
|
|
14
|
+
/** Extra guidance about what to enter. */
|
|
15
|
+
helper?: string
|
|
16
|
+
/** Show helper text under the label, or behind a `?` icon next to it. */
|
|
17
|
+
helperPlacement?: 'below' | 'icon'
|
|
18
|
+
/**
|
|
19
|
+
* Error message, e.g. from a Zod / VeeValidate schema. When set (and not
|
|
20
|
+
* empty) the field shows as an error with this text; leave it undefined
|
|
21
|
+
* when the value is valid. Overrides `status`, `statusText` and the length
|
|
22
|
+
* check.
|
|
23
|
+
*/
|
|
24
|
+
errorText?: string
|
|
25
|
+
/** Colours the field and the status text. Overrides the length check. */
|
|
26
|
+
status?: InputStatus
|
|
27
|
+
/** Message under the field, e.g. why the value is invalid. */
|
|
28
|
+
statusText?: string
|
|
29
|
+
/** Shows an error once the user leaves a field that is too short. */
|
|
30
|
+
minLength?: number
|
|
31
|
+
/** Shows an error as soon as the value is too long. Typing is not blocked. */
|
|
32
|
+
maxLength?: number
|
|
33
|
+
disabled?: boolean
|
|
34
|
+
class?: HTMLAttributes['class']
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const props = withDefaults(defineProps<InputProps>(), {
|
|
38
|
+
helperPlacement: 'below'
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const model = defineModel<string>({ default: '' })
|
|
42
|
+
|
|
43
|
+
// ── Length check ────────────────────────────────────────────────────────────
|
|
44
|
+
// "Too short" waits until the user has left the field once, so it doesn't
|
|
45
|
+
// complain while they are still typing. "Too long" shows straight away.
|
|
46
|
+
const touched = ref(false)
|
|
47
|
+
|
|
48
|
+
const lengthError = computed(() => {
|
|
49
|
+
const length = model.value.length
|
|
50
|
+
if (props.maxLength !== undefined && length > props.maxLength) {
|
|
51
|
+
return `Must be ${props.maxLength} characters or fewer`
|
|
52
|
+
}
|
|
53
|
+
if (touched.value && props.minLength !== undefined && length > 0 && length < props.minLength) {
|
|
54
|
+
return `Must be at least ${props.minLength} characters`
|
|
55
|
+
}
|
|
56
|
+
return undefined
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// errorText is handled by FormField; status / statusText fall back to the length check.
|
|
60
|
+
const status = computed(() => props.status ?? (lengthError.value ? 'error' : undefined))
|
|
61
|
+
const statusText = computed(() => props.statusText ?? lengthError.value)
|
|
62
|
+
</script>
|
|
63
|
+
|
|
64
|
+
<template>
|
|
65
|
+
<FormField
|
|
66
|
+
:label="label"
|
|
67
|
+
:helper="helper"
|
|
68
|
+
:helper-placement="helperPlacement"
|
|
69
|
+
:error-text="errorText"
|
|
70
|
+
:status="status"
|
|
71
|
+
:status-text="statusText"
|
|
72
|
+
:disabled="disabled"
|
|
73
|
+
:class="props.class"
|
|
74
|
+
>
|
|
75
|
+
<template v-if="$slots.leading" #leading><slot name="leading" /></template>
|
|
76
|
+
<template #default="{ id, describedBy, invalid }">
|
|
77
|
+
<input
|
|
78
|
+
:id="id"
|
|
79
|
+
v-model="model"
|
|
80
|
+
v-bind="$attrs"
|
|
81
|
+
:disabled="disabled"
|
|
82
|
+
:aria-invalid="invalid"
|
|
83
|
+
:aria-describedby="describedBy"
|
|
84
|
+
class="h-5 min-w-0 flex-1 bg-transparent outline-none placeholder:text-black/55 disabled:cursor-not-allowed"
|
|
85
|
+
@blur="touched = true"
|
|
86
|
+
/>
|
|
87
|
+
</template>
|
|
88
|
+
<template v-if="$slots.trailing" #trailing><slot name="trailing" /></template>
|
|
89
|
+
</FormField>
|
|
90
|
+
</template>
|
|
@@ -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 } };
|