@@ -11,42 +11,14 @@ import { UUISliderEvent } from './UUISliderEvent';
11
11
const TRACK_PADDING = 12 ;
12
12
const STEP_MIN_WIDTH = 24 ;
13
13
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
-
47
14
const GenerateStepArray = ( start : number , stop : number , step : number ) =>
48
15
Array . from ( { length : ( stop - start ) / step + 1 } , ( _ , i ) => start + i * step ) ;
49
16
17
+ const CountDecimalPlaces = ( num : number ) => {
18
+ const decimalIndex = num . toString ( ) . indexOf ( '.' ) ;
19
+ return decimalIndex >= 0 ? num . toString ( ) . length - decimalIndex - 1 : 0 ;
20
+ } ;
21
+
50
22
/**
51
23
* @element uui-slider
52
24
* @description - Native `<input type="range">` wrapper.
@@ -295,6 +267,37 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
295
267
this . dispatchEvent ( new UUISliderEvent ( UUISliderEvent . CHANGE ) ) ;
296
268
}
297
269
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
+
298
301
render ( ) {
299
302
return html `
300
303
<input
@@ -312,7 +315,7 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
312
315
<div id= "track" aria-hidden = "true">
313
316
<svg height= "100%" width = "100%">
314
317
<rect x= "9" y = "9" height= "3" rx= "2" / >
315
- ${ RenderTrackSteps ( this . _steps , this . _stepWidth ) }
318
+ ${ this . renderTrackSteps ( ) }
316
319
</ svg>
317
320
318
321
<div id= "track-inner" aria-hidden = "true">
@@ -323,7 +326,7 @@ export class UUISliderElement extends UUIFormControlMixin(LitElement, '') {
323
326
</ div>
324
327
</ div>
325
328
</ div>
326
- ${ RenderStepValues ( this . _steps , this . _stepWidth , this . hideStepValues ) }
329
+ ${ this . renderStepValues ( ) }
327
330
` ;
328
331
}
329
332
0 commit comments