Skip to content

Commit

Permalink
[BETA] EuiInputPopover (#2197)
Browse files Browse the repository at this point in the history
* inputpopover component

* popover provides attached styles now

* fullWidth
  • Loading branch information
thompsongl authored Aug 2, 2019
1 parent 496a3e6 commit a0640be
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 17 deletions.
14 changes: 0 additions & 14 deletions src/components/color_picker/_color_picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,3 @@
// sass-lint:disable no-important
padding-bottom: 0 !important;
}

.euiColorPicker__popoverPanel[class*='bottom']:not(.euiColorPicker__popoverPanel--customButton) {
border-top-color: transparentize($euiBorderColor, .2);
border-top-right-radius: 0;
border-top-left-radius: 0;
}

.euiColorPicker__popoverPanel[class*='top']:not(.euiColorPicker__popoverPanel--customButton) {
@include euiBottomShadowFlat;

border-bottom-color: transparentize($euiBorderColor, .2);
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
2 changes: 1 addition & 1 deletion src/components/form/range/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class EuiRange extends Component {
style={style}
showTicks={showTicks}
showRange={showRange}
tabIndex={showInput ? -1 : tabIndex || null}
tabIndex={showInput === true ? -1 : tabIndex || null}
{...rest}
/>

Expand Down
1 change: 1 addition & 0 deletions src/components/popover/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
@import 'popover';
@import 'popover_title';
@import 'popover_footer';
@import 'input_popover';
5 changes: 5 additions & 0 deletions src/components/popover/_input_popover.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.euiInputPopover {
&.euiInputPopover--fullWidth {
max-width: 100%;
}
}
14 changes: 14 additions & 0 deletions src/components/popover/_popover.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@
&.euiPopover__panel-noArrow .euiPopover__panelArrow {
display: none;
}

&.euiPopover__panel-isAttached.euiPopover__panel--bottom {
border-top-color: transparentize($euiBorderColor, .2);
border-top-right-radius: 0;
border-top-left-radius: 0;
}

&.euiPopover__panel-isAttached.euiPopover__panel--top {
@include euiBottomShadowFlat;

border-bottom-color: transparentize($euiBorderColor, .2);
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
}

.euiPopover__panel.euiPopover__panel-isOpen.euiPopover__panel--top {
Expand Down
1 change: 1 addition & 0 deletions src/components/popover/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { EuiInputPopover } from './input_popover';
export { EuiPopover } from './popover';
export { EuiPopoverTitle } from './popover_title';
export { EuiPopoverFooter } from './popover_footer';
Expand Down
89 changes: 89 additions & 0 deletions src/components/popover/input_popover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import tabbable from 'tabbable';

import { EuiFocusTrap } from '../focus_trap';
import { EuiPopover, EuiPopoverPropTypes } from './popover';
import { cascadingMenuKeyCodes } from '../../services';

export const EuiInputPopover = ({
children,
className,
fullWidth,
input,
popoverZIndex,
...props
}) => {
const [inputEl, setInputEl] = useState();
const [inputElWidth, setInputElWidth] = useState();
const [panelEl, setPanelEl] = useState();
const inputRef = node => setInputEl(node);
const panelRef = node => setPanelEl(node);
const setPanelWidth = () => {
if (panelEl && inputElWidth) {
panelEl.style.width = `${inputElWidth}px`;
}
};
useEffect(() => {
if (inputEl) {
const width = inputEl.getBoundingClientRect().width;
setInputElWidth(width);
setPanelWidth();
}
}, [inputEl]);
useEffect(() => {
setPanelWidth();
}, [panelEl]);
const onKeyDown = e => {
if (e.keyCode === cascadingMenuKeyCodes.TAB) {
const tabbableItems = tabbable(panelEl).filter(el => {
return (
[...el.attributes].map(el => el.name).indexOf('data-focus-guard') < 0
);
});
if (
tabbableItems.length &&
tabbableItems[tabbableItems.length - 1] === document.activeElement
) {
props.closePopover();
}
}
};
const classes = classnames(
'euiInputPopover',
{
'euiInputPopover--fullWidth': fullWidth,
},
className
);
return (
<EuiPopover
ownFocus={false}
button={input}
buttonRef={inputRef}
panelRef={panelRef}
className={classes}
{...props}>
<EuiFocusTrap clickOutsideDisables={true}>
<div onKeyDown={onKeyDown}>{children}</div>
</EuiFocusTrap>
</EuiPopover>
);
};

const { button, buttonRef, ...propTypes } = EuiPopoverPropTypes;
EuiInputPopover.propTypes = {
fullWidth: PropTypes.bool,
input: EuiPopoverPropTypes.button,
inputRef: EuiPopoverPropTypes.buttonRef,
...propTypes,
};

EuiInputPopover.defaultProps = {
anchorPosition: 'downLeft',
attachToAnchor: true,
display: 'block',
fullWidth: false,
panelPaddingSize: 's',
};
15 changes: 13 additions & 2 deletions src/components/popover/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export class EuiPopover extends Component {

panelRef = node => {
this.panel = node;
this.props.panelRef && this.props.panelRef(node);

if (node == null) {
// panel has unmounted, restore the state defaults
Expand All @@ -363,13 +364,17 @@ export class EuiPopover extends Component {
}
};

buttonRef = node => (this.button = node);
buttonRef = node => {
this.button = node;
this.props.buttonRef && this.props.buttonRef(node);
};

render() {
const {
anchorClassName,
anchorPosition,
button,
buttonRef,
insert,
isOpen,
ownFocus,
Expand All @@ -379,6 +384,7 @@ export class EuiPopover extends Component {
closePopover,
panelClassName,
panelPaddingSize,
panelRef,
popoverRef,
hasArrow,
repositionOnScroll,
Expand Down Expand Up @@ -408,6 +414,7 @@ export class EuiPopover extends Component {
{ 'euiPopover__panel-isOpen': this.state.isOpening },
{ 'euiPopover__panel-withTitle': withTitle },
{ 'euiPopover__panel-noArrow': !hasArrow || attachToAnchor },
{ 'euiPopover__panel-isAttached': attachToAnchor },
panelClassName
);

Expand Down Expand Up @@ -497,17 +504,19 @@ export class EuiPopover extends Component {
}
}

EuiPopover.propTypes = {
export const EuiPopoverPropTypes = {
anchorClassName: PropTypes.string,
anchorPosition: PropTypes.oneOf(ANCHOR_POSITIONS),
isOpen: PropTypes.bool,
ownFocus: PropTypes.bool,
withTitle: PropTypes.bool,
closePopover: PropTypes.func.isRequired,
button: PropTypes.node.isRequired,
buttonRef: PropTypes.func,
children: PropTypes.node,
panelClassName: PropTypes.string,
panelPaddingSize: PropTypes.oneOf(SIZES),
panelRef: PropTypes.func,
popoverRef: PropTypes.func,
hasArrow: PropTypes.bool,
container: PropTypes.instanceOf(HTMLElement),
Expand All @@ -532,6 +541,8 @@ EuiPopover.propTypes = {
display: PropTypes.oneOf(DISPLAY),
};

EuiPopover.propTypes = EuiPopoverPropTypes;

EuiPopover.defaultProps = {
isOpen: false,
ownFocus: false,
Expand Down

0 comments on commit a0640be

Please sign in to comment.