Created
November 7, 2016 14:18
-
-
Save caseyWebb/a24ff5a98920b6350446f895f47ab15a to your computer and use it in GitHub Desktop.
ko.bindingHandlers.numericInput
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
'use strict' | |
const ko = require('knockout') | |
const _ = require('lodash') | |
ko.bindingHandlers.numericValue = { | |
init(el, valueAccessor, allBindings) { | |
const vstr = ko.pureComputed(() => ko.unwrap(valueAccessor()).toString()) | |
ko.applyBindingsToNode(el, { | |
textInput: ko.pureComputed({ | |
read() { | |
if (isNaNvstr() === 'NaN') return | |
if (allBindings.has('numericValuePadding')) { | |
const padWidth = allBindings.get('numericValuePadding') | |
const padding = _.repeat('0', padWidth - vstr().length) | |
return padding + vstr() | |
} | |
else | |
return vstr() | |
}, | |
write(v) { | |
valueAccessor()(parseFloat(v)) | |
} | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment