@xleddyl/443-components-vue 0.16.34 → 0.16.36

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.
@@ -0,0 +1,220 @@
1
+ <template>
2
+ <AuthLayout v-bind="frame" :title="title" :description="description" :breadcrumbs="breadcrumbs">
3
+ <template v-if="$slots.brand" #brand><slot name="brand" /></template>
4
+ <template v-if="$slots.corner" #corner><slot name="corner" /></template>
5
+ <div ref="content" class="k-auth-login">
6
+ <Transition name="k-auth-step" mode="out-in" @after-enter="focusInput">
7
+ <div
8
+ v-if="view === 'signedOut'"
9
+ key="signedOut"
10
+ class="k-auth-login-actions"
11
+ :class="{ 'is-pending': !signedOutReady }"
12
+ >
13
+ <Button size="lg" block trailing-icon="arrow-right" @click="signInAgain">
14
+ {{ t("auth.signInAgain") }}
15
+ </Button>
16
+ <Button
17
+ v-if="account?.email"
18
+ size="lg"
19
+ block
20
+ variant="soft"
21
+ tone="neutral"
22
+ @click="useAnotherAccount"
23
+ >
24
+ {{ t("auth.otherAccount") }}
25
+ </Button>
26
+ </div>
27
+ <Form v-else-if="!passwordStep" key="email" class="k-auth-login-form" :form="emailForm">
28
+ <FormField name="email" :label="emailText" required>
29
+ <Input
30
+ :model-value="emailForm.values.email"
31
+ @update:model-value="emailForm.values.email = String($event ?? '')"
32
+ type="email"
33
+ icon="envelope"
34
+ autocomplete="username"
35
+ :placeholder="emailPlaceholder ?? t('auth.emailPlaceholder')"
36
+ />
37
+ </FormField>
38
+ <Button type="submit" size="lg" block trailing-icon="arrow-right">
39
+ {{ t("auth.continue") }}
40
+ </Button>
41
+ </Form>
42
+ <Form v-else key="password" class="k-auth-login-form" :form="passwordForm">
43
+ <input
44
+ :value="email"
45
+ type="email"
46
+ autocomplete="username"
47
+ class="k-visually-hidden"
48
+ tabindex="-1"
49
+ aria-hidden="true"
50
+ readonly
51
+ />
52
+ <FormField name="password" :label="passwordText" required>
53
+ <PasswordInput
54
+ :model-value="passwordForm.values.password"
55
+ @update:model-value="passwordForm.values.password = $event ?? ''"
56
+ icon="key"
57
+ autocomplete="current-password"
58
+ />
59
+ </FormField>
60
+ <Button
61
+ type="submit"
62
+ size="lg"
63
+ block
64
+ trailing-icon="arrow-right"
65
+ :loading="passwordForm.submitting.value"
66
+ >
67
+ {{ submitText }}
68
+ </Button>
69
+ </Form>
70
+ </Transition>
71
+ </div>
72
+ <template v-if="$slots.links && view === 'login'" #links>
73
+ <slot name="links" :email="email" :step="passwordStep ? 'password' : 'email'" />
74
+ </template>
75
+ <template v-if="$slots.footer" #footer><slot name="footer" /></template>
76
+ <template v-if="$slots.aside" #aside><slot name="aside" /></template>
77
+ </AuthLayout>
78
+ </template>
79
+
80
+ <script setup>
81
+ import { computed, onMounted, ref, watch } from "vue";
82
+ import { useForm } from "../../composables/useForm";
83
+ import { useLocale } from "../../composables/useLocale";
84
+ import { useRouteState } from "../../composables/useRouteState";
85
+ import { useRouterLike } from "../../internal/layout/route";
86
+ import Button from "../core/Button.vue";
87
+ import Form from "../core/Form.vue";
88
+ import FormField from "../core/FormField.vue";
89
+ import Input from "../core/Input.vue";
90
+ import PasswordInput from "../forms/PasswordInput.vue";
91
+ import AuthLayout from "./AuthLayout.vue";
92
+ import { SIGNED_OUT_REASON, takeSignedOutAccount } from "./auth";
93
+ const props = defineProps({
94
+ title: { type: String, required: false },
95
+ description: { type: String, required: false },
96
+ footer: { type: String, required: false },
97
+ brandName: { type: String, required: false },
98
+ brandSubtitle: { type: String, required: false },
99
+ brandIcon: { type: null, required: false },
100
+ brandLogo: { type: null, required: false },
101
+ brandTo: { type: [String, Object], required: false },
102
+ aside: { type: Boolean, required: false, default: true },
103
+ asideTitle: { type: String, required: false },
104
+ asideDescription: { type: String, required: false },
105
+ asideLabel: { type: String, required: false },
106
+ themeSwitch: { type: Boolean, required: false },
107
+ themeLabel: { type: String, required: false },
108
+ skipLabel: { type: String, required: false },
109
+ submit: { type: Function, required: true },
110
+ submitError: { type: Function, required: false },
111
+ emailLabel: { type: String, required: false },
112
+ emailPlaceholder: { type: String, required: false },
113
+ passwordLabel: { type: String, required: false },
114
+ submitLabel: { type: String, required: false },
115
+ stepKey: { type: String, required: false, default: "step" },
116
+ reasonKey: { type: String, required: false, default: "reason" }
117
+ });
118
+ defineSlots();
119
+ const { t } = useLocale();
120
+ const router = useRouterLike();
121
+ const step = useRouteState(props.stepKey, { location: "query" });
122
+ const reason = useRouteState(props.reasonKey, { location: "query", history: "replace" });
123
+ const stepReplace = useRouteState(props.stepKey, { location: "query", history: "replace" });
124
+ const view = ref(reason.value === SIGNED_OUT_REASON ? "signedOut" : "login");
125
+ const email = ref("");
126
+ const account = ref(null);
127
+ const signedOutReady = ref(false);
128
+ const content = ref();
129
+ const emailText = computed(() => props.emailLabel ?? t("auth.email"));
130
+ const passwordText = computed(() => props.passwordLabel ?? t("auth.password"));
131
+ const submitText = computed(() => props.submitLabel ?? t("auth.signIn"));
132
+ const frame = computed(() => ({
133
+ footer: props.footer,
134
+ brandName: props.brandName,
135
+ brandSubtitle: props.brandSubtitle,
136
+ brandIcon: props.brandIcon,
137
+ brandLogo: props.brandLogo,
138
+ brandTo: props.brandTo,
139
+ aside: props.aside,
140
+ asideTitle: props.asideTitle,
141
+ asideDescription: props.asideDescription,
142
+ asideLabel: props.asideLabel,
143
+ themeSwitch: props.themeSwitch,
144
+ themeLabel: props.themeLabel,
145
+ skipLabel: props.skipLabel
146
+ }));
147
+ const passwordStep = computed(
148
+ () => view.value === "login" && step.value === "password" && !!email.value
149
+ );
150
+ const title = computed(() => {
151
+ if (view.value === "login") return props.title ?? t("auth.loginTitle");
152
+ const name = account.value?.name;
153
+ return name ? t("auth.signedOutTitleNamed", { name }) : t("auth.signedOutTitle");
154
+ });
155
+ const description = computed(() => {
156
+ if (view.value === "login") return props.description;
157
+ const signedOutEmail = account.value?.email;
158
+ return signedOutEmail ? t("auth.signedOutDescriptionEmail", { email: signedOutEmail }) : t("auth.signedOutDescription");
159
+ });
160
+ const knownName = computed(
161
+ () => account.value?.email === email.value ? account.value?.name : void 0
162
+ );
163
+ const breadcrumbs = computed(
164
+ () => passwordStep.value ? [{ label: title.value, onClick: backToEmail }, { label: knownName.value || email.value }] : void 0
165
+ );
166
+ const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
167
+ const emailForm = useForm({
168
+ schema: {
169
+ email: {
170
+ initial: "",
171
+ required: true,
172
+ validate: (value) => EMAIL_PATTERN.test(normalizeEmail(value)) ? void 0 : t("auth.emailInvalid")
173
+ }
174
+ },
175
+ onSubmit: (values) => openPasswordStep(normalizeEmail(values.email))
176
+ });
177
+ const passwordForm = useForm({
178
+ schema: { password: { initial: "", required: true } },
179
+ onSubmit: (values) => props.submit({ email: email.value, password: values.password }),
180
+ submitError: props.submitError
181
+ });
182
+ function normalizeEmail(value) {
183
+ return value.replace(/\s+/g, "").toLowerCase();
184
+ }
185
+ function openPasswordStep(value) {
186
+ email.value = value;
187
+ emailForm.values.email = value;
188
+ passwordForm.reset();
189
+ step.value = "password";
190
+ }
191
+ function backToEmail() {
192
+ if (router?.back) router.back();
193
+ else step.value = void 0;
194
+ }
195
+ function signInAgain() {
196
+ view.value = "login";
197
+ if (account.value?.email) openPasswordStep(account.value.email);
198
+ }
199
+ function useAnotherAccount() {
200
+ account.value = null;
201
+ email.value = "";
202
+ emailForm.reset();
203
+ view.value = "login";
204
+ }
205
+ function focusInput() {
206
+ content.value?.querySelector("input:not([readonly])")?.focus();
207
+ }
208
+ watch(step, (value) => {
209
+ if (value !== "password") passwordForm.reset();
210
+ });
211
+ onMounted(() => {
212
+ if (view.value === "signedOut") {
213
+ account.value = takeSignedOutAccount();
214
+ signedOutReady.value = true;
215
+ }
216
+ if (reason.value) reason.value = void 0;
217
+ if (step.value && !email.value) stepReplace.value = void 0;
218
+ focusInput();
219
+ });
220
+ </script>