Skip to content

Commit 6db44a1

Browse files
committed
fix issue with precision on decimals
- incorporate syntax improvements from @3commascapital in #151
1 parent fa9c464 commit 6db44a1

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/lib/components/RangePips.svelte

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
3737
// stylistic props
3838
export let focus: boolean;
39-
export let orientationStart: string;
39+
export let orientationStart: 'left' | 'right' | 'top' | 'bottom';
4040
4141
// methods
4242
export let moveHandle: (index: number | null, value: number) => void;
@@ -87,6 +87,7 @@
8787
class:selected={isSelected(min, values, precision)}
8888
class:in-range={isInRange(min, values, range)}
8989
style="{orientationStart}: 0%;"
90+
data-val={coerceFloat(min, precision)}
9091
on:pointerdown={(e) => {
9192
labelDown(e);
9293
}}
@@ -96,46 +97,36 @@
9697
>
9798
{#if all === 'label' || first === 'label'}
9899
<span class="pipVal">
99-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{@html formatter(
100-
coerceFloat(min),
101-
0,
102-
0
103-
)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
100+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}
101+
{@html formatter(coerceFloat(min, precision), 0, 0)}
102+
{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
104103
</span>
105104
{/if}
106105
</span>
107106
{/if}
108107

109108
{#if (all && rest !== false) || rest}
110109
{#each Array(pipCount + 1) as _, i}
111-
{#if getValueFromIndex(i, min, max, pipStep, step) !== min && getValueFromIndex(i, min, max, pipStep, step) !== max}
110+
{@const val = getValueFromIndex(i, min, max, pipStep, step, precision)}
111+
{#if val !== min && val !== max}
112112
<span
113113
class="pip"
114-
class:selected={isSelected(
115-
getValueFromIndex(i, min, max, pipStep, step),
116-
values,
117-
precision
118-
)}
119-
class:in-range={isInRange(getValueFromIndex(i, min, max, pipStep, step), values, range)}
120-
style="{orientationStart}: {valueAsPercent(
121-
getValueFromIndex(i, min, max, pipStep, step),
122-
min,
123-
max
124-
)}%;"
114+
class:selected={isSelected(val, values, precision)}
115+
class:in-range={isInRange(val, values, range)}
116+
style="{orientationStart}: {valueAsPercent(val, min, max, precision)}%;"
117+
data-val={val}
125118
on:pointerdown={(e) => {
126119
labelDown(e);
127120
}}
128121
on:pointerup={(e) => {
129-
labelUp(getValueFromIndex(i, min, max, pipStep, step), e);
122+
labelUp(val, e);
130123
}}
131124
>
132125
{#if all === 'label' || rest === 'label'}
133126
<span class="pipVal">
134-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{@html formatter(
135-
getValueFromIndex(i, min, max, pipStep, step),
136-
i,
137-
valueAsPercent(getValueFromIndex(i, min, max, pipStep, step), min, max, precision)
138-
)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
127+
{#if true || prefix}<span class="pipVal-prefix">{prefix}</span>{/if}
128+
{@html formatter(val, i, valueAsPercent(val, min, max, precision))}
129+
{#if true || suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
139130
</span>
140131
{/if}
141132
</span>
@@ -149,6 +140,7 @@
149140
class:selected={isSelected(max, values, precision)}
150141
class:in-range={isInRange(max, values, range)}
151142
style="{orientationStart}: 100%;"
143+
data-val={coerceFloat(max, precision)}
152144
on:pointerdown={(e) => {
153145
labelDown(e);
154146
}}
@@ -158,11 +150,9 @@
158150
>
159151
{#if all === 'label' || last === 'label'}
160152
<span class="pipVal">
161-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{@html formatter(
162-
coerceFloat(max),
163-
pipCount,
164-
100
165-
)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
153+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}
154+
{@html formatter(coerceFloat(max, precision), pipCount, 100)}
155+
{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
166156
</span>
167157
{/if}
168158
</span>
@@ -223,6 +213,7 @@
223213
position: absolute;
224214
top: 0.4em;
225215
transform: translate(-50%, 25%);
216+
display: inline-flex;
226217
}
227218
228219
:global(.rangePips.vertical .pipVal) {

src/lib/components/RangeSlider.svelte

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,20 @@
155155
* the orientation of the handles/pips based on the
156156
* input values of vertical and reversed
157157
**/
158-
$: orientationStart = vertical ? (reversed ? 'top' : 'bottom') : reversed ? 'right' : 'left';
159-
$: orientationEnd = vertical ? (reversed ? 'bottom' : 'top') : reversed ? 'left' : 'right';
158+
$: orientationStart = vertical
159+
? reversed
160+
? 'top'
161+
: 'bottom'
162+
: reversed
163+
? 'right'
164+
: ('left' as 'left' | 'right' | 'top' | 'bottom');
165+
$: orientationEnd = vertical
166+
? reversed
167+
? 'bottom'
168+
: 'top'
169+
: reversed
170+
? 'left'
171+
: ('right' as 'left' | 'right' | 'top' | 'bottom');
160172
161173
/**
162174
* check if an element is a handle on the slider

src/lib/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ export const getValueFromIndex = (
170170
min: number,
171171
max: number,
172172
pipStep: number,
173-
step: number
173+
step: number,
174+
precision: number = 2
174175
) => {
175-
return coerceFloat(min + index * step * pipStep);
176+
return coerceFloat(min + index * step * pipStep, precision);
176177
};

0 commit comments

Comments
 (0)