-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add itemClassNameGenerator to generate CSS class names (#6177)
- Loading branch information
1 parent
f60daad
commit 40db058
Showing
6 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ests/src/main/java/com/vaadin/flow/component/combobox/test/ComboBoxItemClassNamePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.combobox.test; | ||
|
||
import com.vaadin.flow.component.combobox.ComboBox; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("vaadin-combo-box/item-class-name") | ||
public class ComboBoxItemClassNamePage extends Div { | ||
|
||
public ComboBoxItemClassNamePage() { | ||
ComboBox<String> comboBox = new ComboBox<>(); | ||
comboBox.setItems("foo", "bar", "baz"); | ||
|
||
NativeButton setClassNameGenerator = new NativeButton( | ||
"Set class name generator", e -> { | ||
comboBox.setClassNameGenerator(item -> "item-" + item); | ||
}); | ||
setClassNameGenerator.setId("set-generator"); | ||
|
||
NativeButton resetClassNameGenerator = new NativeButton( | ||
"Reset class name generator", e -> { | ||
comboBox.setClassNameGenerator(item -> null); | ||
}); | ||
resetClassNameGenerator.setId("reset-generator"); | ||
|
||
add(comboBox, setClassNameGenerator, resetClassNameGenerator); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...in/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxItemClassNamePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.combobox.test; | ||
|
||
import com.vaadin.flow.component.combobox.MultiSelectComboBox; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("vaadin-multi-select-combo-box/item-class-name") | ||
public class MultiSelectComboBoxItemClassNamePage extends Div { | ||
|
||
public MultiSelectComboBoxItemClassNamePage() { | ||
MultiSelectComboBox<String> comboBox = new MultiSelectComboBox<>(); | ||
comboBox.setItems("foo", "bar", "baz"); | ||
|
||
// Make component wider, so that we can fit multiple chips | ||
comboBox.setWidth("300px"); | ||
|
||
NativeButton setClassNameGenerator = new NativeButton( | ||
"Set class name generator", e -> { | ||
comboBox.setClassNameGenerator(item -> "item-" + item); | ||
}); | ||
setClassNameGenerator.setId("set-generator"); | ||
|
||
NativeButton resetClassNameGenerator = new NativeButton( | ||
"Reset class name generator", e -> { | ||
comboBox.setClassNameGenerator(item -> null); | ||
}); | ||
resetClassNameGenerator.setId("reset-generator"); | ||
|
||
NativeButton setValue = new NativeButton("Set value", e -> { | ||
comboBox.select("foo", "bar"); | ||
}); | ||
setValue.setId("set-value"); | ||
|
||
add(comboBox, setClassNameGenerator, resetClassNameGenerator, setValue); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...-tests/src/test/java/com/vaadin/flow/component/combobox/test/ComboBoxItemClassNameIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.combobox.test; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.combobox.testbench.ComboBoxElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
import com.vaadin.testbench.ElementQuery; | ||
import com.vaadin.testbench.TestBenchElement; | ||
|
||
@TestPath("vaadin-combo-box/item-class-name") | ||
public class ComboBoxItemClassNameIT extends AbstractComponentIT { | ||
|
||
private ComboBoxElement comboBox; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
comboBox = $(ComboBoxElement.class).first(); | ||
} | ||
|
||
@Test | ||
public void noClassesOnItemsSetInitially() { | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("", "", ""); | ||
} | ||
|
||
@Test | ||
public void setClassNameGenerator_classesGenerated() { | ||
click("set-generator"); | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("item-foo", "item-bar", "item-baz"); | ||
} | ||
|
||
@Test | ||
public void changeClassNameGeneratorToReturnNull_classesRemoved() { | ||
click("set-generator"); | ||
click("reset-generator"); | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("", "", ""); | ||
} | ||
|
||
private void assertItemClassNames(String... expectedClassNames) { | ||
TestBenchElement overlay = $("vaadin-combo-box-overlay").first(); | ||
ElementQuery<TestBenchElement> items = overlay | ||
.$("vaadin-combo-box-item"); | ||
|
||
for (int i = 0; i < expectedClassNames.length; i++) { | ||
Assert.assertEquals(items.get(i).getAttribute("class"), | ||
expectedClassNames[i]); | ||
} | ||
} | ||
|
||
private void click(String id) { | ||
$(TestBenchElement.class).id(id).click(); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...test/java/com/vaadin/flow/component/combobox/test/MultiSelectComboBoxItemClassNameIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.combobox.test; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.combobox.testbench.MultiSelectComboBoxElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
import com.vaadin.testbench.ElementQuery; | ||
import com.vaadin.testbench.TestBenchElement; | ||
|
||
@TestPath("vaadin-multi-select-combo-box/item-class-name") | ||
public class MultiSelectComboBoxItemClassNameIT extends AbstractComponentIT { | ||
|
||
private MultiSelectComboBoxElement comboBox; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
comboBox = $(MultiSelectComboBoxElement.class).first(); | ||
} | ||
|
||
@Test | ||
public void noClassesOnItemsSetInitially() { | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("", "", ""); | ||
} | ||
|
||
@Test | ||
public void setClassNameGenerator_itemClassesGenerated() { | ||
click("set-generator"); | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("item-foo", "item-bar", "item-baz"); | ||
} | ||
|
||
@Test | ||
public void changeClassNameGeneratorToReturnNull_itemClassesRemoved() { | ||
click("set-generator"); | ||
click("reset-generator"); | ||
comboBox.openPopup(); | ||
|
||
assertItemClassNames("", "", ""); | ||
} | ||
|
||
@Test | ||
public void setValue_noClassesOnChipsSetInitially() { | ||
click("set-value"); | ||
|
||
assertChipClassNames("", ""); | ||
} | ||
|
||
@Test | ||
public void setClassNameGenerator_setValue_chipClassesGenerated() { | ||
click("set-generator"); | ||
click("set-value"); | ||
|
||
assertChipClassNames("item-foo", "item-bar"); | ||
} | ||
|
||
@Test | ||
public void setValue_setClassNameGenerator_chipClassesGenerated() { | ||
click("set-value"); | ||
click("set-generator"); | ||
|
||
assertChipClassNames("item-foo", "item-bar"); | ||
} | ||
|
||
@Test | ||
public void changeClassNameGeneratorToReturnNull_chipClassesRemoved() { | ||
click("set-generator"); | ||
click("set-value"); | ||
|
||
click("reset-generator"); | ||
|
||
assertChipClassNames("", ""); | ||
} | ||
|
||
private void assertItemClassNames(String... expectedClassNames) { | ||
TestBenchElement overlay = $("vaadin-multi-select-combo-box-overlay") | ||
.first(); | ||
ElementQuery<TestBenchElement> items = overlay | ||
.$("vaadin-multi-select-combo-box-item"); | ||
|
||
for (int i = 0; i < expectedClassNames.length; i++) { | ||
Assert.assertEquals(items.get(i).getAttribute("class"), | ||
expectedClassNames[i]); | ||
} | ||
} | ||
|
||
private void assertChipClassNames(String... expectedClassNames) { | ||
ElementQuery<TestBenchElement> chips = comboBox | ||
.$("vaadin-multi-select-combo-box-chip"); | ||
|
||
for (int i = 0; i < expectedClassNames.length; i++) { | ||
// Skip first chip as it's used for overflow items | ||
Assert.assertEquals(chips.get(i + 1).getAttribute("class"), | ||
expectedClassNames[i]); | ||
} | ||
} | ||
|
||
private void click(String id) { | ||
$(TestBenchElement.class).id(id).click(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters