Fix slider rounding
This commit is contained in:
@@ -6,6 +6,7 @@ import './emby-slider.scss';
|
||||
import 'webcomponents.js/webcomponents-lite';
|
||||
import '../emby-input/emby-input';
|
||||
import globalize from '../../scripts/globalize';
|
||||
import { decimalCount } from '../../utils/number';
|
||||
|
||||
const EmbySliderPrototype = Object.create(HTMLInputElement.prototype);
|
||||
|
||||
@@ -75,13 +76,23 @@ function mapClientToFraction(range, clientX) {
|
||||
function mapFractionToValue(range, fraction) {
|
||||
let value = (range.max - range.min) * fraction;
|
||||
|
||||
let decimals = null;
|
||||
|
||||
// Snap to step
|
||||
if (range.step !== 'any') {
|
||||
const step = normalizeSliderStep(range);
|
||||
decimals = decimalCount(step);
|
||||
value = Math.round(value / step) * step;
|
||||
}
|
||||
|
||||
value += parseFloat(range.min);
|
||||
const min = parseFloat(range.min);
|
||||
|
||||
value += min;
|
||||
|
||||
if (decimals != null) {
|
||||
decimals = Math.max(decimals, decimalCount(min));
|
||||
value = parseFloat(value.toFixed(decimals));
|
||||
}
|
||||
|
||||
return Math.min(Math.max(value, range.min), range.max);
|
||||
}
|
||||
|
||||
@@ -32,3 +32,16 @@ export function toPercent(value: number | null | undefined, locale: string): str
|
||||
|
||||
return `${Math.round(value * 100)}%`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets decimal count of a Number.
|
||||
* @param {number} value Number.
|
||||
* @returns {number} Decimal count of a Number.
|
||||
*/
|
||||
export function decimalCount(value: number): number {
|
||||
if (Number.isInteger(value)) return 0;
|
||||
|
||||
const arr = value.toString().split('.');
|
||||
|
||||
return arr[1].length;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user