![gcp-cherry-pick-bot[bot]](/assets/img/avatar_default.png)
web: fix value handling inside controlled components (#9648) * web: fix esbuild issue with style sheets Getting ESBuild, Lit, and Storybook to all agree on how to read and parse stylesheets is a serious pain. This fix better identifies the value types (instances) being passed from various sources in the repo to the three *different* kinds of style processors we're using (the native one, the polyfill one, and whatever the heck Storybook does internally). Falling back to using older CSS instantiating techniques one era at a time seems to do the trick. It's ugly, but in the face of the aggressive styling we use to avoid Flashes of Unstyled Content (FLoUC), it's the logic with which we're left. In standard mode, the following warning appears on the console when running a Flow: ``` Autofocus processing was blocked because a document already has a focused element. ``` In compatibility mode, the following **error** appears on the console when running a Flow: ``` crawler-inject.js:1106 Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. at initDomMutationObservers (crawler-inject.js:1106:18) at crawler-inject.js:1114:24 at Array.forEach (<anonymous>) at initDomMutationObservers (crawler-inject.js:1114:10) at crawler-inject.js:1549:1 initDomMutationObservers @ crawler-inject.js:1106 (anonymous) @ crawler-inject.js:1114 initDomMutationObservers @ crawler-inject.js:1114 (anonymous) @ crawler-inject.js:1549 ``` Despite this error, nothing seems to be broken and flows work as anticipated. * web: fix value handling inside controlled components This is one of those stupid bugs that drive web developers crazy. The basics are straightforward: when you cause a higher-level component to have a "big enough re-render," for some unknown definition of "big enough," it will re-render the sub-components. In traditional web interaction, those components should never be re-rendered while the user is interacting with the form, but in frameworks where there's dynamic re-arrangement, part or all of the form could get re-rendered at any mmoment. Since neither the form nor any of its intermediaries is tracking the values as they're changed, it's up to the components themselves to keep the user's input-- and to be hardened against property changes coming from the outside world. So static memoization of the initial value passed in, and aggressively walling off the values the customer generates from that field, are needed to protect the user's work from any framework's dynamic DOM management. I remember struggling with this in React; I had hoped Lit was better, but in this case, not better enough. The protocol for "is it an ak-data-control" is "it has a `json()` method that returns the data ready to be sent to the authentik server." I missed that in one place, so that's on me. * Eslint had opinions. * Added comments to explain something. Co-authored-by: Ken Sternberg <133134217+kensternberg-authentik@users.noreply.github.com>
151 lines
4.9 KiB
TypeScript
151 lines
4.9 KiB
TypeScript
import { AKElement } from "@goauthentik/elements/Base";
|
|
import { debounce } from "@goauthentik/elements/utils/debounce";
|
|
import { CustomListenerElement } from "@goauthentik/elements/utils/eventEmitter";
|
|
|
|
import { msg } from "@lit/localize";
|
|
import { PropertyValues, html } from "lit";
|
|
import { customElement, property, state } from "lit/decorators.js";
|
|
import { createRef, ref } from "lit/directives/ref.js";
|
|
import type { Ref } from "lit/directives/ref.js";
|
|
|
|
import type { Pagination } from "@goauthentik/api";
|
|
|
|
import "./ak-dual-select";
|
|
import { AkDualSelect } from "./ak-dual-select";
|
|
import type { DataProvider, DualSelectPair } from "./types";
|
|
|
|
/**
|
|
* @element ak-dual-select-provider
|
|
*
|
|
* A top-level component that understands how the authentik pagination interface works,
|
|
* and can provide new pages based upon navigation requests. This is the interface
|
|
* between authentik and the generic ak-dual-select component; aside from knowing that
|
|
* the Pagination object "looks like Django," the interior components don't know anything
|
|
* about authentik at all and could be dropped into Gravity unchanged.)
|
|
*
|
|
*/
|
|
|
|
@customElement("ak-dual-select-provider")
|
|
export class AkDualSelectProvider extends CustomListenerElement(AKElement) {
|
|
/** A function that takes a page and returns the DualSelectPair[] collection with which to update
|
|
* the "Available" pane.
|
|
*/
|
|
@property({ type: Object })
|
|
provider!: DataProvider;
|
|
|
|
@property({ type: Array })
|
|
selected: DualSelectPair[] = [];
|
|
|
|
@property({ attribute: "available-label" })
|
|
availableLabel = msg("Available options");
|
|
|
|
@property({ attribute: "selected-label" })
|
|
selectedLabel = msg("Selected options");
|
|
|
|
/** The remote lists are debounced by definition. This is the interval for the debounce. */
|
|
@property({ attribute: "search-delay", type: Number })
|
|
searchDelay = 250;
|
|
|
|
@state()
|
|
private options: DualSelectPair[] = [];
|
|
|
|
private dualSelector: Ref<AkDualSelect> = createRef();
|
|
|
|
private isLoading = false;
|
|
|
|
private doneFirstUpdate = false;
|
|
private internalSelected: DualSelectPair[] = [];
|
|
|
|
private pagination?: Pagination;
|
|
|
|
constructor() {
|
|
super();
|
|
setTimeout(() => this.fetch(1), 0);
|
|
// Notify AkForElementHorizontal how to handle this thing.
|
|
this.dataset.akControl = "true";
|
|
this.onNav = this.onNav.bind(this);
|
|
this.onChange = this.onChange.bind(this);
|
|
this.onSearch = this.onSearch.bind(this);
|
|
this.addCustomListener("ak-pagination-nav-to", this.onNav);
|
|
this.addCustomListener("ak-dual-select-change", this.onChange);
|
|
this.addCustomListener("ak-dual-select-search", this.onSearch);
|
|
}
|
|
|
|
willUpdate(changedProperties: PropertyValues<this>) {
|
|
if (changedProperties.has("selected") && !this.doneFirstUpdate) {
|
|
this.doneFirstUpdate = true;
|
|
this.internalSelected = this.selected;
|
|
}
|
|
|
|
if (changedProperties.has("searchDelay")) {
|
|
this.doSearch = debounce(
|
|
AkDualSelectProvider.prototype.doSearch.bind(this),
|
|
this.searchDelay,
|
|
);
|
|
}
|
|
|
|
if (changedProperties.has("provider")) {
|
|
this.pagination = undefined;
|
|
this.fetch();
|
|
}
|
|
}
|
|
|
|
async fetch(page?: number, search = "") {
|
|
if (this.isLoading) {
|
|
return;
|
|
}
|
|
this.isLoading = true;
|
|
const goto = page ?? this.pagination?.current ?? 1;
|
|
const data = await this.provider(goto, search);
|
|
this.pagination = data.pagination;
|
|
this.options = data.options;
|
|
this.isLoading = false;
|
|
}
|
|
|
|
onNav(event: Event) {
|
|
if (!(event instanceof CustomEvent)) {
|
|
throw new Error(`Expecting a CustomEvent for navigation, received ${event} instead`);
|
|
}
|
|
this.fetch(event.detail);
|
|
}
|
|
|
|
onChange(event: Event) {
|
|
if (!(event instanceof CustomEvent)) {
|
|
throw new Error(`Expecting a CustomEvent for change, received ${event} instead`);
|
|
}
|
|
this.internalSelected = event.detail.value;
|
|
this.selected = this.internalSelected;
|
|
}
|
|
|
|
onSearch(event: Event) {
|
|
if (!(event instanceof CustomEvent)) {
|
|
throw new Error(`Expecting a CustomEvent for change, received ${event} instead`);
|
|
}
|
|
this.doSearch(event.detail);
|
|
}
|
|
|
|
doSearch(search: string) {
|
|
this.pagination = undefined;
|
|
this.fetch(undefined, search);
|
|
}
|
|
|
|
get value() {
|
|
return this.dualSelector.value!.selected.map(([k, _]) => k);
|
|
}
|
|
|
|
json() {
|
|
return this.value;
|
|
}
|
|
|
|
render() {
|
|
return html`<ak-dual-select
|
|
${ref(this.dualSelector)}
|
|
.options=${this.options}
|
|
.pages=${this.pagination}
|
|
.selected=${this.internalSelected}
|
|
available-label=${this.availableLabel}
|
|
selected-label=${this.selectedLabel}
|
|
></ak-dual-select>`;
|
|
}
|
|
}
|