Browse Source

Merge pull request #2131 from steweg/feature/fix-select-behavior

Fixed select behavior in case of single with input
Falko Schindler 1 year ago
parent
commit
572d421636
2 changed files with 12 additions and 5 deletions
  1. 2 1
      nicegui/elements/select.js
  2. 10 4
      tests/test_select.py

+ 2 - 1
nicegui/elements/select.js

@@ -20,7 +20,7 @@ export default {
   },
   },
   methods: {
   methods: {
     filterFn(val, update, abort) {
     filterFn(val, update, abort) {
-      update(() => (this.filteredOptions = this.findFilteredOptions()));
+      update(() => (this.filteredOptions = val ? this.findFilteredOptions() : this.initialOptions));
     },
     },
     findFilteredOptions() {
     findFilteredOptions() {
       const needle = this.$el.querySelector("input[type=search]")?.value.toLocaleLowerCase();
       const needle = this.$el.querySelector("input[type=search]")?.value.toLocaleLowerCase();
@@ -30,6 +30,7 @@ export default {
     },
     },
   },
   },
   updated() {
   updated() {
+    if (!this.$attrs.multiple) return;
     const newFilteredOptions = this.findFilteredOptions();
     const newFilteredOptions = this.findFilteredOptions();
     if (newFilteredOptions.length !== this.filteredOptions.length) {
     if (newFilteredOptions.length !== this.filteredOptions.length) {
       this.filteredOptions = newFilteredOptions;
       this.filteredOptions = newFilteredOptions;

+ 10 - 4
tests/test_select.py

@@ -142,8 +142,9 @@ def test_add_new_values(screen:  Screen, option_dict: bool, multiple: bool, new_
                                   "options = ['a', 'b', 'c']")
                                   "options = ['a', 'b', 'c']")
 
 
 
 
-def test_keep_filtered_options(screen: Screen):
-    ui.select(options=['A1', 'A2', 'B1', 'B2'], with_input=True, multiple=True)
+@pytest.mark.parametrize('multiple', [False, True])
+def test_keep_filtered_options(multiple: bool, screen: Screen):
+    ui.select(options=['A1', 'A2', 'B1', 'B2'], with_input=True, multiple=multiple)
 
 
     screen.open('/')
     screen.open('/')
     screen.find_by_tag('input').click()
     screen.find_by_tag('input').click()
@@ -161,7 +162,12 @@ def test_keep_filtered_options(screen: Screen):
 
 
     screen.click('A1')
     screen.click('A1')
     screen.wait(0.5)
     screen.wait(0.5)
+    screen.find_by_tag('input').click()
     screen.should_contain('A1')
     screen.should_contain('A1')
     screen.should_contain('A2')
     screen.should_contain('A2')
-    screen.should_not_contain('B1')
-    screen.should_not_contain('B2')
+    if multiple:
+        screen.should_not_contain('B1')
+        screen.should_not_contain('B2')
+    else:
+        screen.should_contain('B1')
+        screen.should_contain('B2')