Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(input[range]): support step #14970

Closed
wants to merge 2 commits into from
Closed

Conversation

Narretz
Copy link
Contributor

@Narretz Narretz commented Jul 30, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
feat

What is the current behavior? (You can also link to an open issue here)
Input range doesn't handle the step attribute wrt to validation and model updates

Does this PR introduce a breaking change?
No

TODO

  • Add tests for step + min + max
  • Add docs
  • Find out exactly what is broken in Chrome

Other information:

Wrt the Chrome bug mentioned in #14870 (comment), it looks like setting a number isn't the problem, but that Chrome has a bug when you change the step value multiple times. Not sure yet, though.

@Narretz Narretz added this to the 1.5.x milestone Jul 30, 2016
@Narretz Narretz changed the title feat(input[range]): support step wip: feat(input[range]): support step Jul 30, 2016
@Narretz
Copy link
Contributor Author

Narretz commented Jul 31, 2016

@gkalpak I can't replicate the bug you described in #14870 (comment). Here's my repro: http://plnkr.co/edit/cBc2YAySJzD8y0NukAEQ?p=preview
What happens instead is that Chrome sets the value to "60", which is also weird, as "40" (like Firefox sets it) is much closer to "45" (the previous value).

There's also another test case (fn angTestCase()) that shows how Chrome stops adjusting the element value based on the step when the value has been modified previously.

I think I'll raise both bugs in the Webkit tracker, but I'm totally prepared that this is following the spec in some obscure way ...

@gkalpak
Copy link
Member

gkalpak commented Jul 31, 2016

I can't reproduce it on Chrome v52 either. (I was on v51 when I reproduced it. It got probably fixed, introducing another bug 😉)

@Narretz
Copy link
Contributor Author

Narretz commented Aug 1, 2016

Reported as https://bugs.chromium.org/p/chromium/issues/detail?id=633075 (stops adjusting) and https://bugs.chromium.org/p/chromium/issues/detail?id=633074 (adjusts to wrong value)

@Narretz Narretz modified the milestones: 1.5.9, 1.5.x Aug 8, 2016
stepChange(attr.step);
attr.$observe('step', stepChange);
}

Copy link
Contributor

@petebacondarwin petebacondarwin Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of similar (almost duplicate) code here. In the interests of keeping the code size down is it possible to move this into a helper function? Perhaps:

function setupHandlers(type, attrType, noopValidationFn, validatorFn, changeFn) {
  if (attrType) {
    ctrl.$validators[type] = attributeType === type && supportsRange ? noopValidatorFn : validatorFn;
    if (attrType === type) {
      element.attr(type, attr[type]);
    }
    changeFn(attr[type]);
    attr.$observe(type, changeFn);
  }
}

setupHandlers('max', maxAttrType, ...);

@petebacondarwin
Copy link
Contributor

One refactoring comment. Other than that LGTM

@Narretz
Copy link
Contributor Author

Narretz commented Aug 15, 2016

@petebacondarwin Sorry Pete, the current state of the PR is outdated :o I just pushed the updates.
It also includes step support for input number.

The main difference for range to before is that we don't support ngStep, because the browser would still use the default step of 1, and we don't test the step change updates anymore, because not all browsers implement them.

@Narretz Narretz changed the title wip: feat(input[range]): support step feat(input[range]): support step Aug 15, 2016
@@ -1642,6 +1690,21 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}
}

function stepChange(val) {
stepVal = parseNumberAttrVal(val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps do this after the check for NaN below to save unnecessary computation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh - this is a side-effect? OK

@petebacondarwin
Copy link
Contributor

I feel sad that we don't support ngStep for native range inputs. It feels confusing for the developer to have it but only for non-native ranges.

@Narretz
Copy link
Contributor Author

Narretz commented Aug 17, 2016

That's why I didn't document it at all. Because it's not really useful.

Step support works like min / max, but with the following caveat.
Currently, only Firefox fully implements the spec. Other browsers
(Chrome, Safari, Edge) have issues when the step value changes
after the input has been changed. They do not adjust the input value
to a valid value, but instead set the stepMismatch validity state.

Angular will take this validity state, and forward it as the ngModel
"step" error. Adjusting the error ourselves would add too much code,
as the logic is quite involved.
input[number] will now set the step error if the input value
(ngModel $viewValue) does not fit the step constraint set in the step / ngStep attribute.

Fixes angular#10597
@Narretz
Copy link
Contributor Author

Narretz commented Aug 17, 2016

Landed as e1da4be and 9a8b8aa

@Narretz Narretz closed this Aug 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants