-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added NumeralFieldFormatter and updated cleave.js
- Loading branch information
1 parent
af54cb3
commit bd122d1
Showing
7 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/vaadin/textfieldformatter/NumeralFieldFormatter.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,55 @@ | ||
package org.vaadin.textfieldformatter; | ||
|
||
import com.vaadin.ui.AbstractTextField; | ||
|
||
public class NumeralFieldFormatter extends AbstractTextFieldFormatterExtension { | ||
public static String DEFAULT_DELIMITER = ","; | ||
public static int DEFAULT_DECIMAL_SCALE = 2; | ||
public static String DEFAULT_DECIMAL_MARK = "."; | ||
private static int UNDEFINED_INTEGER_SCALE = -1; | ||
|
||
/** | ||
* Creates a TextFieldFormatter for numeral fields. By default has delimiter | ||
* ',' and decimal mark '.'. Integer scale is not limited and shows 2 | ||
* decimals. | ||
* | ||
* @param field | ||
*/ | ||
public NumeralFieldFormatter(AbstractTextField field) { | ||
this(field, DEFAULT_DELIMITER, DEFAULT_DECIMAL_MARK, UNDEFINED_INTEGER_SCALE, DEFAULT_DECIMAL_SCALE, false); | ||
} | ||
|
||
public NumeralFieldFormatter(AbstractTextField field, String delimiter, String decimalMark, int decimalScale) { | ||
this(field, delimiter, decimalMark, UNDEFINED_INTEGER_SCALE, decimalScale, false); | ||
} | ||
|
||
/** | ||
* Creates a field formatter for numeral fields. | ||
* | ||
* @param field | ||
* TextField to be extended | ||
* @param delimiter | ||
* Delimiter for integer groups | ||
* @param decimalMark | ||
* Delimiter for decimal | ||
* @param integerScale | ||
* Limit the scale of integer | ||
* @param decimalScale | ||
* Number of decimals | ||
* @param nonNegativeOnly | ||
* True: allow only non-negative numbers. False: Allow negative, | ||
* 0 and positive. | ||
*/ | ||
public NumeralFieldFormatter(AbstractTextField field, String delimiter, String decimalMark, int integerScale, | ||
int decimalScale, boolean nonNegativeOnly) { | ||
super(field); | ||
getState().formatNumeral = true; | ||
getState().delimiter = delimiter; | ||
getState().numeralDecimalMark = decimalMark; | ||
if (integerScale != -1) { | ||
getState().numeralIntegerScale = integerScale; | ||
} | ||
getState().numeralDecimalScale = decimalScale; | ||
getState().numeralPositiveOnly = nonNegativeOnly; | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
src/main/resources/org/vaadin/textfieldformatter/cleave.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
src/test/java/org/vaadin/textfieldformatter/CustomNumeralFieldFormatterUsageUI.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,19 @@ | ||
package org.vaadin.textfieldformatter; | ||
|
||
import org.vaadin.addonhelpers.AbstractTest; | ||
|
||
import com.vaadin.ui.Component; | ||
import com.vaadin.ui.Notification; | ||
import com.vaadin.ui.TextField; | ||
|
||
public class CustomNumeralFieldFormatterUsageUI extends AbstractTest { | ||
|
||
@Override | ||
public Component getTestComponent() { | ||
TextField tf = new TextField(); | ||
new NumeralFieldFormatter(tf, ".", ",", -1, 3, true); | ||
tf.addValueChangeListener(l -> Notification.show("Value: " + l.getValue())); | ||
return tf; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/org/vaadin/textfieldformatter/DefaultNumeralFieldFormatterUsageUI.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,19 @@ | ||
package org.vaadin.textfieldformatter; | ||
|
||
import org.vaadin.addonhelpers.AbstractTest; | ||
|
||
import com.vaadin.ui.Component; | ||
import com.vaadin.ui.Notification; | ||
import com.vaadin.ui.TextField; | ||
|
||
public class DefaultNumeralFieldFormatterUsageUI extends AbstractTest { | ||
|
||
@Override | ||
public Component getTestComponent() { | ||
TextField tf = new TextField(); | ||
new NumeralFieldFormatter(tf); | ||
tf.addValueChangeListener(l -> Notification.show("Value: " + l.getValue())); | ||
return tf; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/vaadin/textfieldformatter/it/NumeralFieldFormatterUsageIT.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,34 @@ | ||
package org.vaadin.textfieldformatter.it; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.selenium.phantomjs.PhantomJSDriver; | ||
import org.vaadin.textfieldformatter.CustomNumeralFieldFormatterUsageUI; | ||
import org.vaadin.textfieldformatter.DefaultNumeralFieldFormatterUsageUI; | ||
|
||
import com.vaadin.testbench.elements.TextFieldElement; | ||
|
||
public class NumeralFieldFormatterUsageIT extends AbstractCustomTestBenchTestCase { | ||
|
||
@Before | ||
public void init() { | ||
startBrowser(new PhantomJSDriver()); | ||
} | ||
|
||
@Test | ||
public void numeralFieldWithDefaultValues() throws InterruptedException { | ||
openUI(DefaultNumeralFieldFormatterUsageUI.class); | ||
TextFieldElement tf = $(TextFieldElement.class).first(); | ||
tf.sendKeys("12345.80"); | ||
Assert.assertEquals("12,345.80", tf.getValue()); | ||
} | ||
|
||
@Test | ||
public void numeralFieldWithCustomValues() throws InterruptedException { | ||
openUI(CustomNumeralFieldFormatterUsageUI.class); | ||
TextFieldElement tf = $(TextFieldElement.class).first(); | ||
tf.sendKeys("-12345,801"); | ||
Assert.assertEquals("12.345,801", tf.getValue()); | ||
} | ||
} |