Make min/max field values work for inputted values also

(previous commit only worked for the up/down buttons)
This commit is contained in:
Bryan Housel
2018-06-02 12:19:43 -04:00
parent 861299bb1c
commit 7ae8c9f6a7
+25 -16
View File
@@ -83,31 +83,40 @@ export function uiFieldText(field, context) {
spinControl.selectAll('button')
.on('click', function(d) {
d3_event.preventDefault();
var num = parseInt(input.node().value || 0, 10);
if (isNaN(num)) {
num = 0;
}
num = num + d;
if (field.minValue !== undefined) {
num = Math.max(num, field.minValue);
}
if (field.maxValue !== undefined) {
num = Math.min(num, field.maxValue);
}
input.node().value = num;
input.node().value = parsed(input.node().value) + d;
change()();
});
}
}
// parse as a number
function parsed(val) {
return parseInt(val || 0, 10) || 0;
}
// clamp number to min/max
function clamped(num) {
if (field.minValue !== undefined) {
num = Math.max(num, field.minValue);
}
if (field.maxValue !== undefined) {
num = Math.min(num, field.maxValue);
}
return num;
}
function change(onInput) {
return function() {
var t = {};
t[field.key] = utilGetSetValue(input) || undefined;
var val = utilGetSetValue(input) || undefined;
if (!onInput && field.type === 'number') {
val = clamped(parsed(val)) + '';
utilGetSetValue(input, val);
}
t[field.key] = val;
dispatch.call('change', this, t, onInput);
};
}