Skip to content

Commit 13874c8

Browse files
authored
refactor: extract login overlay and form reusable mixins (#6253)
1 parent e86a065 commit 13874c8

8 files changed

Lines changed: 301 additions & 242 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2018 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
import type { LoginMixinClass } from './vaadin-login-mixin.js';
8+
9+
export declare function LoginFormMixin<T extends Constructor<HTMLElement>>(
10+
base: T,
11+
): Constructor<LoginFormMixinClass> & Constructor<LoginMixinClass> & T;
12+
13+
export declare class LoginFormMixinClass {
14+
/**
15+
* Submits the form.
16+
*/
17+
submit(): void;
18+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2018 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { LoginMixin } from './vaadin-login-mixin.js';
7+
8+
/**
9+
* @polymerMixin
10+
* @mixes LoginMixin
11+
*/
12+
export const LoginFormMixin = (superClass) =>
13+
class LoginFormMixin extends LoginMixin(superClass) {
14+
static get observers() {
15+
return ['_errorChanged(error)'];
16+
}
17+
18+
/** @protected */
19+
connectedCallback() {
20+
super.connectedCallback();
21+
22+
if (!this.noAutofocus) {
23+
this.$.vaadinLoginUsername.focus();
24+
}
25+
}
26+
27+
/** @private */
28+
_errorChanged() {
29+
if (this.error && !this._preventAutoEnable) {
30+
this.disabled = false;
31+
}
32+
}
33+
34+
/**
35+
* Submits the form.
36+
*/
37+
submit() {
38+
const userName = this.$.vaadinLoginUsername;
39+
const password = this.$.vaadinLoginPassword;
40+
41+
if (this.disabled || !(userName.validate() && password.validate())) {
42+
return;
43+
}
44+
45+
this.error = false;
46+
this.disabled = true;
47+
48+
const loginEventDetails = {
49+
bubbles: true,
50+
cancelable: true,
51+
detail: {
52+
username: userName.value,
53+
password: password.value,
54+
},
55+
};
56+
57+
const firedEvent = this.dispatchEvent(new CustomEvent('login', loginEventDetails));
58+
if (this.action && firedEvent) {
59+
const csrfMetaName = document.querySelector('meta[name=_csrf_parameter]');
60+
const csrfMetaValue = document.querySelector('meta[name=_csrf]');
61+
if (csrfMetaName && csrfMetaValue) {
62+
this.$.csrf.name = csrfMetaName.content;
63+
this.$.csrf.value = csrfMetaValue.content;
64+
}
65+
this.querySelector('form').submit();
66+
}
67+
}
68+
69+
/** @protected */
70+
_handleInputKeydown(e) {
71+
if (e.key === 'Enter') {
72+
const { currentTarget: inputActive } = e;
73+
const nextInput =
74+
inputActive.id === 'vaadinLoginUsername' ? this.$.vaadinLoginPassword : this.$.vaadinLoginUsername;
75+
if (inputActive.validate()) {
76+
if (nextInput.checkValidity()) {
77+
this.submit();
78+
} else {
79+
nextInput.focus();
80+
}
81+
}
82+
}
83+
}
84+
85+
/** @protected */
86+
_handleInputKeyup(e) {
87+
const input = e.currentTarget;
88+
if (e.key === 'Tab' && input instanceof HTMLInputElement) {
89+
input.select();
90+
}
91+
}
92+
93+
/** @protected */
94+
_onForgotPasswordClick() {
95+
this.dispatchEvent(new CustomEvent('forgot-password'));
96+
}
97+
};

‎packages/login/src/vaadin-login-form.d.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
77
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
8-
import { LoginMixin } from './vaadin-login-mixin.js';
8+
import { LoginFormMixin } from './vaadin-login-form-mixin.js';
99
export { LoginI18n } from './vaadin-login-mixin.js';
1010

1111
/**
@@ -69,9 +69,7 @@ export interface LoginFormEventMap extends HTMLElementEventMap, LoginFormCustomE
6969
* @fires {CustomEvent} forgot-password - Fired when user clicks on the "Forgot password" button.
7070
* @fires {CustomEvent} login - Fired when a user submits the login.
7171
*/
72-
declare class LoginForm extends ElementMixin(ThemableMixin(LoginMixin(HTMLElement))) {
73-
submit(): void;
74-
72+
declare class LoginForm extends LoginFormMixin(ElementMixin(ThemableMixin(HTMLElement))) {
7573
addEventListener<K extends keyof LoginFormEventMap>(
7674
type: K,
7775
listener: (this: LoginForm, ev: LoginFormEventMap[K]) => void,

‎packages/login/src/vaadin-login-form.js‎

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import './vaadin-login-form-wrapper.js';
1010
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
1111
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1212
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
13-
import { LoginMixin } from './vaadin-login-mixin.js';
13+
import { LoginFormMixin } from './vaadin-login-form-mixin.js';
1414

1515
/**
1616
* `<vaadin-login-form>` is a Web Component providing an easy way to require users
@@ -49,9 +49,9 @@ import { LoginMixin } from './vaadin-login-mixin.js';
4949
* @extends HTMLElement
5050
* @mixes ElementMixin
5151
* @mixes ThemableMixin
52-
* @mixes LoginMixin
52+
* @mixes LoginFormMixin
5353
*/
54-
class LoginForm extends LoginMixin(ElementMixin(ThemableMixin(PolymerElement))) {
54+
class LoginForm extends LoginFormMixin(ElementMixin(ThemableMixin(PolymerElement))) {
5555
static get template() {
5656
return html`
5757
<style>
@@ -111,19 +111,6 @@ class LoginForm extends LoginMixin(ElementMixin(ThemableMixin(PolymerElement)))
111111
return 'vaadin-login-form';
112112
}
113113

114-
static get observers() {
115-
return ['_errorChanged(error)'];
116-
}
117-
118-
/** @protected */
119-
connectedCallback() {
120-
super.connectedCallback();
121-
122-
if (!this.noAutofocus) {
123-
this.$.vaadinLoginUsername.focus();
124-
}
125-
}
126-
127114
/**
128115
* @param {StampedTemplate} dom
129116
* @return {null}
@@ -132,74 +119,6 @@ class LoginForm extends LoginMixin(ElementMixin(ThemableMixin(PolymerElement)))
132119
_attachDom(dom) {
133120
this.appendChild(dom);
134121
}
135-
136-
/** @private */
137-
_errorChanged() {
138-
if (this.error && !this._preventAutoEnable) {
139-
this.disabled = false;
140-
}
141-
}
142-
143-
submit() {
144-
const userName = this.$.vaadinLoginUsername;
145-
const password = this.$.vaadinLoginPassword;
146-
147-
if (this.disabled || !(userName.validate() && password.validate())) {
148-
return;
149-
}
150-
151-
this.error = false;
152-
this.disabled = true;
153-
154-
const loginEventDetails = {
155-
bubbles: true,
156-
cancelable: true,
157-
detail: {
158-
username: userName.value,
159-
password: password.value,
160-
},
161-
};
162-
163-
const firedEvent = this.dispatchEvent(new CustomEvent('login', loginEventDetails));
164-
if (this.action && firedEvent) {
165-
const csrfMetaName = document.querySelector('meta[name=_csrf_parameter]');
166-
const csrfMetaValue = document.querySelector('meta[name=_csrf]');
167-
if (csrfMetaName && csrfMetaValue) {
168-
this.$.csrf.name = csrfMetaName.content;
169-
this.$.csrf.value = csrfMetaValue.content;
170-
}
171-
this.querySelector('form').submit();
172-
}
173-
}
174-
175-
/** @private */
176-
_handleInputKeydown(e) {
177-
if (e.key === 'Enter') {
178-
const { currentTarget: inputActive } = e;
179-
const nextInput =
180-
inputActive.id === 'vaadinLoginUsername' ? this.$.vaadinLoginPassword : this.$.vaadinLoginUsername;
181-
if (inputActive.validate()) {
182-
if (nextInput.checkValidity()) {
183-
this.submit();
184-
} else {
185-
nextInput.focus();
186-
}
187-
}
188-
}
189-
}
190-
191-
/** @private */
192-
_handleInputKeyup(e) {
193-
const input = e.currentTarget;
194-
if (e.key === 'Tab' && input instanceof HTMLInputElement) {
195-
input.select();
196-
}
197-
}
198-
199-
/** @private */
200-
_onForgotPasswordClick() {
201-
this.dispatchEvent(new CustomEvent('forgot-password'));
202-
}
203122
}
204123

205124
customElements.define(LoginForm.is, LoginForm);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2018 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
import type { OverlayClassMixinClass } from '@vaadin/component-base/src/overlay-class-mixin.js';
8+
import type { LoginMixinClass } from './vaadin-login-mixin.js';
9+
10+
export declare function LoginOverlayMixin<T extends Constructor<HTMLElement>>(
11+
base: T,
12+
): Constructor<LoginMixinClass> & Constructor<LoginOverlayMixinClass> & Constructor<OverlayClassMixinClass> & T;
13+
14+
export declare class LoginOverlayMixinClass {
15+
/**
16+
* Defines the application description
17+
*/
18+
description: string;
19+
20+
/**
21+
* True if the overlay is currently displayed.
22+
*/
23+
opened: boolean;
24+
25+
/**
26+
* Defines the application title
27+
*/
28+
title: string;
29+
}

0 commit comments

Comments
 (0)