Fixes #7191: Access SlimSelect's internal options when getting current options so selection state is maintained

This commit is contained in:
thatmattlove
2021-09-08 09:54:38 -07:00
parent d40d1638af
commit 851f8a1585
5 changed files with 28 additions and 18 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -5,7 +5,7 @@ import SlimSelect from 'slim-select';
import { createToast } from '../../bs';
import { hasUrl, hasExclusions, isTrigger } from '../util';
import { DynamicParamsMap } from './dynamicParams';
import { isStaticParams } from './types';
import { isStaticParams, isOption } from './types';
import {
hasMore,
isTruthy,
@@ -156,11 +156,6 @@ export class APISelect {
*/
private preSorted: boolean = false;
/**
* This instance's available options.
*/
private _options: Option[] = [EMPTY_PLACEHOLDER];
/**
* Array of options values which should be considered disabled or static.
*/
@@ -295,7 +290,7 @@ export class APISelect {
* This instance's available options.
*/
private get options(): Option[] {
return this._options;
return this.slim.data.data.filter(isOption);
}
/**
@@ -329,7 +324,6 @@ export class APISelect {
// If there is not a placeholder, add one to the front.
deduplicated.unshift(this.emptyOption);
}
this._options = deduplicated;
this.slim.setData(deduplicated);
}
@@ -381,7 +375,12 @@ export class APISelect {
const fetcher = debounce((event: Event) => this.handleSearch(event), 300, false);
// Query the API when the input value changes or a value is pasted.
this.slim.slim.search.input.addEventListener('keyup', event => fetcher(event));
this.slim.slim.search.input.addEventListener('keyup', event => {
// Only search when necessary keys are pressed.
if (!event.key.match(/^(Arrow|Enter|Tab).*/)) {
return fetcher(event);
}
});
this.slim.slim.search.input.addEventListener('paste', event => fetcher(event));
// Watch every scroll event to determine if the scroll position is at bottom.
@@ -470,7 +469,7 @@ export class APISelect {
for (const result of data.results) {
let text = result.display;
if (typeof result._depth === 'number') {
if (typeof result._depth === 'number' && result._depth > 0) {
// If the object has a `_depth` property, indent its display text.
if (!this.preSorted) {
this.preSorted = true;

View File

@@ -1,4 +1,5 @@
import type { Stringifiable } from 'query-string';
import type { Option, Optgroup } from 'slim-select/dist/data';
/**
* Map of string keys to primitive array values accepted by `query-string`. Keys are used as
@@ -187,3 +188,12 @@ export function isStaticParams(value: unknown): value is DataStaticParam[] {
}
return false;
}
/**
* Type guard to determine if a SlimSelect `dataObject` is an `Option`.
*
* @param data Option or Option Group
*/
export function isOption(data: Option | Optgroup): data is Option {
return !('options' in data);
}