diff --git a/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/BookForm.java b/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/BookForm.java index 6c5a04b..571f7c0 100644 --- a/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/BookForm.java +++ b/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/BookForm.java @@ -375,6 +375,7 @@ public void mergeDraft(Product draft) { updateDirtyIndicators(); } + @Override public void focus() { productName.focus(); } diff --git a/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/NumberField.java b/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/NumberField.java index dc8bc26..a00e4f8 100644 --- a/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/NumberField.java +++ b/vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/crud/form/NumberField.java @@ -65,7 +65,6 @@ public NumberField(String string) { }); fireEvent(event); }); - textField.setWidth("100%"); } private void setTypeNumber() { @@ -81,6 +80,14 @@ protected TextField initContent() { return textField; } + @Override + public void setValue(Integer value) { + if (value == null) { + value = 0; + } + super.setValue(value); + } + @Override protected void doSetValue(Integer value) { textField.setValue(stockCountConverter.convertToPresentation(value, diff --git a/vaadincreate-ui/src/main/webapp/VAADIN/themes/vaadincreate/vaadincreate.scss b/vaadincreate-ui/src/main/webapp/VAADIN/themes/vaadincreate/vaadincreate.scss index 9de23f7..10e1796 100644 --- a/vaadincreate-ui/src/main/webapp/VAADIN/themes/vaadincreate/vaadincreate.scss +++ b/vaadincreate-ui/src/main/webapp/VAADIN/themes/vaadincreate/vaadincreate.scss @@ -105,4 +105,7 @@ $fake-grid-background-color: white; -webkit-appearance: none; margin: 0; } + input[type=number] { + appearance: textfield; + } } \ No newline at end of file diff --git a/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/NumberFieldTest.java b/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/NumberFieldTest.java index e7053f9..e0d567e 100644 --- a/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/NumberFieldTest.java +++ b/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/NumberFieldTest.java @@ -3,26 +3,34 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.vaadin.tatu.vaadincreate.i18n.DefaultI18NProvider; +import com.vaadin.data.ValueContext; import com.vaadin.server.ServiceException; import com.vaadin.testbench.uiunittest.UIUnitTest; +import com.vaadin.ui.TextField; import com.vaadin.ui.UI; public class NumberFieldTest extends UIUnitTest { + private final static char MINUS = '\u002D'; private static final String ERROR_MESSAGE = "Cannot convert value to a number."; - UI ui; + private UI ui; private NumberField field; @Before public void setup() throws ServiceException { // Vaadin mocks + Locale.setDefault(DefaultI18NProvider.LOCALE_EN); + ui = mockVaadin(); + ui.getSession().setLocale(DefaultI18NProvider.LOCALE_EN); field = new NumberField("Field"); ui.setContent(field); } @@ -44,7 +52,7 @@ public void setValueByText() { test(field.textField).setValue("20"); assertEquals(Integer.valueOf(20), field.getValue()); - test(field.textField).setValue("-20"); + test(field.textField).setValue(MINUS + "20"); assertEquals(Integer.valueOf(-20), field.getValue()); } @@ -85,4 +93,10 @@ public void valueChangeEvent() { assertEquals(3, count.get()); assertEquals(1, user.get()); } + + @Test + public void setValueNull() { + field.setValue(null); + assertEquals(Integer.valueOf(0), field.getValue()); + } } diff --git a/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/StockCountConverterTest.java b/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/StockCountConverterTest.java index 4e588aa..392e614 100644 --- a/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/StockCountConverterTest.java +++ b/vaadincreate-ui/src/test/java/org/vaadin/tatu/vaadincreate/crud/form/StockCountConverterTest.java @@ -3,10 +3,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.Locale; +import org.junit.Before; import org.junit.Test; import org.vaadin.tatu.vaadincreate.crud.form.StockCountConverter; @@ -15,42 +17,70 @@ public class StockCountConverterTest { + private static final String ERROR_MESSAGE = "Invalid stock count"; + private final static char MINUS = '\u002D'; + private StockCountConverter converter; + private TextField field; + private ValueContext context; + + @Before + public void setup() { + Locale.setDefault(Locale.ENGLISH); + converter = new StockCountConverter(ERROR_MESSAGE); + + field = new TextField(); + context = new ValueContext(null, field, Locale.ENGLISH); + } + @Test public void testConvertToModel() { - var converter = new StockCountConverter("Invalid stock count"); // Test with valid stock count - var result = converter.convertToModel("10", new ValueContext()); + var result = converter.convertToModel("10", context); assertNotNull(result); assertFalse(result.isError()); result.ifOk(value -> assertEquals(Integer.valueOf(10), value)); + // Test with negative stock count + result = converter.convertToModel(MINUS + "10", context); + assertNotNull(result); + assertFalse(result.isError()); + result.ifOk(value -> assertEquals(Integer.valueOf(-10), value)); + // Test with null stock count - result = converter.convertToModel(null, new ValueContext()); + result = converter.convertToModel(null, context); assertNotNull(result); assertFalse(result.isError()); result.ifOk(value -> assertEquals(Integer.valueOf(0), value)); // Test with invalid stock count - result = converter.convertToModel("abc", new ValueContext()); + result = converter.convertToModel("2.2", context); assertNotNull(result); assertTrue(result.isError()); - result.ifError(error -> assertEquals("Invalid stock count", error)); + result.ifError(error -> assertEquals(ERROR_MESSAGE, error)); // Test with invalid stock count - result = converter.convertToModel("10 000", new ValueContext()); + result = converter.convertToModel("abc", context); assertNotNull(result); assertTrue(result.isError()); - result.ifError(error -> assertEquals("Invalid stock count", error)); + result.ifError(error -> assertEquals(ERROR_MESSAGE, error)); + + // Test with invalid stock count + result = converter.convertToModel("10 000", context); + assertNotNull(result); + assertTrue(result.isError()); + result.ifError(error -> assertEquals(ERROR_MESSAGE, error)); } @Test public void testConvertToPresentation() { - var converter = new StockCountConverter("Invalid stock count"); - var field = new TextField(); - var context = new ValueContext(null, field, new Locale("fi")); - var result = converter.convertToPresentation(1000, context); assertEquals("1000", result); + + result = converter.convertToPresentation(-1000, context); + assertEquals(MINUS + "1000", result); + + result = converter.convertToPresentation(null, context); + assertNull(result); } } \ No newline at end of file