Permalink
Browse files

Merge pull request #2298 from ekylibre/debug/number-inputs

Imported C&F numeric input behaviour into Ekylibre
  • Loading branch information...
Nyxelio committed Jun 29, 2018
2 parents 3d86f85 + 43525dc commit f1afa0d20479b778c063acd5dcdd5738cc0a4e9d
Showing with 27 additions and 0 deletions.
  1. +9 −0 app/assets/javascripts/form/numeric.js.coffee
  2. +18 −0 config/initializers/simple_form.rb
@@ -0,0 +1,9 @@
(($) ->
$(document).ready ->
$(document).on "keyup", "input.numeric", (e) ->
currentValue = parseFloat($(this).val() || $(this).attr('placeholder') || 0)
if e.which == 38
$(this).val(currentValue + 1)
if e.which == 40
$(this).val(currentValue - 1)
) (jQuery)
@@ -140,3 +140,21 @@
# Cache SimpleForm inputs discovery
# config.cache_discovery = !Rails.env.development?
end
module SeparatorHandling
def input(wrapper_options = nil)
validation = <<~JS
if(/^\\d*[\.,]\\d*$/.test(value)){
value=value.replace(",",".");
} else {
value=value.replace(/[^\\d.-]/g, "")
.replace(/\\./, "DOT")
.replace(/\\./g, "")
.replace(/DOT/, ".");
};
JS
super(wrapper_options.merge(oninput: validation)).gsub("type=\"number\"", "type=\"text\"")
end
end
SimpleForm::Inputs::NumericInput.prepend(SeparatorHandling)

0 comments on commit f1afa0d

Please sign in to comment.