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

EuiWrappingPopover example in popover docs for help #5102

Closed
wants to merge 1 commit into from
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
113 changes: 113 additions & 0 deletions src-docs/src/views/popover/elastic_charts_wrapping_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import React, { useMemo, useState } from 'react';
import {
Axis,
BarSeries,
Chart,
Position,
ScaleType,
Settings,
toEntries,
} from '@elastic/charts';

import {
EuiColorPicker,
EuiButton,
EuiButtonEmpty,
EuiWrappingPopover,
EuiFlexItem,
EuiSpacer,
} from '../../../../src/components';

export const ElasticChartsColorPickerStoryExample = () => {
const [colors, setColors] = useState<Record>({});

const CustomColorPicker = useMemo(
() => ({ anchor, color, onClose, seriesIdentifiers, onChange }) => {
const handleClose = () => {
onClose();
anchor.focus();
setColors((prevColors) => ({
...prevColors,
...toEntries(seriesIdentifiers, 'key', color),
}));
};
const handleChange = (c) => {
setColors((prevColors) => ({
...prevColors,
...toEntries(seriesIdentifiers, 'key', c),
}));
onChange(c);
};

return (
<>
<EuiWrappingPopover
isOpen
button={anchor}
closePopover={handleClose}
anchorPosition="leftCenter"
// ownFocus={false}
>
<EuiColorPicker
display="inline"
color={color}
onChange={handleChange}
/>
<EuiSpacer size="m" />
<EuiFlexItem grow={false}>
<EuiButtonEmpty size="s" onClick={() => handleChange(null)}>
Clear color
</EuiButtonEmpty>
</EuiFlexItem>
<EuiButton fullWidth size="s" onClick={handleClose}>
Done
</EuiButton>
</EuiWrappingPopover>
</>
);
},
[setColors]
);
CustomColorPicker.displayName = 'CustomColorPicker';

const chart = (
<>
<Chart size={{ height: 200 }}>
<Settings showLegend legendColorPicker={CustomColorPicker} />
<Axis
id="bottom"
position={Position.Bottom}
title="Bottom axis"
showOverlappingTicks
/>
<Axis
id="left2"
title="Left axis"
position={Position.Left}
tickFormat={(d) => Number(d).toFixed(2)}
/>

<BarSeries
id="bars 1"
xScaleType={ScaleType.Linear}
yScaleType={ScaleType.Linear}
xAccessor="x"
yAccessors={['y']}
splitSeriesAccessors={['g']}
data={[
{ x: 0, g: 'a', y: 1 },
{ x: 0, g: 'b', y: 2 },
{ x: 1, g: 'a', y: 2 },
{ x: 1, g: 'b', y: 3 },
{ x: 2, g: 'a', y: 3 },
{ x: 2, g: 'b', y: 4 },
{ x: 3, g: 'a', y: 4 },
{ x: 3, g: 'b', y: 5 },
]}
color={({ key }) => colors[key] ?? null}
/>
</Chart>
</>
);
return chart;
};
8 changes: 8 additions & 0 deletions src-docs/src/views/popover/popover_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import InputPopover from './input_popover';
const inputPopoverSource = require('!!raw-loader!./input_popover');
const inputPopoverHtml = renderToHtml(PopoverBlock);

import { ElasticChartsColorPickerStoryExample } from './elastic_charts_wrapping_example';

const popOverSnippet = `<EuiPopover
button={button}
isOpen={isPopoverOpen}
Expand Down Expand Up @@ -462,5 +464,11 @@ export const PopoverExample = {
),
demo: <PopoverHTMLElementAnchor />,
},
{
title: 'Eui Wrapping Popover color picker within elastic charts',
text:
'Please help me. I must be missing something potentially really obvious. Thank you in advance.',
demo: <ElasticChartsColorPickerStoryExample />,
},
],
};
2 changes: 2 additions & 0 deletions src-docs/src/views/popover/popover_htmlelement_anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState, useEffect } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';

import { EuiWrappingPopover } from '../../../../src/components';
import { EuiButton } from '../../../../src/components/button';

const PopoverApp = (props) => {
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
Expand All @@ -23,6 +24,7 @@ const PopoverApp = (props) => {
isOpen={isPopoverOpen}
closePopover={closePopover}>
<div>Normal JSX content populates the popover.</div>
<EuiButton onClick={closePopover}>Done</EuiButton>
</EuiWrappingPopover>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ const DEFAULT_POPOVER_STYLES = {
left: 50,
};

function getElementFromInitialFocus(
export function getElementFromInitialFocus(
initialFocus?: FocusTarget
): HTMLElement | null {
const initialFocusType = typeof initialFocus;
Expand Down
23 changes: 22 additions & 1 deletion src/components/popover/wrapping_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/

import React, { Component } from 'react';
import { EuiPopover, Props as EuiPopoverProps } from './popover';
import {
EuiPopover,
// getElementFromInitialFocus,
Props as EuiPopoverProps,
} from './popover';
import { EuiPortal } from '../portal';

export interface EuiWrappingPopoverProps extends EuiPopoverProps {
Expand Down Expand Up @@ -35,6 +39,7 @@ export class EuiWrappingPopover extends Component<EuiWrappingPopoverProps> {
this.portal.insertAdjacentElement('beforebegin', this.props.button);
}
}
// this.updateFocus();
}

setPortalRef = (node: HTMLElement | null) => {
Expand All @@ -45,6 +50,21 @@ export class EuiWrappingPopover extends Component<EuiWrappingPopoverProps> {
this.anchor = node;
};

// updateFocus = () => {
// if (
// this.hasSetInitialFocus &&
// this.panel.contains(document.activeElement)
// ) {
// return;
// }
// let focusTarget;

// if (this.props.initialFocus != null) {
// focusTarget = getElementFromInitialFocus(this.props.initialFocus);
// }
// focusTarget.focus();
// };

render() {
const { button, ...rest } = this.props;

Expand All @@ -53,6 +73,7 @@ export class EuiWrappingPopover extends Component<EuiWrappingPopoverProps> {
portalRef={this.setPortalRef}
insert={{ sibling: this.props.button, position: 'after' }}>
<EuiPopover
ownFocus
{...rest}
button={
<div
Expand Down