Skip to content
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

[Autocomplete] Fix 2 bugs where TomSelect would reset when not necessary #1502

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions src/Autocomplete/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class default_1 extends Controller {
}
resetTomSelect() {
if (this.tomSelect) {
this.dispatchEvent('before-reset', { tomSelect: this.tomSelect });
this.stopMutationObserver();
const currentHtml = this.element.innerHTML;
const currentValue = this.tomSelect.getValue();
Expand Down Expand Up @@ -143,6 +144,7 @@ class default_1 extends Controller {
subtree: true,
attributes: true,
characterData: true,
attributeOldValue: true,
});
this.isObserving = true;
}
Expand All @@ -164,7 +166,11 @@ class default_1 extends Controller {
break;
}
if (mutation.target === this.element && mutation.attributeName === 'multiple') {
requireReset = true;
const isNowMultiple = this.element.hasAttribute('multiple');
const wasMultiple = mutation.oldValue === 'multiple';
if (isNowMultiple !== wasMultiple) {
requireReset = true;
}
break;
}
break;
Expand All @@ -191,12 +197,14 @@ class default_1 extends Controller {
});
}
areOptionsEquivalent(newOptions) {
if (this.originalOptions.length !== newOptions.length) {
const filteredOriginalOptions = this.originalOptions.filter((option) => option.value !== '');
const filteredNewOptions = newOptions.filter((option) => option.value !== '');
if (filteredOriginalOptions.length !== filteredNewOptions.length) {
return false;
}
const normalizeOption = (option) => `${option.value}-${option.text}-${option.group}`;
const originalOptionsSet = new Set(this.originalOptions.map(normalizeOption));
const newOptionsSet = new Set(newOptions.map(normalizeOption));
const originalOptionsSet = new Set(filteredOriginalOptions.map(normalizeOption));
const newOptionsSet = new Set(filteredNewOptions.map(normalizeOption));
return (originalOptionsSet.size === newOptionsSet.size &&
[...originalOptionsSet].every((option) => newOptionsSet.has(option)));
}
Expand Down
18 changes: 14 additions & 4 deletions src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export default class extends Controller {

private resetTomSelect(): void {
if (this.tomSelect) {
this.dispatchEvent('before-reset', { tomSelect: this.tomSelect });
this.stopMutationObserver();

// Grab the current HTML then restore it after destroying TomSelect
Expand Down Expand Up @@ -358,6 +359,7 @@ export default class extends Controller {
subtree: true,
attributes: true,
characterData: true,
attributeOldValue: true,
});
this.isObserving = true;
}
Expand All @@ -384,7 +386,11 @@ export default class extends Controller {
}

if (mutation.target === this.element && mutation.attributeName === 'multiple') {
requireReset = true;
const isNowMultiple = this.element.hasAttribute('multiple');
const wasMultiple = mutation.oldValue === 'multiple';
if (isNowMultiple !== wasMultiple) {
requireReset = true;
}

break;
}
Expand Down Expand Up @@ -419,14 +425,18 @@ export default class extends Controller {
}

private areOptionsEquivalent(newOptions: Array<{ value: string; text: string; group: string | null }>): boolean {
if (this.originalOptions.length !== newOptions.length) {
// remove the empty option, which is added by TomSelect so may be missing from new options
const filteredOriginalOptions = this.originalOptions.filter((option) => option.value !== '');
const filteredNewOptions = newOptions.filter((option) => option.value !== '');

if (filteredOriginalOptions.length !== filteredNewOptions.length) {
return false;
}

const normalizeOption = (option: { value: string; text: string; group: string | null }) =>
`${option.value}-${option.text}-${option.group}`;
const originalOptionsSet = new Set(this.originalOptions.map(normalizeOption));
const newOptionsSet = new Set(newOptions.map(normalizeOption));
const originalOptionsSet = new Set(filteredOriginalOptions.map(normalizeOption));
const newOptionsSet = new Set(filteredNewOptions.map(normalizeOption));

return (
originalOptionsSet.size === newOptionsSet.size &&
Expand Down
42 changes: 42 additions & 0 deletions src/Autocomplete/assets/test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,46 @@ describe('AutocompleteController', () => {
});
expect(getSelectedValues()).toEqual(['2', '3']);
});

it('does not trigger a reset when the style of "multiple" attribute changes', async () => {
const { container } = await startAutocompleteTest(`
<select multiple data-testid='main-element' data-controller='autocomplete'>
<option value=''>Select dogs</option>
<option value='1'>dog1</option>
<option value='2'>dog2</option>
<option value='3'>dog3</option>
</select>
`);

let wasReset = false;
container.addEventListener('autocomplete:before-reset', () => {
wasReset = true;
});

const selectElement = getByTestId(container, 'main-element') as HTMLSelectElement;
selectElement.setAttribute('multiple', 'multiple');
// wait for the mutation observe
await shortDelay(10);
expect(wasReset).toBe(false);
});

it('does not trigger a reset based on the extra, empty select', async () => {
const { container, tomSelect } = await startAutocompleteTest(`
<select data-testid='main-element' data-controller='autocomplete'>
<option value='1'>dog1</option>
<option value='2'>dog2</option>
<option value='3'>dog3</option>
</select>
`);

let wasReset = false;
container.addEventListener('autocomplete:before-reset', () => {
wasReset = true;
});

tomSelect.addItem('2');
// wait for the mutation observe
await shortDelay(10);
expect(wasReset).toBe(false);
});
});
Loading