Skip to content

Commit

Permalink
Merge pull request #26 from thompsongl/into-feature/range-dropdowns
Browse files Browse the repository at this point in the history
`EuiDualRange` thumb position on resize
  • Loading branch information
cchaos authored Sep 5, 2019
2 parents d1bf98a + 1b58cdf commit 9a6d9de
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/components/form/range/dual_range.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { keyCodes } from '../../../services';
import { isWithinRange } from '../../../services/number';
import { EuiInputPopover } from '../../popover';
import { EuiFormControlLayoutDelimited } from '../form_control_layout';
import { EuiResizeObserver } from '../../observer/resize_observer';
import makeId from '../form_row/make_id';

import { EuiRangeHighlight } from './range_highlight';
Expand All @@ -22,13 +23,15 @@ export class EuiDualRange extends Component {
hasFocus: false,
rangeSliderRefAvailable: false,
isPopoverOpen: false,
rangeWidth: null,
};

rangeSliderRef = null;
handleRangeSliderRefUpdate = ref => {
this.rangeSliderRef = ref;
this.setState({
rangeSliderRefAvailable: !!ref,
rangeWidth: !!ref ? ref.clientWidth : null,
});
};

Expand Down Expand Up @@ -206,7 +209,7 @@ export class EuiDualRange extends Component {
this._handleOnChange(this.lowerValue, upper, e);
};

calculateThumbPositionStyle = value => {
calculateThumbPositionStyle = (value, width) => {
// Calculate the left position based on value
const decimal =
(value - this.props.min) / (this.props.max - this.props.min);
Expand All @@ -215,7 +218,11 @@ export class EuiDualRange extends Component {
valuePosition = valuePosition >= 0 ? valuePosition : 0;

const EUI_THUMB_SIZE = 16;
const thumbToTrackRatio = EUI_THUMB_SIZE / this.rangeSliderRef.clientWidth;
const trackWidth =
this.props.showInput === 'only' && !!width
? width
: this.rangeSliderRef.clientWidth;
const thumbToTrackRatio = EUI_THUMB_SIZE / trackWidth;
const trackPositionScale = (1 - thumbToTrackRatio) * 100;
return { left: `${valuePosition * trackPositionScale}%` };
};
Expand Down Expand Up @@ -246,6 +253,12 @@ export class EuiDualRange extends Component {
});
};

onResize = ({ width }) => {
this.setState({
rangeWidth: width,
});
};

render() {
const {
className,
Expand Down Expand Up @@ -397,7 +410,10 @@ export class EuiDualRange extends Component {
onKeyDown={this.handleLowerKeyDown}
onFocus={() => this.toggleHasFocus(true)}
onBlur={() => this.toggleHasFocus(false)}
style={this.calculateThumbPositionStyle(this.lowerValue || min)}
style={this.calculateThumbPositionStyle(
this.lowerValue || min,
this.state.rangeWidth
)}
aria-describedby={this.props['aria-describedby']}
aria-label={this.props['aria-label']}
/>
Expand All @@ -411,7 +427,10 @@ export class EuiDualRange extends Component {
onKeyDown={this.handleUpperKeyDown}
onFocus={() => this.toggleHasFocus(true)}
onBlur={() => this.toggleHasFocus(false)}
style={this.calculateThumbPositionStyle(this.upperValue || max)}
style={this.calculateThumbPositionStyle(
this.upperValue || max,
this.state.rangeWidth
)}
aria-describedby={this.props['aria-describedby']}
aria-label={this.props['aria-label']}
/>
Expand Down Expand Up @@ -440,7 +459,9 @@ export class EuiDualRange extends Component {
isOpen={this.state.isPopoverOpen}
closePopover={this.closePopover}
disableFocusTrap={true}>
{theRange}
<EuiResizeObserver onResize={this.onResize}>
{resizeRef => <div ref={resizeRef}>{theRange}</div>}
</EuiResizeObserver>
</EuiInputPopover>
) : (
undefined
Expand Down

0 comments on commit 9a6d9de

Please sign in to comment.