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

Into feature/range dropdowns #20

Closed
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions src/components/form/range/__snapshots__/dual_range.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`EuiDualRange allows value prop to accept empty strings 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -38,6 +39,7 @@ exports[`EuiDualRange allows value prop to accept numbers 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -98,6 +100,7 @@ exports[`EuiDualRange props compressed should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiRangeSlider--compressed euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -151,6 +154,7 @@ exports[`EuiDualRange props custom ticks should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasTicks euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -180,6 +184,7 @@ exports[`EuiDualRange props disabled should render 1`] = `
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
disabled=""
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -208,6 +213,7 @@ exports[`EuiDualRange props fullWidth should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -311,6 +317,7 @@ exports[`EuiDualRange props labels should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -356,6 +363,7 @@ exports[`EuiDualRange props levels should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -434,6 +442,7 @@ exports[`EuiDualRange props range should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -522,6 +531,7 @@ exports[`EuiDualRange props ticks should render 1`] = `
<input
aria-hidden="true"
class="euiRangeSlider euiRangeSlider--hasTicks euiRangeSlider--hasRange euiDualRange__slider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down
11 changes: 11 additions & 0 deletions src/components/form/range/__snapshots__/range.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`EuiRange allows value prop to accept a number 1`] = `
>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -38,6 +39,7 @@ exports[`EuiRange allows value prop to accept empty string 1`] = `
>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -80,6 +82,7 @@ exports[`EuiRange props compressed should render 1`] = `
>
<input
class="euiRangeSlider euiRangeSlider--compressed"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -123,6 +126,7 @@ exports[`EuiRange props custom ticks should render 1`] = `
</div>
<input
class="euiRangeSlider euiRangeSlider--hasTicks"
id="generated-id"
max="100"
min="0"
step="1"
Expand All @@ -142,6 +146,7 @@ exports[`EuiRange props disabled should render 1`] = `
<input
class="euiRangeSlider"
disabled=""
id="generated-id"
max="100"
min="0"
step="1"
Expand All @@ -160,6 +165,7 @@ exports[`EuiRange props fullWidth should render 1`] = `
>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -259,6 +265,7 @@ exports[`EuiRange props labels should render 1`] = `
>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -294,6 +301,7 @@ exports[`EuiRange props levels should render 1`] = `
</div>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand All @@ -320,6 +328,7 @@ exports[`EuiRange props range should render 1`] = `
</div>
<input
class="euiRangeSlider euiRangeSlider--hasRange"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down Expand Up @@ -399,6 +408,7 @@ exports[`EuiRange props ticks should render 1`] = `
</div>
<input
class="euiRangeSlider euiRangeSlider--hasTicks"
id="generated-id"
max="100"
min="0"
step="1"
Expand All @@ -417,6 +427,7 @@ exports[`EuiRange props value should render 1`] = `
>
<input
class="euiRangeSlider"
id="generated-id"
max="100"
min="0"
step="1"
Expand Down
19 changes: 17 additions & 2 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 makeId from '../form_row/make_id';

import { EuiRangeHighlight } from './range_highlight';
import { EuiRangeInput } from './range_input';
Expand All @@ -17,6 +18,7 @@ import { EuiRangeWrapper } from './range_wrapper';

export class EuiDualRange extends Component {
state = {
id: this.props.id || makeId(),
hasFocus: false,
rangeSliderRefAvailable: false,
isPopoverOpen: false,
Expand Down Expand Up @@ -230,6 +232,14 @@ export class EuiDualRange extends Component {
});
};

onInputBlur = e => {
// Firefox returns `relatedTarget` as `null` for security reasons, but provides a proprietary `explicitOriginalTarget`
const relatedTarget = e.relatedTarget || e.explicitOriginalTarget;
if (!relatedTarget || relatedTarget.id !== this.state.id) {
this.closePopover();
}
};

closePopover = () => {
this.setState({
isPopoverOpen: false,
Expand All @@ -243,7 +253,7 @@ export class EuiDualRange extends Component {
disabled,
fullWidth,
readOnly,
id,
id: propsId,
max,
min,
name,
Expand All @@ -261,6 +271,8 @@ export class EuiDualRange extends Component {
...rest
} = this.props;

const { id } = this.state;

const digitTolerance = Math.max(String(min).length, String(max).length);
const showInputOnly = showInput === 'only';
const canShowDropdown = showInputOnly && !readOnly && !disabled;
Expand All @@ -281,6 +293,7 @@ export class EuiDualRange extends Component {
aria-describedby={this.props['aria-describedby']}
aria-label={this.props['aria-label']}
onFocus={canShowDropdown ? this.onInputFocus : undefined}
onBlur={canShowDropdown ? this.onInputBlur : undefined}
readOnly={readOnly}
autoSize={!showInputOnly}
fullWidth={!!showInputOnly && fullWidth}
Expand All @@ -306,6 +319,7 @@ export class EuiDualRange extends Component {
aria-describedby={this.props['aria-describedby']}
aria-label={this.props['aria-label']}
onFocus={canShowDropdown ? this.onInputFocus : undefined}
onBlur={canShowDropdown ? this.onInputBlur : undefined}
readOnly={readOnly}
autoSize={!showInputOnly}
fullWidth={!!showInputOnly && fullWidth}
Expand Down Expand Up @@ -424,7 +438,8 @@ export class EuiDualRange extends Component {
}
fullWidth={fullWidth}
isOpen={this.state.isPopoverOpen}
closePopover={this.closePopover}>
closePopover={this.closePopover}
disableFocusTrap={true}>
{theRange}
</EuiInputPopover>
) : (
Expand Down
2 changes: 2 additions & 0 deletions src/components/form/range/dual_range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { requiredProps } from '../../../test/required_props';

import { EuiDualRange } from './dual_range';

jest.mock('../form_row/make_id', () => () => 'generated-id');

describe('EuiDualRange', () => {
test('is rendered', () => {
const component = render(
Expand Down
18 changes: 16 additions & 2 deletions src/components/form/range/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames';

import { isWithinRange } from '../../../services/number';
import { EuiInputPopover } from '../../popover';
import makeId from '../form_row/make_id';

import { EuiRangeHighlight } from './range_highlight';
import { EuiRangeInput } from './range_input';
Expand All @@ -17,6 +18,7 @@ export class EuiRange extends Component {
constructor(props) {
super(props);
this.state = {
id: props.id || makeId(),
isPopoverOpen: false,
};
}
Expand All @@ -40,6 +42,14 @@ export class EuiRange extends Component {
});
};

onInputBlur = e => {
// Firefox returns `relatedTarget` as `null` for security reasons, but provides a proprietary `explicitOriginalTarget`
const relatedTarget = e.relatedTarget || e.explicitOriginalTarget;
if (!relatedTarget || relatedTarget.id !== this.state.id) {
this.closePopover();
}
};

closePopover = () => {
this.setState({
isPopoverOpen: false,
Expand All @@ -53,7 +63,7 @@ export class EuiRange extends Component {
disabled,
fullWidth,
readOnly,
id,
id: propsId,
max,
min,
name,
Expand All @@ -75,6 +85,8 @@ export class EuiRange extends Component {
...rest
} = this.props;

const { id } = this.state;

const digitTolerance = Math.max(String(min).length, String(max).length);
const showInputOnly = showInput === 'only';
const canShowDropdown = showInputOnly && !readOnly && !disabled;
Expand All @@ -92,6 +104,7 @@ export class EuiRange extends Component {
onChange={this.handleOnChange}
name={name}
onFocus={canShowDropdown ? this.onInputFocus : undefined}
onBlur={canShowDropdown ? this.onInputBlur : undefined}
fullWidth={showInputOnly && fullWidth}
autoSize={!showInputOnly}
{...rest}
Expand Down Expand Up @@ -180,7 +193,8 @@ export class EuiRange extends Component {
input={theInput}
fullWidth={fullWidth}
isOpen={this.state.isPopoverOpen}
closePopover={this.closePopover}>
closePopover={this.closePopover}
disableFocusTrap={true}>
{theRange}
</EuiInputPopover>
) : (
Expand Down
2 changes: 2 additions & 0 deletions src/components/form/range/range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { requiredProps } from '../../../test/required_props';

import { EuiRange } from './range';

jest.mock('../form_row/make_id', () => () => 'generated-id');

describe('EuiRange', () => {
test('is rendered', () => {
const component = render(
Expand Down
9 changes: 6 additions & 3 deletions src/components/popover/input_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { cascadingMenuKeyCodes } from '../../services';

interface EuiInputPopoverProps
extends Omit<EuiPopoverProps, 'button' | 'buttonRef'> {
disableFocusTrap?: boolean;
fullWidth?: boolean;
input: EuiPopoverProps['button'];
inputRef?: EuiPopoverProps['buttonRef'];
Expand All @@ -27,6 +28,7 @@ type Props = CommonProps &
export const EuiInputPopover: FunctionComponent<Props> = ({
children,
className,
disableFocusTrap = false,
input,
fullWidth = false,
...props
Expand Down Expand Up @@ -68,8 +70,9 @@ export const EuiInputPopover: FunctionComponent<Props> = ({
);
});
if (
tabbableItems.length &&
tabbableItems[tabbableItems.length - 1] === document.activeElement
disableFocusTrap ||
(tabbableItems.length &&
tabbableItems[tabbableItems.length - 1] === document.activeElement)
) {
props.closePopover();
}
Expand All @@ -96,7 +99,7 @@ export const EuiInputPopover: FunctionComponent<Props> = ({
panelRef={panelRef}
className={classes}
{...props}>
<EuiFocusTrap clickOutsideDisables={true}>
<EuiFocusTrap clickOutsideDisables={true} disabled={disableFocusTrap}>
<div onKeyDown={onKeyDown}>{children}</div>
</EuiFocusTrap>
</EuiPopover>
Expand Down