web: use custom-element as wrapper
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
62
web/src/elements/forms/HorizontalFormElement.ts
Normal file
62
web/src/elements/forms/HorizontalFormElement.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { customElement, LitElement, CSSResult, property, css } from "lit-element";
|
||||
import { TemplateResult, html } from "lit-html";
|
||||
import PFForm from "@patternfly/patternfly/components/Form/form.css";
|
||||
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
|
||||
|
||||
@customElement("ak-form-element-horizontal")
|
||||
export class HorizontalFormElement extends LitElement {
|
||||
|
||||
static get styles(): CSSResult[] {
|
||||
return [PFForm, PFFormControl, css`
|
||||
slot {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
}
|
||||
.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);
|
||||
}
|
||||
`];
|
||||
}
|
||||
|
||||
@property()
|
||||
label: string = "";
|
||||
|
||||
@property({ type: Boolean })
|
||||
required = false;
|
||||
|
||||
@property()
|
||||
errorMessage: string = "";
|
||||
|
||||
@property()
|
||||
invalid: boolean = false;
|
||||
|
||||
updated(): void {
|
||||
this.querySelectorAll<HTMLInputElement>("input[autofocus]").forEach(input => {
|
||||
input.focus();
|
||||
});
|
||||
}
|
||||
|
||||
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>
|
||||
${this.required ? html`<span class="pf-c-form__label-required" aria-hidden="true">*</span>` : html``}
|
||||
</label>
|
||||
</div>
|
||||
<div class="pf-c-form__group-control">
|
||||
<div class="pf-c-form__horizontal-group">
|
||||
<slot></slot>
|
||||
${this.invalid ? html`<p class="pf-c-form__helper-text pf-m-error" aria-live="polite">${this.errorMessage}</p>` : html``}
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import { TemplateResult, html } from "lit-html";
|
||||
|
||||
export function formGroup(label: string, body: TemplateResult): 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">${label}</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="pf-c-form__group-control">
|
||||
<div class="pf-c-form__horizontal-group">
|
||||
${body}
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
@ -5,8 +5,8 @@ import { html, TemplateResult } from "lit-html";
|
||||
import { DEFAULT_CONFIG } from "../../api/Config";
|
||||
import { Form } from "../../elements/forms/Form";
|
||||
import { until } from "lit-html/directives/until";
|
||||
import { formGroup } from "../../elements/forms/utils";
|
||||
import { ifDefined } from "lit-html/directives/if-defined";
|
||||
import "../../elements/forms/HorizontalFormElement";
|
||||
|
||||
@customElement("ak-group-form")
|
||||
export class GroupForm extends Form<Group> {
|
||||
@ -31,42 +31,42 @@ export class GroupForm extends Form<Group> {
|
||||
|
||||
renderForm(): TemplateResult {
|
||||
return html`<form class="pf-c-form pf-m-horizontal">
|
||||
${formGroup(gettext("Name"), html`
|
||||
<input type="text" name="name" value="${ifDefined(this.group?.name)}" class="pf-c-form-control" required="">
|
||||
`)}
|
||||
${formGroup("", html`
|
||||
<div class="pf-c-check">
|
||||
<input type="checkbox" name="is_superuser" class="pf-c-check__input" ?checked=${this.group?.isSuperuser || false}>
|
||||
<label class="pf-c-check__label">
|
||||
${gettext("Is superuser")}
|
||||
</label>
|
||||
</div>
|
||||
<p class="pf-c-form__helper-text">${gettext("Users added to this group will be superusers.")}</p>
|
||||
`)}
|
||||
${formGroup(gettext("Parent"), html`
|
||||
<select name="parent" class="pf-c-form-control">
|
||||
<option value="" ?selected=${this.group?.parent === undefined}>---------</option>
|
||||
${until(new CoreApi(DEFAULT_CONFIG).coreGroupsList({}).then(groups => {
|
||||
return groups.results.map(group => {
|
||||
return html`<option value=${ifDefined(group.pk)} ?selected=${this.group?.parent === group.pk}>${group.name}</option>`;
|
||||
<ak-form-element-horizontal label=${gettext("Name")} ?required=${true}>
|
||||
<input type="text" name="name" value="${ifDefined(this.group?.name)}" class="pf-c-form-control" required="">
|
||||
</ak-form-element-horizontal>
|
||||
<ak-form-element-horizontal ?required=${true}>
|
||||
<div class="pf-c-check">
|
||||
<input type="checkbox" name="is_superuser" class="pf-c-check__input" ?checked=${this.group?.isSuperuser || false}>
|
||||
<label class="pf-c-check__label">
|
||||
${gettext("Is superuser")}
|
||||
</label>
|
||||
</div>
|
||||
<p class="pf-c-form__helper-text">${gettext("Users added to this group will be superusers.")}</p>
|
||||
</ak-form-element-horizontal>
|
||||
<ak-form-element-horizontal label=${gettext("Parent")} ?required=${true}>
|
||||
<select name="parent" class="pf-c-form-control">
|
||||
<option value="" ?selected=${this.group?.parent === undefined}>---------</option>
|
||||
${until(new CoreApi(DEFAULT_CONFIG).coreGroupsList({}).then(groups => {
|
||||
return groups.results.map(group => {
|
||||
return html`<option value=${ifDefined(group.pk)} ?selected=${this.group?.parent === group.pk}>${group.name}</option>`;
|
||||
});
|
||||
}), html``)}
|
||||
</select>
|
||||
</ak-form-element-horizontal>
|
||||
<ak-form-element-horizontal label=${gettext("Members")} ?required=${true}>
|
||||
<select name="users" class="pf-c-form-control" multiple="">
|
||||
${until(new CoreApi(DEFAULT_CONFIG).coreUsersList({}).then(users => {
|
||||
return users.results.map(user => {
|
||||
const selected = Array.from(this.group?.users || []).some(su => {
|
||||
return su == user.pk;
|
||||
});
|
||||
}), html``)}
|
||||
</select>
|
||||
`)}
|
||||
${formGroup(gettext("Members"), html`
|
||||
<select name="users" class="pf-c-form-control" multiple="">
|
||||
${until(new CoreApi(DEFAULT_CONFIG).coreUsersList({}).then(users => {
|
||||
return users.results.map(user => {
|
||||
const selected = Array.from(this.group?.users || []).some(su => {
|
||||
return su == user.pk;
|
||||
});
|
||||
return html`<option value=${ifDefined(user.pk)} ?selected=${selected}>${user.username}</option>`;
|
||||
});
|
||||
}))}
|
||||
</select>
|
||||
<p class="pf-c-form__helper-text">${gettext("Hold control/command to select multiple items.")}</p>
|
||||
`)}
|
||||
</form>`;
|
||||
return html`<option value=${ifDefined(user.pk)} ?selected=${selected}>${user.username}</option>`;
|
||||
});
|
||||
}))}
|
||||
</select>
|
||||
<p class="pf-c-form__helper-text">${gettext("Hold control/command to select multiple items.")}</p>
|
||||
</ak-form-element-horizontal>
|
||||
</form>`;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user