All files RangeSlider.js

100% Statements 18/18
91.89% Branches 34/37
50% Functions 1/2
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                                          1x   1x                                     32x   32x   32x                 32x                                 32x   20x         20x             20x 20x 20x 20x 20x   20x                                                             12x             1x   1x                                                
/**
 * MIT License
 *
 * Copyright (c) 2020 Jason Wilson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
import React from 'react';
import PropTypes from 'prop-types';
import { useBootstrapPrefix } from 'react-bootstrap/ThemeProvider';
import classNames from 'classnames';
 
const DEFAULT_CLASS_PREFIX = 'range-slider';
 
const RangeSlider = React.forwardRef(({
  size,
  disabled = false,
  value,
  onChange,
  min = 0,
  max = 100,
  step,
  variant = 'primary',
  inputProps = {},
  tooltip = 'auto',
  tooltipPlacement = 'bottom',
  tooltipLabel,
  tooltipStyle = {},
  tooltipProps = {},
  bsPrefix,
  className,
}, ref) => {
 
  const prefix = useBootstrapPrefix(bsPrefix, DEFAULT_CLASS_PREFIX);
 
  const isTooltip = tooltip === 'auto' || tooltip === 'on';
 
  const classes = classNames(
    className,
    prefix,
    size && `${prefix}--${size}`,
    disabled && 'disabled',
    variant && `${prefix}--${variant}`,
  );
 
  const inputEl = (
    <input
      type="range"
      className={classes}
      value={value}
      min={min}
      max={max}
      step={step}
      onChange={onChange}
      disabled={disabled}
      ref={ref}
      style={{
        '--value': value,
      }}
      {...inputProps}
    />
  );
 
  if (isTooltip) {
 
    const wrapClasses = classNames(
      `${prefix}__wrap`,
      size && `${prefix}__wrap--${size}`,
    );
 
    const tooltipClasses = classNames(
      `${prefix}__tooltip`,
      isTooltip && `${prefix}__tooltip--${tooltip}`,
      tooltipPlacement && `${prefix}__tooltip--${tooltipPlacement}`,
      disabled && `${prefix}__tooltip--disabled`,
    );
 
    const thumbRadius = size === 'sm' ? 8 : (size === 'lg' ? 12 : 10);
    const fract = (value - min) / (max - min);
    const percentLeft = fract * 100;
    const fractFromCentre = (fract - 0.5) * 2;
    const adjustment = fractFromCentre * -thumbRadius; // Half thumb width
 
    return (
      <span
        className={wrapClasses}
      >
 
        {inputEl}
 
        <div
          className={tooltipClasses}
          style={{
            ...(tooltipStyle || {}),
            left: `calc(${percentLeft}% + ${adjustment}px)`,
          }}
          {...tooltipProps}
        >
 
          <div className={`${prefix}__tooltip__label`}>
 
            {tooltipLabel ? tooltipLabel(value) : value}
 
          </div>
 
          <div className={`${prefix}__tooltip__arrow`}/>
 
        </div>
        
      </span>
    );
 
  } else {
 
    return inputEl;
 
  }
 
});
 
// Fix: https://github.com/jaywilz/react-bootstrap-range-slider/issues/3
const Element = typeof Element === 'undefined' ? function() {} : Element;
 
RangeSlider.propTypes = {
  value: PropTypes.number.isRequired,
  onChange: PropTypes.func.isRequired,
  min: PropTypes.number,
  max: PropTypes.number,
  step: PropTypes.number,
  disabled: PropTypes.bool,
  size: PropTypes.oneOf([ 'sm', 'lg' ]),
  variant: PropTypes.oneOf([ 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light' ]),
  inputProps: PropTypes.object,
  tooltip: PropTypes.oneOf([ 'auto', 'on', 'off' ]),
  tooltipPlacement: PropTypes.oneOf([ 'top', 'bottom' ]),
  tooltipLabel: PropTypes.func,
  tooltipStyle: PropTypes.object,
  tooltipProps: PropTypes.object,
  className: PropTypes.string,
  ref: PropTypes.oneOfType([
    PropTypes.func, 
    PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
  ]),
  bsPrefix: PropTypes.string,
};
 
export default RangeSlider;