Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiDualRange thumb position on resize #26

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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