Skip to content
Merged
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
17 changes: 5 additions & 12 deletions src-docs/src/views/notification_event/notification_event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import React, { useState } from 'react';
import { EuiPanel } from '../../../../src/components/panel';
import { EuiSpacer } from '../../../../src/components/spacer';
import { EuiButtonGroup } from '../../../../src/components/button';
import {
EuiContextMenuItem,
EuiContextMenuPanelProps,
} from '../../../../src/components/context_menu';
import { EuiContextMenuItem } from '../../../../src/components/context_menu';
import { EuiNotificationEvent } from '../../../../src/components/notification/notification_event';

const notificationEventsData = [
Expand Down Expand Up @@ -65,18 +62,17 @@ const notificationEventsData = [

export default () => {
const [event, setEvent] = useState<any>(notificationEventsData[0]);
const [contextMenuItems, setContextMenuItems] = useState<
EuiContextMenuPanelProps['items']
>();

const onRead = (id: string, isRead: boolean) => {
const nextState = { ...event, isRead: !isRead };

setEvent(nextState);
};

const onOpenContextMenu = (id: string, isRead: boolean) => {
const nextContextMenus = [
const onOpenContextMenu = (id: string) => {
const { isRead } = event;

return [
<EuiContextMenuItem
key="contextMenuItemA"
onClick={() => onRead(id, isRead)}>
Expand All @@ -91,8 +87,6 @@ export default () => {
Don’t notify me about this
</EuiContextMenuItem>,
];

setContextMenuItems(nextContextMenus);
};

const [toggleIdSelected, setToggleIdSelected] = useState('reportButton');
Expand Down Expand Up @@ -143,7 +137,6 @@ export default () => {
primaryAction={event.primaryAction}
messages={event.messages}
onRead={onRead}
contextMenuItems={contextMenuItems}
onOpenContextMenu={onOpenContextMenu}
onClickPrimaryAction={() => {}}
onClickTitle={event.id !== 'news' ? () => {} : undefined}
Expand Down
22 changes: 9 additions & 13 deletions src-docs/src/views/notification_event/notifications_feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import React, { useState } from 'react';
import { EuiPanel } from '../../../../src/components/panel';
import { EuiSpacer } from '../../../../src/components/spacer';
import { EuiButton } from '../../../../src/components/button';
import {
EuiContextMenuItem,
EuiContextMenuPanelProps,
} from '../../../../src/components/context_menu';
import { EuiContextMenuItem } from '../../../../src/components/context_menu';
import { EuiNotificationEvent } from '../../../../src/components/notification/notification_event';

const notificationEventsData = [
Expand Down Expand Up @@ -93,9 +90,6 @@ const notificationEventsData = [

export default () => {
const [events, setEvents] = useState(notificationEventsData);
const [contextMenuItems, setContextMenuItems] = useState<
EuiContextMenuPanelProps['items']
>();

const onRead = (id: string, isRead: boolean) => {
const nextState = events.map((event) => {
Expand All @@ -111,12 +105,17 @@ export default () => {
setEvents(nextState);
};

const onOpenContextMenu = (id: string, isRead: boolean, type: string) => {
const nextContextMenus = [
const onOpenContextMenu = (id: string) => {
const {
isRead,
meta: { type },
} = events.find(({ id: eventId }) => eventId === id)!;

return [
<EuiContextMenuItem
key="contextMenuItemA"
onClick={() => onRead(id, isRead)}>
{isRead ? ' Mark as unread' : 'Mark as read'}
{isRead ? 'Mark as unread' : 'Mark as read'}
</EuiContextMenuItem>,

<EuiContextMenuItem
Expand All @@ -129,8 +128,6 @@ export default () => {
Don’t notify me about this
</EuiContextMenuItem>,
];

setContextMenuItems(nextContextMenus);
};

const onResetData = () => {
Expand All @@ -151,7 +148,6 @@ export default () => {
primaryAction={event.primaryAction}
messages={event.messages}
onRead={onRead}
contextMenuItems={contextMenuItems}
onOpenContextMenu={onOpenContextMenu}
onClickPrimaryAction={() => {}}
onClickTitle={onClickTitle!}
Expand Down
20 changes: 7 additions & 13 deletions src/components/notification/notification_event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import React, { FunctionComponent } from 'react';
import React, { FunctionComponent, ReactElement } from 'react';
import classNames from 'classnames';
import {
EuiNotificationEventMeta,
Expand All @@ -28,8 +28,8 @@ import {
EuiNotificationEventMessagesProps,
} from './notification_event_messages';
import { EuiButtonEmpty, EuiButtonEmptyProps } from '../button';
import { EuiContextMenuPanelProps } from '../context_menu';
import { EuiLink } from '../link';
import { EuiContextMenuItem, EuiContextMenuItemProps } from '../context_menu';

export type EuiNotificationEventPrimaryActionProps = EuiButtonEmptyProps & {
label: string;
Expand Down Expand Up @@ -70,13 +70,11 @@ export type EuiNotificationEventProps = {
*/
onRead?: (id: string, isRead: boolean) => void;
/**
* An array of context menu items. See #EuiContextMenuItem
* Provided the `id` of the event must return an array of #EuiContextMenuItem elements
*/
contextMenuItems?: EuiContextMenuPanelProps['items'];
/**
* Returns the `id`, `isRead` ad `type` of the open context menu
*/
onOpenContextMenu?: (id: string, isRead: boolean, type: string) => void;
onOpenContextMenu?: (
id: string
) => Array<ReactElement<EuiContextMenuItemProps, typeof EuiContextMenuItem>>;
};

export const EuiNotificationEvent: FunctionComponent<EuiNotificationEventProps> = ({
Expand All @@ -87,7 +85,6 @@ export const EuiNotificationEvent: FunctionComponent<EuiNotificationEventProps>
primaryAction,
messages,
onRead,
contextMenuItems,
onOpenContextMenu,
onClickTitle,
onClickPrimaryAction,
Expand All @@ -110,11 +107,8 @@ export const EuiNotificationEvent: FunctionComponent<EuiNotificationEventProps>
iconAriaLabel={meta.iconAriaLabel}
time={meta.time}
isRead={isRead}
contextMenuItems={contextMenuItems}
onOpenContextMenu={
onOpenContextMenu
? () => onOpenContextMenu(id, isRead!, meta.type)
: undefined
onOpenContextMenu ? () => onOpenContextMenu(id) : undefined
}
eventName={meta.eventName}
onRead={() => onRead?.(id, isRead!)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('EuiNotificationEventMeta', () => {
type="Alert"
time={<span>2 min ago</span>}
iconType="logoCloud"
contextMenuItems={contextMenuItems}
onOpenContextMenu={() => contextMenuItems}
eventName="eventName"
/>
);
Expand Down
39 changes: 26 additions & 13 deletions src/components/notification/notification_event_meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
* under the License.
*/

import React, { FunctionComponent, useState, ReactNode } from 'react';
import React, {
FunctionComponent,
useState,
ReactNode,
ReactElement,
} from 'react';
import classNames from 'classnames';
import { EuiIcon, IconType } from '../icon';
import { EuiBadge, EuiBadgeProps } from '../badge';
import { EuiPopover } from '../popover';
import { EuiButtonIcon } from '../button';
import { EuiContextMenuPanel, EuiContextMenuPanelProps } from '../context_menu';
import {
EuiContextMenuItem,
EuiContextMenuItemProps,
EuiContextMenuPanel,
} from '../context_menu';
import { EuiI18n } from '../i18n';
import {
EuiNotificationEventReadButton,
Expand Down Expand Up @@ -64,14 +73,12 @@ export type EuiNotificationEventMetaProps = Omit<
* Indicates when the event was received.
*/
time: ReactNode;
/**
* An array of context menu items. See #EuiContextMenuItem
*/
contextMenuItems?: EuiContextMenuPanelProps['items'];
/**
* Necessary to trigger onOpenContextMenu from EuiNotificationEvent
*/
onOpenContextMenu?: () => void;
onOpenContextMenu?: () => Array<
ReactElement<EuiContextMenuItemProps, typeof EuiContextMenuItem>
>;
/**
* Applies an `onClick` handler to the `read` indicator.
*/
Expand All @@ -89,14 +96,16 @@ export const EuiNotificationEventMeta: FunctionComponent<EuiNotificationEventMet
eventName,
iconAriaLabel,
onOpenContextMenu,
contextMenuItems = [],
}) => {
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
const classes = classNames('euiNotificationEventMeta', {
'euiNotificationEventMeta--hasContextMenu':
onOpenContextMenu || contextMenuItems.length > 0,
'euiNotificationEventMeta--hasContextMenu': onOpenContextMenu,
});

const [contextMenuItems, setContextMenuItems] = useState<
ReturnType<NonNullable<typeof onOpenContextMenu>>
>([]);

const id = htmlIdGenerator()();

const onMarkAsRead = () => {
Expand All @@ -109,7 +118,9 @@ export const EuiNotificationEventMeta: FunctionComponent<EuiNotificationEventMet

const onOpenPopover = () => {
setIsPopoverOpen(!isPopoverOpen);
onOpenContextMenu?.();
if (onOpenContextMenu) {
setContextMenuItems(onOpenContextMenu());
}
};

return (
Expand Down Expand Up @@ -138,7 +149,7 @@ export const EuiNotificationEventMeta: FunctionComponent<EuiNotificationEventMet
<span className="euiNotificationEventMeta__time">{time}</span>
</div>

{(onOpenContextMenu || contextMenuItems.length > 0) && (
{onOpenContextMenu && (
<div className="euiNotificationEventMeta__contextMenuWrapper">
<EuiPopover
id={id}
Expand Down Expand Up @@ -169,7 +180,9 @@ export const EuiNotificationEventMeta: FunctionComponent<EuiNotificationEventMet
</EuiI18n>
}
closePopover={() => setIsPopoverOpen(false)}>
<EuiContextMenuPanel items={contextMenuItems} />
<div onClick={() => setIsPopoverOpen(false)}>
<EuiContextMenuPanel items={contextMenuItems} />
</div>
</EuiPopover>
</div>
)}
Expand Down