Skip to content

Commit

Permalink
Eager load forms code (#3099)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Sep 23, 2024
1 parent 3fa6eb9 commit 5533045
Show file tree
Hide file tree
Showing 85 changed files with 245 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/components/primer/alpha/select_panel_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {getAnchoredPosition} from '@primer/behaviors'
import {controller, target} from '@github/catalyst'
import {announceFromElement, announce} from '../aria_live'
import {IncludeFragmentElement} from '@github/include-fragment-element'
import type {PrimerTextFieldElement} from 'lib/primer/forms/primer_text_field'
import type {PrimerTextFieldElement} from 'app/lib/primer/forms/primer_text_field'
import type {AnchorAlignment, AnchorSide} from '@primer/behaviors'
import '@oddbird/popover-polyfill'

Expand Down
6 changes: 3 additions & 3 deletions app/components/primer/primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import './beta/auto_complete/auto_complete'
import './beta/clipboard_copy'
import './beta/relative_time'
import './alpha/tab_container'
import '../../../lib/primer/forms/primer_multi_input'
import '../../../lib/primer/forms/primer_text_field'
import '../../../lib/primer/forms/toggle_switch_input'
import '../../lib/primer/forms/primer_multi_input'
import '../../lib/primer/forms/primer_text_field'
import '../../lib/primer/forms/toggle_switch_input'
import './alpha/action_menu/action_menu_element'
import './alpha/select_panel_element'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions app/lib/primer/forms/primer_multi_input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export declare class PrimerMultiInputElement extends HTMLElement {
fields: HTMLInputElement[];
activateField(name: string): void;
private findField;
}
declare global {
interface Window {
PrimerMultiInputElement: typeof PrimerMultiInputElement;
}
}
44 changes: 44 additions & 0 deletions app/lib/primer/forms/primer_multi_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/* eslint-disable custom-elements/expose-class-on-global */
import { controller, targets } from '@github/catalyst';
let PrimerMultiInputElement = class PrimerMultiInputElement extends HTMLElement {
activateField(name) {
const fieldWithName = this.findField(name);
if (!fieldWithName)
return;
for (const field of this.fields) {
if (field === fieldWithName)
continue;
field.setAttribute('disabled', 'disabled');
field.setAttribute('hidden', 'hidden');
field.parentElement?.setAttribute('hidden', 'hidden');
}
fieldWithName.removeAttribute('disabled');
fieldWithName.removeAttribute('hidden');
fieldWithName.parentElement?.removeAttribute('hidden');
}
findField(name) {
for (const field of this.fields) {
if (field.getAttribute('data-name') === name) {
return field;
}
}
return null;
}
};
__decorate([
targets
], PrimerMultiInputElement.prototype, "fields", void 0);
PrimerMultiInputElement = __decorate([
controller
], PrimerMultiInputElement);
export { PrimerMultiInputElement };
if (!window.customElements.get('primer-multi-input')) {
Object.assign(window, { PrimerMultiInputElement });
window.customElements.define('primer-multi-input', PrimerMultiInputElement);
}
File renamed without changes.
28 changes: 28 additions & 0 deletions app/lib/primer/forms/primer_text_field.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import '@github/auto-check-element';
import type { AutoCheckErrorEvent, AutoCheckSuccessEvent } from '@github/auto-check-element';
declare global {
interface HTMLElementEventMap {
'auto-check-success': AutoCheckSuccessEvent;
'auto-check-error': AutoCheckErrorEvent;
}
}
export declare class PrimerTextFieldElement extends HTMLElement {
#private;
inputElement: HTMLInputElement;
validationElement: HTMLElement;
validationMessageElement: HTMLElement;
validationSuccessIcon: HTMLElement;
validationErrorIcon: HTMLElement;
leadingVisual: HTMLElement;
leadingSpinner: HTMLElement;
connectedCallback(): void;
disconnectedCallback(): void;
clearContents(): void;
clearError(): void;
setValidationMessage(message: string): void;
toggleValidationStyling(isError: boolean): void;
setSuccess(message: string): void;
setError(message: string): void;
showLeadingSpinner(): void;
hideLeadingSpinner(): void;
}
119 changes: 119 additions & 0 deletions app/lib/primer/forms/primer_text_field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint-disable custom-elements/expose-class-on-global */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _PrimerTextFieldElement_abortController;
import '@github/auto-check-element';
import { controller, target } from '@github/catalyst';
let PrimerTextFieldElement = class PrimerTextFieldElement extends HTMLElement {
constructor() {
super(...arguments);
_PrimerTextFieldElement_abortController.set(this, void 0);
}
connectedCallback() {
__classPrivateFieldGet(this, _PrimerTextFieldElement_abortController, "f")?.abort();
const { signal } = (__classPrivateFieldSet(this, _PrimerTextFieldElement_abortController, new AbortController(), "f"));
this.addEventListener('auto-check-success', async (event) => {
const message = await event.detail.response.text();
if (message && message.length > 0) {
this.setSuccess(message);
}
else {
this.clearError();
}
}, { signal });
this.addEventListener('auto-check-error', async (event) => {
const errorMessage = await event.detail.response.text();
this.setError(errorMessage);
}, { signal });
}
disconnectedCallback() {
__classPrivateFieldGet(this, _PrimerTextFieldElement_abortController, "f")?.abort();
}
clearContents() {
this.inputElement.value = '';
this.inputElement.focus();
this.inputElement.dispatchEvent(new Event('input', { bubbles: true, cancelable: false }));
}
clearError() {
this.inputElement.removeAttribute('invalid');
this.validationElement.hidden = true;
this.validationMessageElement.replaceChildren();
}
setValidationMessage(message) {
const template = document.createElement('template');
// eslint-disable-next-line github/no-inner-html
template.innerHTML = message;
const fragment = document.importNode(template.content, true);
this.validationMessageElement.replaceChildren(fragment);
}
toggleValidationStyling(isError) {
if (isError) {
this.validationElement.classList.remove('FormControl-inlineValidation--success');
}
else {
this.validationElement.classList.add('FormControl-inlineValidation--success');
}
this.validationSuccessIcon.hidden = isError;
this.validationErrorIcon.hidden = !isError;
this.inputElement.setAttribute('invalid', isError ? 'true' : 'false');
}
setSuccess(message) {
this.toggleValidationStyling(false);
this.setValidationMessage(message);
this.validationElement.hidden = false;
}
setError(message) {
this.toggleValidationStyling(true);
this.setValidationMessage(message);
this.validationElement.hidden = false;
}
showLeadingSpinner() {
this.leadingSpinner?.removeAttribute('hidden');
this.leadingVisual?.setAttribute('hidden', '');
}
hideLeadingSpinner() {
this.leadingSpinner?.setAttribute('hidden', '');
this.leadingVisual?.removeAttribute('hidden');
}
};
_PrimerTextFieldElement_abortController = new WeakMap();
__decorate([
target
], PrimerTextFieldElement.prototype, "inputElement", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "validationElement", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "validationMessageElement", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "validationSuccessIcon", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "validationErrorIcon", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "leadingVisual", void 0);
__decorate([
target
], PrimerTextFieldElement.prototype, "leadingSpinner", void 0);
PrimerTextFieldElement = __decorate([
controller
], PrimerTextFieldElement);
export { PrimerTextFieldElement };
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions app/lib/primer/forms/toggle_switch_input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare class ToggleSwitchInputElement extends HTMLElement {
validationElement: HTMLElement;
validationMessageElement: HTMLElement;
connectedCallback(): void;
}
34 changes: 34 additions & 0 deletions app/lib/primer/forms/toggle_switch_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable custom-elements/expose-class-on-global */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { controller, target } from '@github/catalyst';
let ToggleSwitchInputElement = class ToggleSwitchInputElement extends HTMLElement {
connectedCallback() {
this.addEventListener('toggleSwitchError', (event) => {
this.validationMessageElement.textContent = event.detail;
this.validationElement.removeAttribute('hidden');
});
this.addEventListener('toggleSwitchSuccess', () => {
this.validationMessageElement.textContent = '';
this.validationElement.setAttribute('hidden', 'hidden');
});
this.addEventListener('toggleSwitchLoading', () => {
this.validationMessageElement.textContent = '';
this.validationElement.setAttribute('hidden', 'hidden');
});
}
};
__decorate([
target
], ToggleSwitchInputElement.prototype, "validationElement", void 0);
__decorate([
target
], ToggleSwitchInputElement.prototype, "validationMessageElement", void 0);
ToggleSwitchInputElement = __decorate([
controller
], ToggleSwitchInputElement);
export { ToggleSwitchInputElement };
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"include": [
"app/components/primer/*.ts",
"lib/primer/forms/*.ts"
"app/lib/primer/forms/*.ts"
],
"exclude": [
"demo/**/*",
Expand Down

0 comments on commit 5533045

Please sign in to comment.