File size: 1,289 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import clsx from 'clsx';
import { omit } from 'lodash';
import PropTypes from 'prop-types';
import { createRef, Component } from 'react';

import './style.scss';

const noop = () => {};

export default class extends Component {
	static displayName = 'FormRange';

	static propTypes = {
		onChange: PropTypes.func,
	};

	static defaultProps = {
		onChange: noop,
	};

	rangeRef = createRef();

	componentDidMount() {
		if ( this.shouldNormalizeChange() ) {
			this.rangeRef.current.addEventListener( 'change', this.onChange );
		}
	}

	componentWillUnmount() {
		this.rangeRef.current.removeEventListener( 'change', this.onChange );
	}

	shouldNormalizeChange = () => {
		const ua = window.navigator.userAgent;

		// Internet Explorer doesn't trigger the normal "input" event as the
		// user drags the thumb. Instead, it emits the equivalent event on
		// "change", so we watch the change event and emit a simulated event.
		return -1 !== ua.indexOf( 'MSIE' ) || -1 !== ua.indexOf( 'Trident/' );
	};

	onChange = ( event ) => {
		this.props.onChange( event );
	};

	render() {
		const classes = clsx( this.props.className, 'form-range' );

		return (
			<input
				ref={ this.rangeRef }
				type="range"
				className={ classes }
				{ ...omit( this.props, 'className' ) }
			/>
		);
	}
}