import clsx from 'clsx'; import { omit } from 'lodash'; import PropTypes from 'prop-types'; import { Component } from 'react'; import FormRange from 'calypso/components/forms/form-range'; import './style.scss'; export default class extends Component { static displayName = 'Range'; static propTypes = { minContent: PropTypes.node, maxContent: PropTypes.node, min: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ), max: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ), value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ), showValueLabel: PropTypes.bool, }; static defaultProps = { min: 0, max: 10, value: 0, showValueLabel: false, }; state = { id: 'range' + crypto.randomUUID(), }; getMinContentElement = () => { if ( this.props.minContent ) { return { this.props.minContent }; } }; getMaxContentElement = () => { if ( this.props.maxContent ) { return { this.props.maxContent }; } }; getValueLabelElement = () => { let left; let offset; if ( this.props.showValueLabel ) { left = ( 100 * ( this.props.value - this.props.min ) ) / ( this.props.max - this.props.min ); // The center of the slider thumb is not aligned to the same // percentage stops as an absolute positioned element will be. // Therefore, we adjust based on the thumb's position relative to // its own size. Ideally, we would use `getComputedStyle` here, // but this method doesn't support the thumb pseudo-element in all // browsers. The multiplier is equal to half of the thumb's width. // // Normal: // v v v // |( )----( )----( )| // // Adjusted: // v v v // |( )----( )----( )| offset = Math.floor( 13 * ( ( 50 - left ) / 50 ) ); // 26px / 2 = 13px return ( { this.props.value } ); } }; render() { const classes = clsx( this.props.className, 'range', { 'has-min-content': !! this.props.minContent, 'has-max-content': !! this.props.maxContent, } ); return (
{ this.getMinContentElement() } { this.getMaxContentElement() } { this.getValueLabelElement() }
); } }