Skip to content

Commit

Permalink
Merge pull request #194 from kbase/cody/slider-decimals
Browse files Browse the repository at this point in the history
Fix slider step precision
  • Loading branch information
codytodonnell authored Apr 9, 2024
2 parents 7e39b6f + 7aaec2c commit 44dd59c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/common/utils/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ export function uriEncodeTemplateTag(
export function formatNumber(num: number) {
return num.toLocaleString();
}

/**
* Count the number of decimals in a given float or int
*/
export function countDecimals(num: number) {
if (num % 1 !== 0) return num.toString().split('.')[1].length;
return 0;
}
14 changes: 12 additions & 2 deletions src/features/collections/CollectionDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { usePageTitle } from '../layout/layoutSlice';
import styles from './Collections.module.scss';
import { useEffect, useMemo, useRef, useState } from 'react';
import { DataProduct } from './DataProduct';
import { snakeCaseToHumanReadable } from '../../common/utils/stringUtils';
import {
countDecimals,
snakeCaseToHumanReadable,
} from '../../common/utils/stringUtils';
import { MATCHER_LABELS, MatchModal } from './MatchModal';
import { ExportModal } from './ExportModal';
import { Button, Input } from '../../common/components';
Expand Down Expand Up @@ -808,6 +811,13 @@ const RangeFilterControls = ({
}
}, [filter.value, filterMax, filterMin, setValue]);

// Find the greatest number of decimal places used between the
// filter's max and min and use 10^-numDecimals as the step
const getStep = (min: number, max: number) => {
const numDecimals = Math.max(countDecimals(min), countDecimals(max));
return parseFloat(Math.pow(10, -1 * numDecimals).toFixed(numDecimals));
};

return (
<Stack className={styles['range-filter']}>
<Stack direction="row" spacing={2}>
Expand Down Expand Up @@ -849,7 +859,7 @@ const RangeFilterControls = ({
step={
filter.type === 'int'
? 1
: (filter.max_value - filter.min_value) / 100
: getStep(filter.max_value, filter.min_value)
}
marks={[filter.min_value, filter.max_value].map((v) => ({
value: v,
Expand Down

0 comments on commit 44dd59c

Please sign in to comment.