File size: 2,867 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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Rheostat from 'rheostat';
import { storiesOf } from '@storybook/react';
import { connectRange } from 'react-instantsearch-dom';
import { WrapWithHits } from './util';

const stories = storiesOf('Integration With Other Libraries', module);

stories.add('Airbnb Rheostat', () => (
  <WrapWithHits linkedStoryGroup="3rdPartyIntegrations.stories.js">
    <h3 style={{ marginBottom: 50, textAlign: 'center' }}>
      ⚠️ This example only works with the version 2.x of Rheostat ️️⚠️
    </h3>
    <ConnectedRange attribute="price" />
  </WrapWithHits>
));

class Range extends Component {
  static propTypes = {
    min: PropTypes.number,
    max: PropTypes.number,
    currentRefinement: PropTypes.object,
    refine: PropTypes.func.isRequired,
    canRefine: PropTypes.bool.isRequired,
    header: PropTypes.node,
    footer: PropTypes.node,
  };

  state = { currentValues: { min: this.props.min, max: this.props.max } };

  componentDidUpdate(prevProps) {
    if (
      this.props.canRefine &&
      (prevProps.currentRefinement.min !== this.props.currentRefinement.min ||
        prevProps.currentRefinement.max !== this.props.currentRefinement.max)
    ) {
      this.setState({
        currentValues: {
          min: this.props.currentRefinement.min,
          max: this.props.currentRefinement.max,
        },
      });
    }
  }

  onValuesUpdated = (sliderState) => {
    this.setState({
      currentValues: { min: sliderState.values[0], max: sliderState.values[1] },
    });
  };

  onChange = (sliderState) => {
    if (
      this.props.currentRefinement.min !== sliderState.values[0] ||
      this.props.currentRefinement.max !== sliderState.values[1]
    ) {
      this.props.refine({
        min: sliderState.values[0],
        max: sliderState.values[1],
      });
    }
  };

  render() {
    const { min, max, currentRefinement } = this.props;
    const { currentValues } = this.state;

    return min !== max ? (
      <Rheostat
        className="ais-RangeSlider"
        min={min}
        max={max}
        values={[currentRefinement.min, currentRefinement.max]}
        onChange={this.onChange}
        onValuesUpdated={this.onValuesUpdated}
      >
        <div
          className="rheostat-marker rheostat-marker--large"
          style={{ left: '0%', position: 'absolute', marginLeft: '0px' }}
        >
          <div className="rheostat-value">{currentValues.min}</div>
        </div>
        <div
          className="rheostat-marker rheostat-marker--large"
          style={{ left: '100%', position: 'absolute', marginLeft: '-1px' }}
        >
          <div className="rheostat-value">{currentValues.max}</div>
        </div>
      </Rheostat>
    ) : null;
  }
}

const ConnectedRange = connectRange(Range);

export default ConnectedRange;