Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/young-waves-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@getodk/xforms-engine": patch
"@getodk/web-forms": patch
---

Fixed performance issues when rendering a select with options from a large entity list
55 changes: 48 additions & 7 deletions packages/web-forms/src/components/common/MultiselectDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { computed, inject } from 'vue';
import MarkdownBlock from './MarkdownBlock.vue';
import { TRANSLATE } from '@/lib/constants/injection-keys.ts';
import type { Translate } from '@/lib/locale/useLocale.ts';
import type { VirtualScrollerScrollIndexChangeEvent } from 'primevue';

interface MultiselectDropdownProps {
readonly question: SelectNode;
Expand All @@ -14,19 +15,24 @@ interface MultiselectDropdownProps {
const t: Translate = inject(TRANSLATE)!;
const props = defineProps<MultiselectDropdownProps>();

const INITIAL_PAGE_SIZE = 20;
const DEFAULT_PRIMEVUE_ITEM_HEIGHT = 38;

defineEmits(['update:modelValue', 'change']);

const options = computed(() => {
return props.question.currentState.valueOptions.map((option) => {
const label = props.question.getValueOption(option.value);
if (label == null) {
throw new Error(`Failed to find option for value: ${option.value}`);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why we needed this, but it massively slowed down the rendering, because it was doing a n^2 lookup. Do we still need this validation? Is there a better way to do it than on the client every time?

return props.question.currentState.valueOptions.map((option, i) => {
if (i < INITIAL_PAGE_SIZE) {
return {
value: option.value,
label: option.label.formatted,
search: option.label.asString,
loaded: true
};
}

return {
value: option.value,
label: option.label.formatted,
search: option.label.asString,
loaded: false

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a placeholder that's swapped out when the page is loaded.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The labels for options with index +20 won't be reactive anymore, right? So if a label with media takes time to load, or if a calc changes some values, is it refreshed properly?

What about these scenarios but with autocomplete feature? Finding an option with index +20 and media request is slow.

};
});
});
Expand All @@ -35,6 +41,40 @@ const selectValues = (values: readonly string[]) => {
props.question.selectValues(values);
};

const handleLazyLoad = (params: VirtualScrollerScrollIndexChangeEvent) => {
const { first, last } = params;
for (let i = first; i < last; i++) {
const placeholder = options.value[i];
if (!placeholder?.value || placeholder.loaded) {
continue;
}
const option = props.question.getValueOption(placeholder.value);
if (!option) {
// should never happen, but handle gracefully if it does
continue;
}
options.value[i] = {
value: option.value,
label: option.label.formatted,
search: option.label.asString,
loaded: true
};
}
};

const virtualScrollerOptions = computed(() => {
const isJSDOM = typeof navigator !== 'undefined' && navigator.userAgent.includes('jsdom');
if (isJSDOM) {
return;
}
return {
lazy: true,
onLazyLoad: handleLazyLoad,
itemSize: DEFAULT_PRIMEVUE_ITEM_HEIGHT,
showLoader: true
};
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately JSDOM completely breaks primevue's expectations, so to get the tests to pass I have to disable the lazy loading.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this is merged: #1755, this piece of code can be deleted. It's worth waiting a bit, since the other PRs are very close to merging.


let panelClass = 'multi-select-dropdown-panel';
if (props.question.appearances['no-buttons']) {
panelClass += ' no-buttons';
Expand Down Expand Up @@ -68,6 +108,7 @@ const selectedLabels = computed(() => {
option-label="search"
:panel-class="panelClass"
:model-value="question.currentState.value"
:virtual-scroller-options="virtualScrollerOptions"
@update:model-value="selectValues"
@change="$emit('change')"
>
Expand Down
58 changes: 49 additions & 9 deletions packages/web-forms/src/components/common/SearchableDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { computed, inject } from 'vue';
import MarkdownBlock from './MarkdownBlock.vue';
import { TRANSLATE } from '@/lib/constants/injection-keys.ts';
import type { Translate } from '@/lib/locale/useLocale.ts';
import type { VirtualScrollerScrollIndexChangeEvent } from 'primevue';

interface SearchableDropdownProps {
readonly question: SelectNode;
Expand All @@ -14,31 +15,69 @@ interface SearchableDropdownProps {
const t: Translate = inject(TRANSLATE)!;
const props = defineProps<SearchableDropdownProps>();

const INITIAL_PAGE_SIZE = 20;
const DEFAULT_PRIMEVUE_ITEM_HEIGHT = 38;

defineEmits(['update:modelValue', 'change']);

const options = computed(() => {
return props.question.currentState.valueOptions.map((option) => {
const label = props.question.getValueOption(option.value);
if (label == null) {
throw new Error(`Failed to find option for value: ${option.value}`);
return props.question.currentState.valueOptions.map((option, i) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to extract some of this to /lib to reuse code in both components?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to do that, but as this is likely going to be released as a patch I didn't want to risk destabalising anything else. After realising mutliselect is deprecated and should be replaced with the select widget I think the full solution will be to merge our multiselect and searchable dropdown components into one that does both, which means extracting to lib won't be necessary any more. Issue: getodk/web-forms#865

if (i < INITIAL_PAGE_SIZE) {
return {
value: option.value,
label: option.label.formatted,
search: option.label.asString,
loaded: true
};
}

return {
value: option.value,
loaded: false
};
});
});

const handleLazyLoad = (params: VirtualScrollerScrollIndexChangeEvent) => {
const { first, last } = params;
for (let i = first; i < last; i++) {
const placeholder = options.value[i];
if (!placeholder?.value || placeholder.loaded) {
continue;
}
const option = props.question.getValueOption(placeholder.value);
if (!option) {
// should never happen, but handle gracefully if it does
continue;
}
options.value[i] = {
value: option.value,
label: option.label.formatted,
search: option.label.asString,
loaded: true
};
});
}
};

const virtualScrollerOptions = computed(() => {
const isJSDOM = typeof navigator !== 'undefined' && navigator.userAgent.includes('jsdom');
if (isJSDOM) {
return;
}
return {
lazy: true,
onLazyLoad: handleLazyLoad,
itemSize: DEFAULT_PRIMEVUE_ITEM_HEIGHT,
showLoader: true
};
});

const selectedLabel = computed(() => {
const value = props.question.currentState?.value?.[0];
if (!value) {
return [];
}
const valueOptions = props.question.currentState.valueOptions;
const found = valueOptions.find((opt) => opt.value === value);
return found?.label.formatted;
const option = props.question.getValueOption(value);
return option?.label.formatted;
});

const selectValue = (value: string) => {
Expand All @@ -58,6 +97,7 @@ const selectValue = (value: string) => {
:options="options"
option-label="search"
option-value="value"
:virtual-scroller-options="virtualScrollerOptions"
@update:model-value="selectValue"
@change="$emit('change')"
>
Expand Down
5 changes: 1 addition & 4 deletions packages/xforms-engine/src/instance/SelectControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ export class SelectControl
getValueOption(value: string): SelectItem | null {
// Note: this method is a client-facing convenience API for reading state,
// so it **MUST** read from client-reactive state!
Comment thread
latin-panda marked this conversation as resolved.
Outdated
const valueOption = this.currentState.valueOptions.find((item) => {
return item.value === value;
});

const valueOption = this.mapOptionsByValue().get(value);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work, and is far faster than scanning through each of the valueOptions. The comment above is concerning though and makes me think I might have missed something.

return valueOption ?? null;
}

Expand Down
Loading