Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Entering negative values in SpinnerItem

    Hi guys,

    Consider the following code:

    Code:
      public class App implements EntryPoint {
    
       public void onModuleLoad() {
          List<Canvas> members = new ArrayList<>();
          members.add(testSpinnerItemNegativeValue());
    
          Layout panel = new VLayout();
          panel.setWidth100();
          panel.setHeight100();
          panel.setMembers(members.toArray(new Canvas[0]));
          panel.draw();
       }
    
       private Canvas testSpinnerItemNegativeValue() {
           SpinnerItem item = new SpinnerItem();
           item.setMax(5);
           item.setMin(-5);
           item.setChangeOnKeypress(true);
           item.addChangedHandler(changedEvent -> GWT.log("spinnerItem.onChanged"));
           item.addChangeHandler(changeEvent -> GWT.log("spinnerItem.onChange"));
    
           DynamicForm form = new DynamicForm();
           form.setWidth100();
           form.setItems(item);
           return form;
       }
    }
    Our users use negative numbers, but when they clear the spinneritem, they cannot enter a new negative number by entering '-'. The event is not registered and nothing shows in the field. Instead they have to write the number first and then move the cursor to the beginning of the field and then enter the '-'.
    It is a requirement that item.setChangeOnKeypress is set to true, so that each key event is firing a change/changed event. Of course, no event is expected when just '-' is entered, since this is not a valid value.
    Is this achievable somehow?

    We have tested with:
    12.1-d20200307
    12.0-p20191129
    Best Regards
    Rasmus
    Last edited by rn; 9 Mar 2020, 00:25. Reason: Added which SmartGwt versions that we have tested on.

    #2
    This has been fixed for builds dated March 12 and later.

    Comment


      #3
      Hello,

      Yes, it is now possible to enter the '-' in an empty field - great! :)

      But I am having some problems dealing with the validation and change/changed events. They all report a value of 0, when I enter the '-' letter in a empty spinner item.
      So my SpinnerItem is configured as:

      Code:
      SpinnerItem item = new SpinnerItem();
      item.setMax(5);
      item.setMin(-5);
      item.setChangeOnKeypress(true);
      item.setValidators(new CustomValidator() {
         @Override
         protected boolean condition(Object o) {
            GWT.log("validate: " + o);
            return true;
         }
      });
      item.addChangedHandler(changedEvent -> GWT.log("onChanged:" + changedEvent.getValue()));
      item.addChangeHandler(changeEvent -> GWT.log("onChange: " + changeEvent.getValue()));
      All the log output is reporting a value of 0. It is hard to do validation and do stuff in the change/changed events, since I cannot completely trust the new value. A zero might be a ''-' or actually '0' entered by the user.

      How can I handle this? - should I look into key events as well?
      Thanks for your help.

      Best Regards
      Rasmus

      Comment


        #4
        The value "-" is not a number - but SpinnerItems need numbers. This is exactly why changeOnKeypress is switched off for SpinnerItems.

        So, internally, "0", "-" and "-0" are all equal to zero - there is no other way to deal with it.

        We don't understand what the problem is, really - what would you do with the value "-"?


        Comment


          #5
          Note that, if you want the value that's currently visible in the SpinnerItem, rather than the result of getValue(), you can call item.getEnteredValue().

          Comment


            #6
            Thanks for your reply.
            I will look into getEnteredValue() - it might solve our problem.

            Comment

            Working...
            X