-
Notifications
You must be signed in to change notification settings - Fork 78
fix(web-forms#864): performance for selects on large entity lists #1750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
22e3db8
4d500bd
e87be43
2d660ba
40c7aea
922bc97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,15 +14,12 @@ interface MultiselectDropdownProps { | |
| const t: Translate = inject(TRANSLATE)!; | ||
| const props = defineProps<MultiselectDropdownProps>(); | ||
|
|
||
| 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 { | ||
| value: option.value, | ||
| label: option.label.formatted, | ||
|
|
@@ -35,16 +32,26 @@ const selectValues = (values: readonly string[]) => { | |
| props.question.selectValues(values); | ||
| }; | ||
|
|
||
| const virtualScrollerOptions = computed(() => { | ||
| const isJSDOM = typeof navigator !== 'undefined' && navigator.userAgent.includes('jsdom'); | ||
| if (isJSDOM) { | ||
| return; | ||
| } | ||
| return { | ||
| itemSize: DEFAULT_PRIMEVUE_ITEM_HEIGHT | ||
| }; | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
| } | ||
|
|
||
| const selectedLabels = computed(() => { | ||
| const state = props.question.currentState; | ||
| return state.value.map((val) => { | ||
| const found = state.valueOptions.find((opt) => opt.value === val); | ||
| return found?.label.formatted; | ||
| return state.value.map((value) => { | ||
| const option = props.question.getValueOption(value); | ||
| return option?.label.formatted; | ||
| }); | ||
| }); | ||
| </script> | ||
|
|
@@ -68,6 +75,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')" | ||
| > | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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! | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this comment, it doesn't seem to help much for this case. |
||
| const valueOption = this.currentState.valueOptions.find((item) => { | ||
| return item.value === value; | ||
| }); | ||
|
|
||
| const valueOption = this.mapOptionsByValue().get(value); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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?