Skip to content

Commit 4b814df

Browse files
Fix: Make slider handle decimal values (#959)
* Make slider handle decimal values
1 parent ed30b6d commit 4b814df

File tree

3 files changed

+65
-38
lines changed

3 files changed

+65
-38
lines changed

packages/uui-range-slider/lib/uui-range-slider.element.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const TRACK_HEIGHT = 3;
1616
const TRACK_PADDING = 12;
1717
const STEP_MIN_WIDTH = 24;
1818

19+
const CountDecimalPlaces = (num: number) => {
20+
const decimalIndex = num.toString().indexOf('.');
21+
return decimalIndex >= 0 ? num.toString().length - decimalIndex - 1 : 0;
22+
};
23+
1924
// TODO: ability to focus on the range, to enable keyboard interaction to move the range.
2025
// TODO: Ability to click outside a range, to move the range if the maxGap has been reached.
2126
// TODO: .
@@ -624,8 +629,16 @@ export class UUIRangeSliderElement extends UUIFormControlMixin(LitElement, '') {
624629

625630
private _renderThumbValues() {
626631
return html`<div class="thumb-values">
627-
<span><span>${this._lowInputValue}</span></span>
628-
<span><span>${this._highInputValue}</span></span>
632+
<span
633+
><span
634+
>${this._lowInputValue.toFixed(CountDecimalPlaces(this._step))}</span
635+
></span
636+
>
637+
<span
638+
><span
639+
>${this._highInputValue.toFixed(CountDecimalPlaces(this._step))}</span
640+
></span
641+
>
629642
</div>`;
630643
}
631644

@@ -656,7 +669,9 @@ export class UUIRangeSliderElement extends UUIFormControlMixin(LitElement, '') {
656669
let index = 0;
657670
const stepValues = new Array(stepAmount + 1)
658671
.fill(this._step)
659-
.map(step => this._min + step * index++);
672+
.map(step =>
673+
(this._min + step * index++).toFixed(CountDecimalPlaces(this._step)),
674+
);
660675

661676
return html`<div class="step-values">
662677
${stepValues.map(value => html`<span><span>${value}</span></span>`)}

packages/uui-slider/lib/uui-slider.element.ts

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,14 @@ import { UUISliderEvent } from './UUISliderEvent';
1111
const TRACK_PADDING = 12;
1212
const STEP_MIN_WIDTH = 24;
1313

14-
const RenderTrackSteps = (steps: number[], stepWidth: number) => {
15-
return svg`
16-
${steps.map(el => {
17-
if (stepWidth >= STEP_MIN_WIDTH) {
18-
const x = Math.round(TRACK_PADDING + stepWidth * steps.indexOf(el));
19-
return svg`<circle class="track-step" cx="${x}" cy="50%" r="4.5" />`;
20-
}
21-
return svg``;
22-
})}
23-
`;
24-
};
25-
26-
const RenderStepValues = (
27-
steps: number[],
28-
stepWidth: number,
29-
hide: boolean,
30-
) => {
31-
if (hide) return nothing;
32-
33-
return html`<div id="step-values">
34-
${steps.map(
35-
el =>
36-
html` <span
37-
><span>
38-
${steps.length <= 20 && stepWidth >= STEP_MIN_WIDTH
39-
? el.toFixed(0)
40-
: nothing}
41-
</span></span
42-
>`,
43-
)}
44-
</div>`;
45-
};
46-
4714
const GenerateStepArray = (start: number, stop: number, step: number) =>
4815
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
4916

17+
const CountDecimalPlaces = (num: number) => {
18+
const decimalIndex = num.toString().indexOf('.');
19+
return decimalIndex >= 0 ? num.toString().length - decimalIndex - 1 : 0;
20+
};
21+
5022
/**
5123
* @element uui-slider
5224
* @description - Native `<input type="range">` wrapper.
@@ -295,6 +267,37 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
295267
this.dispatchEvent(new UUISliderEvent(UUISliderEvent.CHANGE));
296268
}
297269

270+
renderTrackSteps() {
271+
return svg`
272+
${this._steps.map(el => {
273+
if (this._stepWidth >= STEP_MIN_WIDTH) {
274+
const x = Math.round(
275+
TRACK_PADDING + this._stepWidth * this._steps.indexOf(el),
276+
);
277+
return svg`<circle class="track-step" cx="${x}" cy="50%" r="4.5" />`;
278+
}
279+
return svg``;
280+
})}
281+
`;
282+
}
283+
284+
renderStepValues() {
285+
if (this.hideStepValues) return nothing;
286+
287+
return html`<div id="step-values">
288+
${this._steps.map(
289+
el =>
290+
html` <span
291+
><span>
292+
${this._steps.length <= 20 && this._stepWidth >= STEP_MIN_WIDTH
293+
? el.toFixed(CountDecimalPlaces(this.step))
294+
: nothing}
295+
</span></span
296+
>`,
297+
)}
298+
</div>`;
299+
}
300+
298301
render() {
299302
return html`
300303
<input
@@ -312,7 +315,7 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
312315
<div id="track" aria-hidden="true">
313316
<svg height="100%" width="100%">
314317
<rect x="9" y="9" height="3" rx="2" />
315-
${RenderTrackSteps(this._steps, this._stepWidth)}
318+
${this.renderTrackSteps()}
316319
</svg>
317320
318321
<div id="track-inner" aria-hidden="true">
@@ -323,7 +326,7 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
323326
</div>
324327
</div>
325328
</div>
326-
${RenderStepValues(this._steps, this._stepWidth, this.hideStepValues)}
329+
${this.renderStepValues()}
327330
`;
328331
}
329332

packages/uui-slider/lib/uui-slider.story.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ export const Readonly: Story = {
5656
readonly: true,
5757
},
5858
};
59+
60+
export const DecimalValue: Story = {
61+
args: {
62+
min: 0,
63+
max: 1,
64+
step: 0.1,
65+
value: 0.5,
66+
},
67+
};

0 commit comments

Comments
 (0)