Skip to content

Commit

Permalink
Added NumeralFieldFormatter and updated cleave.js
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesh2 committed Aug 28, 2017
1 parent af54cb3 commit bd122d1
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 5 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ public class TextFieldFormatterState extends JavaScriptExtensionState {
public boolean lowercase = false;
public boolean formatPhone = false;
public String phoneRegionCode;

public boolean formatNumeral = false;
public int numeralIntegerScale;
public int numeralDecimalScale;
public String numeralDecimalMark;
public boolean numeralPositiveOnly;
}

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion src/main/resources/org/vaadin/textfieldformatter/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@ window.org_vaadin_textfieldformatter_AbstractTextFieldFormatterExtension = funct
phone: true,
phoneRegionCode: connector.getState().phoneRegionCode
});
} else if (this.getState().formatNumeral) {
if (connector.getState().numeralIntegerScale) {
var cleave = new Cleave(el, {
numeral: true,
numeralIntegerScale: connector.getState().numeralIntegerScale,
numeralDecimalScale: connector.getState().numeralDecimalScale,
numeralDecimalMark: connector.getState().numeralDecimalMark,
delimiter: connector.getState().delimiter,
numeralPositiveOnly: connector.getState().numeralPositiveOnly,
});
} else {
var cleave = new Cleave(el, {
numeral: true,
numeralDecimalScale: connector.getState().numeralDecimalScale,
numeralDecimalMark: connector.getState().numeralDecimalMark,
delimiter: connector.getState().delimiter,
numeralPositiveOnly: connector.getState().numeralPositiveOnly,
});
}
} else if (this.getState().formatBlocks) {

if (connector.getState().delimiters) {
new Cleave(el, {
blocks: connector.getState().formatBlocks,
Expand Down
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;
}

}
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;
}

}
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());
}
}

0 comments on commit bd122d1

Please sign in to comment.