aboutsummaryrefslogtreecommitdiffstats
path: root/documentation/components/components-combobox.asciidoc
diff options
context:
space:
mode:
authorPekka Hyvönen <pekka@vaadin.com>2017-01-04 11:48:45 +0200
committerIlia Motornyi <elmot@vaadin.com>2017-01-04 11:48:45 +0200
commit6ef75bda79099953f85e1c48a0e833a6ed258acf (patch)
tree9098676e92e779bcd0a470f2363007efa0b7941c /documentation/components/components-combobox.asciidoc
parent7b9c6bd5d13cf9c004d1cb2d544683ed3e7b17e5 (diff)
downloadvaadin-framework-6ef75bda79099953f85e1c48a0e833a6ed258acf.tar.gz
vaadin-framework-6ef75bda79099953f85e1c48a0e833a6ed258acf.zip
Update general Component documentation
Part of vaadin/framework8-issues#538
Diffstat (limited to 'documentation/components/components-combobox.asciidoc')
-rw-r--r--documentation/components/components-combobox.asciidoc57
1 files changed, 51 insertions, 6 deletions
diff --git a/documentation/components/components-combobox.asciidoc b/documentation/components/components-combobox.asciidoc
index b5eb92de7e..ccd75a3dc5 100644
--- a/documentation/components/components-combobox.asciidoc
+++ b/documentation/components/components-combobox.asciidoc
@@ -24,12 +24,6 @@ Components">>.
.The [classname]#ComboBox# Component
image::img/combobox-basic.png[width=35%, scaledwidth=50%]
-[classname]#ComboBox# supports adding new items when the user presses
-kbd:[Enter].
-ifdef::web[]
-See <<dummy/../../../framework/components/components-selection#components.selection.newitems,"Allowing Adding New Items">>.
-endif::web[]
-
[[components.combobox.filtering]]
== Filtered Selection
@@ -73,6 +67,57 @@ containing the input string. As shown in <<figure.components.combobox.filter>>
below, when we type some text in the input area, the drop-down list will show
all the matching items.
+[[components.combobox.newitems]]
+== Allowing Adding New Items
+
+[classname]#ComboBox# allows the user to add new items, when the user types
+in a value and presses kbd:[Enter]. You need to enable this with
+[methodname]#setNewItemHandler()#.
+
+Adding new items is not possible if the selection component is read-only. An
+attempt to do so may result in an exception.
+
+
+=== Handling New Items
+
+Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
+item caption string as parameter for the [methodname]#accept(String)# method.
+
+
+[source, java]
+----
+// List of planets
+List<Planet> planets = new ArrayList<>();
+planets.add(new Planet(1, "Mercury"));
+planets.add(new Planet(2, "Venus"));
+planets.add(new Planet(3, "Earth"));
+planets.add(new Planet(4, "Mars"));
+
+ComboBox<Planet> select =
+ new ComboBox<>("Select or Add a Planet");
+select.setItems(planets);
+
+// Use the name property for item captions
+select.setItemCaptionGenerator(Planet::getName);
+
+// Allow adding new items and add
+// handling for new items
+select.setNewItemHandler(inputString -> {
+
+ // Create a new bean - can't set all properties
+ Planet newPlanet = new Planet(planets.size(), inputString);
+ planets.add(newPlanet);
+
+ // Update combobox content
+ select.setItems(planets);
+
+ // Remember to set the selection to the new item
+ select.select(newPlanet);
+
+ Notification.show("Added new planet called " +
+ inputString);
+});
+----
[[components.combobox.css]]
== CSS Style Rules