Formatter
So although we’ve used prefix and suffix to add some formatting before and
after the values in the previous examples, we can also use the formatter function to format the
values in any way we like.
The formatter function is passed the value, index and percent of the pip/handle
to allow us to modify the display.
<RangeSlider formatter={(value, index, percent) => { return value }} />
Pips and Floats
There are three properties that deal with formatting; formatter, handleFormatter & rangeFormatter. These
are for the pips, handles and ranges respectively.
- If the
handleFormatteris not set, then theformatterwill be inherited for it- as long as the
formatteris already set
- as long as the
- But
rangeFormatteris not inherited as the function parameters are different
Single Formatter
Like in the prefix example, where we added a simple € prefix. This time we will also style
as a currency, but we will format the whole number into a currency string using the
Intl.NumberFormat API.
Multiple Formatters
Here we will use the same formatter example as above for displaying the handleFormatter, but
we will use a different formatter for the pip labels to demonstrate how you can use different
formatters for each.
Formatting the rangeFloat
Above we demonstrated how to format the float using the handleFormatter property,
but we can also format the rangeFloat separately using the rangeFormatter property.
This has a slightly different signature to the formatter & handleFormatter properties,
as we need to pass in both the first and second handle values.
Fun Formatters
A cute example where we don’t even care about numbers, a sort-of Net Promoter Score.
Giga
Here’s a pretty intense example, where we use the formatter to convert the values
into Gigabytes, Terabytes, Petabytes, etc. There’s a pretty hefty algorithm to scale
the values logarithmically, and then a special function for styling the numbers as
Giga, Tera, Peta, etc.
Squares
One last example of handle and pip formatters working to compliment
each other. This time we will use the handleFormatter to show the
real value, and the formatter to show the square of the value.