-
-
Notifications
You must be signed in to change notification settings - Fork 396
[Autocomplete] clear tomselect instance to avoid double initialization #3155
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: 2.x
Are you sure you want to change the base?
Changes from all commits
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,42 @@ | ||
| <?php | ||
|
|
||
| namespace App\Form\Type; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
|
|
||
| class AutocompleteSelectType extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| $builder->add( | ||
| 'favorite_fruit', | ||
| ChoiceType::class, | ||
| [ | ||
| 'choices' => [ | ||
| 'Apple' => 'apple', | ||
| 'Banana' => 'banana', | ||
| 'Cherry' => 'cherry', | ||
| 'Coconut' => 'coconut', | ||
| 'Grape' => 'grape', | ||
| 'Kiwi' => 'kiwi', | ||
| 'Lemon' => 'lemon', | ||
| 'Mango' => 'mango', | ||
| 'Orange' => 'orange', | ||
| 'Papaya' => 'papaya', | ||
| 'Peach' => 'peach', | ||
| 'Pineapple' => 'pineapple', | ||
| 'Pear' => 'pear', | ||
| 'Pomegranate' => 'pomegranate', | ||
| 'Pomelo' => 'pomelo', | ||
| 'Raspberry' => 'raspberry', | ||
| 'Strawberry' => 'strawberry', | ||
| 'Watermelon' => 'watermelon', | ||
| ], | ||
| 'autocomplete' => true, | ||
| 'label' => 'Your favorite fruit:' | ||
| ] | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| {% extends 'example.html.twig' %} | ||
|
|
||
| {% block example %}{% endblock %} | ||
| {% block example %} | ||
| Autocomplete: | ||
| {{ form_start(form) }} | ||
| {{ form_widget(form.favorite_fruit) }} | ||
| {{ form_end(form) }} | ||
| {% endblock %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ export default class extends Controller { | |
| declare readonly tomSelectOptionsValue: object; | ||
| declare readonly hasPreloadValue: boolean; | ||
| declare readonly preloadValue: string; | ||
| tomSelect: TomSelect; | ||
| tomSelect: TomSelect | undefined; | ||
|
|
||
| private mutationObserver: MutationObserver; | ||
| private isObserving = false; | ||
|
|
@@ -98,6 +98,10 @@ export default class extends Controller { | |
| disconnect() { | ||
| this.stopMutationObserver(); | ||
|
|
||
| if (!this.tomSelect) { | ||
| return; | ||
| } | ||
|
|
||
| // TomSelect.destroy() resets the element to its original HTML. This | ||
| // causes the selected value to be lost. We store it. | ||
| let currentSelectedValues: string[] = []; | ||
|
|
@@ -114,6 +118,7 @@ export default class extends Controller { | |
| } | ||
|
|
||
| this.tomSelect.destroy(); | ||
| this.tomSelect = undefined; | ||
|
|
||
| if (this.selectElement) { | ||
| if (this.selectElement.multiple) { | ||
|
|
@@ -163,11 +168,15 @@ export default class extends Controller { | |
| plugins, | ||
| // clear the text input after selecting a value | ||
| onItemAdd: () => { | ||
| this.tomSelect.setTextboxValue(''); | ||
| this.tomSelect?.setTextboxValue(''); | ||
| }, | ||
| closeAfterSelect: true, | ||
| // fix positioning (in the dropdown) of options added through addOption() | ||
| onOptionAdd: (value: string, data: { [key: string]: any }) => { | ||
| if (!this.tomSelect) { | ||
| return; | ||
| } | ||
|
Comment on lines
+176
to
+178
Member
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. same thing
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. See above. |
||
|
|
||
| let parentElement = this.tomSelect.input as Element; | ||
| let optgroupData = null; | ||
|
|
||
|
|
@@ -232,10 +241,10 @@ export default class extends Controller { | |
| const config = this.#mergeConfigs(commonConfig, { | ||
| maxOptions: this.getMaxOptions(), | ||
| score: (search: string) => { | ||
| const scoringFunction = this.tomSelect.getScoreFunction(search); | ||
| const scoringFunction = this.tomSelect?.getScoreFunction(search); | ||
| return (item: any) => { | ||
| // strip HTML tags from each option's searchable text | ||
| return scoringFunction({ ...item, text: this.#stripTags(item[labelField]) }); | ||
| return scoringFunction?.({ ...item, text: this.#stripTags(item[labelField]) }); | ||
| }; | ||
| }, | ||
| render: { | ||
|
|
@@ -295,7 +304,7 @@ export default class extends Controller { | |
| }, | ||
| optgroupField: 'group_by', | ||
| // avoid extra filtering after results are returned | ||
| score: (search: string) => (item: any) => 1, | ||
| score: (_search: string) => (_item: any) => 1, | ||
|
Member
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. why this change ? genuine question here
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. The underscore is used to indicate an unused variable while making the linter happy.
Member
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. Was he unhappy ? never observed that .. but ok :)
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. 🤷🏻♂️ It's not easy being a linter I guess. |
||
| render: { | ||
| option: (item: any) => `<div>${item[labelField]}</div>`, | ||
| item: (item: any) => `<div>${item[labelField]}</div>`, | ||
|
|
@@ -439,6 +448,10 @@ export default class extends Controller { | |
| } | ||
|
|
||
| private changeTomSelectDisabledState(isDisabled: boolean): void { | ||
| if (!this.tomSelect) { | ||
| return; | ||
| } | ||
|
|
||
|
Comment on lines
+451
to
+454
Member
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. Same ting, not supposed to happen here
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. Will be reverted. |
||
| this.stopMutationObserver(); | ||
| if (isDisabled) { | ||
| this.tomSelect.disable(); | ||
|
|
||
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.
This is not supposed to happen, is it ?
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.
We can make it more defensive when something like this happens.