2021-09-21 09:19:26 +00:00
|
|
|
import { LitElement, CSSResult, css } from "lit";
|
|
|
|
import { customElement, property } from "lit/decorators";
|
|
|
|
import { TemplateResult, html } from "lit";
|
2021-04-04 18:07:46 +00:00
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
2021-03-29 08:09:43 +00:00
|
|
|
import PFForm from "@patternfly/patternfly/components/Form/form.css";
|
|
|
|
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
|
2021-04-04 18:07:46 +00:00
|
|
|
import AKGlobal from "../../authentik.css";
|
|
|
|
import { t } from "@lingui/macro";
|
2021-08-21 13:24:31 +00:00
|
|
|
import { FormGroup } from "./FormGroup";
|
2021-03-29 08:09:43 +00:00
|
|
|
|
|
|
|
@customElement("ak-form-element-horizontal")
|
|
|
|
export class HorizontalFormElement extends LitElement {
|
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
PFBase,
|
|
|
|
PFForm,
|
|
|
|
PFFormControl,
|
|
|
|
AKGlobal,
|
|
|
|
css`
|
|
|
|
.pf-c-form__group {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns:
|
|
|
|
var(--pf-c-form--m-horizontal__group-label--md--GridColumnWidth)
|
|
|
|
var(--pf-c-form--m-horizontal__group-control--md--GridColumnWidth);
|
|
|
|
}
|
|
|
|
.pf-c-form__group-label {
|
|
|
|
padding-top: var(--pf-c-form--m-horizontal__group-label--md--PaddingTop);
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
2021-03-29 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property()
|
2021-03-29 10:16:32 +00:00
|
|
|
label = "";
|
2021-03-29 08:09:43 +00:00
|
|
|
|
|
|
|
@property({ type: Boolean })
|
|
|
|
required = false;
|
|
|
|
|
2021-04-04 18:07:46 +00:00
|
|
|
@property({ type: Boolean })
|
|
|
|
writeOnly = false;
|
|
|
|
|
|
|
|
@property({ type: Boolean })
|
|
|
|
writeOnlyActivated = false;
|
|
|
|
|
2021-03-29 08:09:43 +00:00
|
|
|
@property()
|
2021-03-29 10:16:32 +00:00
|
|
|
errorMessage = "";
|
2021-03-29 08:09:43 +00:00
|
|
|
|
2021-08-21 13:24:31 +00:00
|
|
|
_invalid = false;
|
|
|
|
|
2021-03-29 10:16:32 +00:00
|
|
|
@property({ type: Boolean })
|
2021-08-21 13:24:31 +00:00
|
|
|
set invalid(v: boolean) {
|
|
|
|
this._invalid = v;
|
|
|
|
// check if we're in a form group, and expand that form group
|
|
|
|
const parent = this.parentElement?.parentElement;
|
|
|
|
if (parent && "expanded" in parent) {
|
|
|
|
(parent as FormGroup).expanded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get invalid(): boolean {
|
|
|
|
return this._invalid;
|
|
|
|
}
|
2021-03-29 08:09:43 +00:00
|
|
|
|
2021-03-29 16:22:15 +00:00
|
|
|
@property()
|
|
|
|
name = "";
|
|
|
|
|
2021-03-29 08:09:43 +00:00
|
|
|
updated(): void {
|
2021-08-03 15:52:21 +00:00
|
|
|
this.querySelectorAll<HTMLInputElement>("input[autofocus]").forEach((input) => {
|
2021-03-29 08:09:43 +00:00
|
|
|
input.focus();
|
|
|
|
});
|
2021-03-29 16:22:15 +00:00
|
|
|
this.querySelectorAll("*").forEach((input) => {
|
|
|
|
switch (input.tagName.toLowerCase()) {
|
|
|
|
case "input":
|
|
|
|
case "textarea":
|
|
|
|
case "select":
|
|
|
|
case "ak-codemirror":
|
2021-04-10 18:13:11 +00:00
|
|
|
case "ak-chip-group":
|
2021-03-29 16:22:15 +00:00
|
|
|
(input as HTMLInputElement).name = this.name;
|
|
|
|
break;
|
|
|
|
default:
|
2021-04-04 18:07:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.writeOnly && !this.writeOnlyActivated) {
|
2021-08-03 15:52:21 +00:00
|
|
|
const i = input as HTMLInputElement;
|
2021-04-04 18:07:46 +00:00
|
|
|
i.setAttribute("hidden", "true");
|
2021-04-04 18:11:57 +00:00
|
|
|
const handler = () => {
|
2021-04-04 18:07:46 +00:00
|
|
|
i.removeAttribute("hidden");
|
|
|
|
this.writeOnlyActivated = true;
|
|
|
|
i.parentElement?.removeEventListener("click", handler);
|
|
|
|
};
|
|
|
|
i.parentElement?.addEventListener("click", handler);
|
2021-03-29 16:22:15 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-29 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render(): TemplateResult {
|
|
|
|
return html`<div class="pf-c-form__group">
|
|
|
|
<div class="pf-c-form__group-label">
|
|
|
|
<label class="pf-c-form__label">
|
|
|
|
<span class="pf-c-form__label-text">${this.label}</span>
|
2021-08-03 15:52:21 +00:00
|
|
|
${this.required
|
|
|
|
? html`<span class="pf-c-form__label-required" aria-hidden="true">*</span>`
|
|
|
|
: html``}
|
2021-03-29 08:09:43 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div class="pf-c-form__group-control">
|
2021-08-03 15:52:21 +00:00
|
|
|
${this.writeOnly && !this.writeOnlyActivated
|
|
|
|
? html`<div class="pf-c-form__horizontal-group">
|
|
|
|
<input
|
|
|
|
class="pf-c-form-control"
|
|
|
|
type="password"
|
|
|
|
disabled
|
|
|
|
value="**************"
|
|
|
|
/>
|
|
|
|
</div>`
|
|
|
|
: html``}
|
2021-03-29 09:57:53 +00:00
|
|
|
<slot class="pf-c-form__horizontal-group"></slot>
|
2021-03-29 08:09:43 +00:00
|
|
|
<div class="pf-c-form__horizontal-group">
|
2021-08-03 15:52:21 +00:00
|
|
|
${this.writeOnly
|
|
|
|
? html`<p class="pf-c-form__helper-text" aria-live="polite">
|
|
|
|
${t`Click to change value`}
|
|
|
|
</p>`
|
|
|
|
: html``}
|
|
|
|
${this.invalid
|
|
|
|
? html`<p class="pf-c-form__helper-text pf-m-error" aria-live="polite">
|
|
|
|
${this.errorMessage}
|
|
|
|
</p>`
|
|
|
|
: html``}
|
2021-03-29 08:09:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
}
|
|
|
|
}
|