Files
authentik/web/src/elements/Alert.ts
Ken Sternberg 058a388518 web: unit tests for the simple things, with fixes that the tests revealed (#11633)
* Added tests and refinements as tests indicate.

* Building out the test suite.

* web: test the simple things. Fix what the tests revealed.

- Move `EmptyState.test.ts` into the `./tests` folder.
- Provide unit tests for:
  - Alert
  - Divider
  - Expand
  - Label
  - LoadingOverlay
- Give all tested items an Interface and a functional variant for rendering
- Give Label an alternative syntax for declaring alert levels
- Remove the slot name in LoadingOverlay
  - Change the slot call in `./enterprise/rac/index.ts` to not need the slot name as well
- Change the attribute names `topMost`, `textOpen`, and `textClosed` to `topmost`, `text-open`, and
  `text-closed`, respectively.
  - Change locations in the code where those are used to correspond

** Why interfaces: **

Provides another check on the input/output boundaries of our elements, gives Storybook and
WebdriverIO another validation to check, and guarantees any rendering functions cannot be passed
invalid property names.

** Why functions for rendering: **

Providing functions for rendering gets us one step closer to dynamically defining our forms-in-code
at runtime without losing any type safety.

** Why rename the attributes: **

A *very* subtle bug:
[Element:setAttribute()](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute)
automatically "converts an attribute name to all lower-case when called on an HTML element in an
HTML document." The three attributes renamed are all treated *as* attributes, either classic boolean
or stringly-typed attributes, and attempting to manipulate them with `setAttribute()` will fail.

All of these attributes are presentational; none of them end up in a transaction with the back-end,
so kebab-to-camel conversions are not a concern.

Also, ["topmost" is one word](https://www.merriam-webster.com/dictionary/topmost).

** Why remove the slot name: **

Because there was only one slot.  A name is not needed.

* Fix minor spelling error.
2024-10-10 15:14:29 -07:00

102 lines
2.6 KiB
TypeScript

import { AKElement } from "@goauthentik/elements/Base";
import { type SlottedTemplateResult, type Spread } from "@goauthentik/elements/types";
import { spread } from "@open-wc/lit-helpers";
import { html, nothing } from "lit";
import { customElement, property } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import PFAlert from "@patternfly/patternfly/components/Alert/alert.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
export enum Level {
Warning = "pf-m-warning",
Info = "pf-m-info",
Success = "pf-m-success",
Danger = "pf-m-danger",
}
export const levelNames = ["warning", "info", "success", "danger"];
export type Levels = (typeof levelNames)[number];
export interface IAlert {
inline?: boolean;
plain?: boolean;
icon?: string;
level?: string;
}
/**
* @class Alert
* @element ak-alert
*
* Alerts are in-page elements intended to draw the user's attention and alert them to important
* details. Alerts are used alongside form elements to warn users of potential mistakes they can
* make, as well as in in-line documentation.
*/
@customElement("ak-alert")
export class Alert extends AKElement implements IAlert {
/**
* Whether or not to display the entire component's contents in-line or not.
*
* @attr
*/
@property({ type: Boolean })
inline = false;
@property({ type: Boolean })
plain = false;
/**
* Method of determining severity
*
* @attr
*/
@property()
level: Level | Levels = Level.Warning;
/**
* Icon to display
*
* @attr
*/
@property()
icon = "fa-exclamation-circle";
static get styles() {
return [PFBase, PFAlert];
}
get classmap() {
const level = levelNames.includes(this.level)
? `pf-m-${this.level}`
: (this.level as string);
return {
"pf-c-alert": true,
"pf-m-inline": this.inline,
"pf-m-plain": this.plain,
[level]: true,
};
}
render() {
return html`<div class="${classMap(this.classmap)}">
<div class="pf-c-alert__icon">
<i class="fas ${this.icon}"></i>
</div>
<h4 class="pf-c-alert__title"><slot></slot></h4>
</div>`;
}
}
export function akAlert(properties: IAlert, content: SlottedTemplateResult = nothing) {
const message = typeof content === "string" ? html`<span>${content}</span>` : content;
return html`<ak-alert ${spread(properties as Spread)}>${message}</ak-alert>`;
}
declare global {
interface HTMLElementTagNameMap {
"ak-alert": Alert;
}
}