Преглед на файлове

Add an option for defining number of visible items in a NativeSelect (#9109)

tags/8.1.0.alpha7
Artur преди 7 години
родител
ревизия
a48e5a1cb8

+ 24
- 0
client/src/main/java/com/vaadin/client/ui/VNativeSelect.java Целия файл

@@ -110,4 +110,28 @@ public class VNativeSelect extends FocusableFlowPanelComposite
}
super.setHeight(height);
}

/**
* Sets the number of items that are visible. If only one item is visible,
* then the box will be displayed as a drop-down list (the default).
*
* @since
* @param visibleItemCount
* the visible item count
*/
public void setVisibleItemCount(int visibleItemCount) {
getListBox().setVisibleItemCount(visibleItemCount);
}

/**
* Gets the number of items that are visible. If only one item is visible,
* then the box will be displayed as a drop-down list.
*
* @since
* @return the visible item count
*/
public int getVisibleItemCount() {
return getListBox().getVisibleItemCount();
}

}

+ 29
- 0
server/src/main/java/com/vaadin/ui/NativeSelect.java Целия файл

@@ -203,4 +203,33 @@ public class NativeSelect<T> extends AbstractSingleSelect<T>
Objects.nonNull(caption);
getState().emptySelectionCaption = caption;
}

/**
* Sets the number of items that are visible. If only one item is visible,
* then the box will be displayed as a drop-down list (the default).
*
* @since
* @param visibleItemCount
* the visible item count
* @throws IllegalArgumentException
* if the value is smaller than one
*/
public void setVisibleItemCount(int visibleItemCount) {
if (visibleItemCount < 1) {
throw new IllegalArgumentException(
"There must be at least one item visible");
}
getState().visibleItemCount = visibleItemCount;
}

/**
* Gets the number of items that are visible. If only one item is visible,
* then the box will be displayed as a drop-down list.
*
* @since
* @return the visible item count
*/
public int getVisibleItemCount() {
return getState(false).visibleItemCount;
}
}

+ 4
- 0
shared/src/main/java/com/vaadin/shared/ui/nativeselect/NativeSelectState.java Целия файл

@@ -15,6 +15,7 @@
*/
package com.vaadin.shared.ui.nativeselect;

import com.vaadin.shared.annotations.DelegateToWidget;
import com.vaadin.shared.ui.AbstractSingleSelectState;

/**
@@ -43,6 +44,9 @@ public class NativeSelectState extends AbstractSingleSelectState {
*/
public String emptySelectionCaption = "";

@DelegateToWidget
public int visibleItemCount = 1;

{
primaryStyleName = STYLE_NAME;
}

+ 19
- 0
uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java Целия файл

@@ -1,5 +1,7 @@
package com.vaadin.tests.components.nativeselect;

import java.util.LinkedHashMap;

import com.vaadin.tests.components.abstractlisting.AbstractSingleSelectTestUI;
import com.vaadin.ui.NativeSelect;

@@ -18,4 +20,21 @@ public class NativeSelects
component.setEmptySelectionAllowed(false);
return component;
}

@Override
protected void createActions() {
super.createActions();
LinkedHashMap<String, Integer> options = new LinkedHashMap<>();
options.put("1", 1);
options.put("2", 2);
options.put("5", 5);
createSelectAction("Visible item count", CATEGORY_SIZE, options, "1",
new Command<NativeSelect<Object>, Integer>() {
@Override
public void execute(NativeSelect<Object> c, Integer value,
Object data) {
c.setVisibleItemCount(value);
}
});
}
}

+ 42
- 0
uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectVisibleItemCountTest.java Целия файл

@@ -0,0 +1,42 @@
/*
* Copyright 2000-2016 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.tests.components.nativeselect;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.By;
import com.vaadin.testbench.elements.NativeSelectElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class NativeSelectVisibleItemCountTest extends SingleBrowserTest {

@Test
public void changeItemCount() {
openTestURL();
WebElement select = $(NativeSelectElement.class).first()
.findElement(By.xpath("select"));
Assert.assertEquals("1", select.getAttribute("size"));
selectMenuPath("Component", "Size", "Visible item count", "5");
Assert.assertEquals("5", select.getAttribute("size"));
}

@Override
protected Class<?> getUIClass() {
return NativeSelects.class;
}
}

Loading…
Отказ
Запис