diff --git a/packages/grafana-ui/src/components/Card/Card.mdx b/packages/grafana-ui/src/components/Card/Card.mdx index d98f13e84ce..7c5622615ac 100644 --- a/packages/grafana-ui/src/components/Card/Card.mdx +++ b/packages/grafana-ui/src/components/Card/Card.mdx @@ -2,6 +2,7 @@ import { Meta, Preview, Props } from "@storybook/addon-docs/blocks"; import { Card } from "./Card"; import { Button } from '../Button'; import { IconButton } from '../IconButton/IconButton'; +import {TagList} from '../Tags/TagList'; export const logo = 'https://grafana.com/static/assets/img/apple-touch-icon.png' @@ -12,7 +13,7 @@ export const logo = 'https://grafana.com/static/assets/img/apple-touch-icon.png' ## Usage ### Basic -A basic Card component expects at least a heading to be used as title. Optionally a `metadata` prop is accepted, to provide some secondary information for the card. Multiple meta data elements can be provided as an array, in which case they will be separated by a horizontal line: `|`. +A basic `Card` component expects at least a heading, used as a title. ```jsx + + + {['Folder: Test', 'Views: 100']} + + ``` - console.log('clicked tag:', tag) } - /> + + + {['Folder: Test', 'Views: 100']} + + -Metadata also accepts html elements, which could be links, for example. For elements, that are not strings, a `key` prop has to be manually specified. +Metadata also accepts HTML elements, which could be links, for example. For elements, that are not strings, a `key` prop has to be manually specified. + +```jsx + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + +``` + + + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + + + +### Tags +Tags can be rendered inside the Card, by being wrapped in `Card.Tags` component. Note that this component does not provide any tag styling and that should be handled by the children. It is recommended to use it with Grafana-UI's `TagList` component. ```jsx https://ops-us-east4.grafana.net/api/prom, - ]} -/> + description="Card with a list of tags" +> + + console.log(tag)} /> + + ``` https://ops-us-east4.grafana.net/api/prom, - ]} - /> + description="Card with a list of tags" + > + + console.log(tag)} /> + + ### As a link + Card can be used as a clickable link item by specifying `href` prop. In this case the Card's content will be rendered inside `a`. ```jsx @@ -148,135 +174,150 @@ To render cards in a list, it is possible to nest them inside `li` items. ### With media elements -Cards can also be rendered with media content such icons or images. +Cards can also be rendered with media content such icons or images. Such elements need to be wrapped in `Card.Figure` component. ```jsx -https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} -/> + + + Grafana Logo + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + ``` - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} - /> + + + Grafana Logo + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + ### Action Cards -Cards also accept primary and secondary actions. Usually the primary actions are displayed as buttons while secondary actions are displayed as icon buttons. +Cards also accept primary and secondary actions. Usually the primary actions are displayed as buttons while secondary actions are displayed as icon buttons. The actions need to be wrappd in `Card.Actions` and `Card.SecondaryActions` components respectively. ```jsx - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} - actions={[, ]} - secondaryActions={[ - , - , - ]} -/> + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + + + + + + + + + ``` - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} - actions={[, - , - ]} - secondaryActions={[ - , - , - ]} - /> + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + + + + + + + + + ### Disabled state -Card can have a disabled state, effectively making it and its actions non-clickable. If there is more than one primary action, disabled state will disable them instead of the whole card. +Card can have a disabled state, effectively making it and its actions non-clickable. If there are any actions, they will be disabled instead of the whole card. ```jsx - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} disabled -/> +> + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + ``` https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} disabled - /> + > + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + ```jsx - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} - actions={[, ]} - secondaryActions={[ - , - , - ]} - disabled -/> + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + + + + + + + + + ``` - https://ops-us-east4.grafana.net/api/prom, - ]} - image={Grafana Logo} - actions={[, ]} - secondaryActions={[ - , - , - ]} - disabled - /> + + + Grafana + https://ops-us-east4.grafana.net/api/prom + + + Grafana Logo + + + + + + + + + + - ### Props diff --git a/packages/grafana-ui/src/components/Card/Card.story.internal.tsx b/packages/grafana-ui/src/components/Card/Card.story.internal.tsx index 533c08da559..0288365c65a 100644 --- a/packages/grafana-ui/src/components/Card/Card.story.internal.tsx +++ b/packages/grafana-ui/src/components/Card/Card.story.internal.tsx @@ -1,10 +1,12 @@ import React from 'react'; import { boolean } from '@storybook/addon-knobs'; +import { action } from '@storybook/addon-actions'; import { withCenteredStory } from '../../utils/storybook/withCenteredStory'; import { Card } from './Card'; import mdx from './Card.mdx'; import { Button } from '../Button'; import { IconButton } from '../IconButton/IconButton'; +import { TagList } from '../Tags/TagList'; const logo = 'https://grafana.com/static/assets/img/apple-touch-icon.png'; @@ -63,58 +65,57 @@ export const WithTooltip = () => { export const WithTags = () => { const { disabled } = getKnobs(); return ( - + + Elastic Search + + console.log('tag', tag)} /> + + ); }; export const WithMedia = () => { const { disabled } = getKnobs(); return ( - + + Prometheus https://ops-us-east4.grafana.net/api/prom - , - ]} - disabled={disabled} - image={Prometheus Logo} - /> + + + + Prometheus Logo + + ); }; export const WithActions = () => { const { disabled } = getKnobs(); return ( - + + + Prometheus + https://ops-us-east4.grafana.net/api/prom - , - ]} - disabled={disabled} - image={Prometheus Logo} - actions={[ + + + + Prometheus Logo + + , + , - ]} - secondaryActions={[ - , - , - ]} - /> + + + + + + + ); }; @@ -124,33 +125,36 @@ export const Full = () => { return ( + + {['Subtitle', 'Meta info 1', 'Meta info 2']} https://ops-us-east4.grafana.net/api/prom - , - ]} - disabled={disabled} - image={Prometheus Logo} - tags={['firing', 'active', 'test', 'testdata', 'prometheus']} - description="Description, body text. Greetings! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." - actions={[ + + + + Prometheus Logo + + + + + , + , - ]} - secondaryActions={[ - , - , - , - , - , - ]} - /> + + + + + + + + + + ); }; diff --git a/packages/grafana-ui/src/components/Card/Card.tsx b/packages/grafana-ui/src/components/Card/Card.tsx index 45bc6e43be8..da5cd338ca5 100644 --- a/packages/grafana-ui/src/components/Card/Card.tsx +++ b/packages/grafana-ui/src/components/Card/Card.tsx @@ -1,13 +1,11 @@ -import React, { cloneElement, FC, HTMLAttributes, ReactElement, ReactNode, useCallback, useMemo } from 'react'; +import React, { memo, cloneElement, FC, HTMLAttributes, ReactNode, useCallback } from 'react'; import { css, cx } from 'emotion'; import { GrafanaTheme } from '@grafana/data'; import { useTheme, styleMixins, stylesFactory } from '../../themes'; import { Tooltip, PopoverContent } from '../Tooltip/Tooltip'; -import { OnTagClick } from '../Tags/Tag'; -import { TagList } from '../Tags/TagList'; /** - * @alpha + * @public */ export interface ContainerProps extends HTMLAttributes { /** Content for the card's tooltip */ @@ -25,7 +23,7 @@ const CardContainer: FC = ({ children, tooltip, ...props }) => { }; /** - * @alpha + * @public */ export interface CardInnerProps { href?: string; @@ -44,100 +42,88 @@ const CardInner: FC = ({ children, href }) => { }; /** - * @alpha + * @public */ export interface Props extends ContainerProps { /** Main heading for the Card **/ heading: ReactNode; - /** Additional data about the card. If array is supplied, elements will be rendered with vertical line separator */ - metadata?: ReactNode | ReactNode[]; /** Card description text */ description?: string; - /** List of tags to display in the card */ - tags?: string[]; - /** Optional callback for tag onclick event */ - onTagClick?: OnTagClick; /** Indicates if the card and all its actions can be interacted with */ disabled?: boolean; - /** Image or icon to be displayed on the let side of the card */ - image?: ReactNode; - /** Main card actions **/ - actions?: ReactElement[]; - /** Right-side actions */ - secondaryActions?: ReactElement[]; /** Link to redirect to on card click. If provided, the Card inner content will be rendered inside `a` */ href?: string; /** On click handler for the Card */ onClick?: () => void; } +export interface CardInterface extends FC { + Tags: typeof Tags; + Figure: typeof Figure; + Meta: typeof Meta; + Actions: typeof Actions; + SecondaryActions: typeof SecondaryActions; +} + /** * Generic card component * - * @alpha + * @public */ -export const Card: FC = ({ +export const Card: CardInterface = ({ heading, description, - metadata, - tags = [], - onTagClick, disabled, - image, - actions = [], tooltip, - secondaryActions = [], href, onClick, className, + children, ...htmlProps }) => { - const hasActions = Boolean(actions.length || secondaryActions.length); - const disableHover = disabled || actions.length > 1 || !onClick; - const disableEvents = disabled && !actions.length; const theme = useTheme(); - const styles = getCardStyles(theme, disableEvents, disableHover); - // Join meta data elements by '|' - const meta = useMemo( - () => - Array.isArray(metadata) - ? (metadata as ReactNode[]).filter(Boolean).reduce((prev, curr, i) => [ - prev, - - | - , - curr, - ]) - : metadata, - [metadata, styles.separator] + const styles = getCardStyles(theme); + const [tags, figure, meta, actions, secondaryActions] = ['Tags', 'Figure', 'Meta', 'Actions', 'SecondaryActions'].map( + item => { + const found = React.Children.toArray(children as React.ReactElement[]).find(child => { + return child?.type && (child.type as any).displayName === item; + }); + + if (found) { + return React.cloneElement(found, { disabled, styles, ...found.props }); + } + return found; + } ); + + const hasActions = Boolean(actions || secondaryActions); + const disableHover = disabled || !onClick; + const disableEvents = disabled && !actions; + + const containerStyles = getContainerStyles(theme, disableEvents, disableHover); const onCardClick = useCallback(() => (disableHover ? () => {} : onClick), [disableHover, onClick]); return ( - {image &&
{image}
} + {figure}
-
{heading}
- {meta &&
{meta}
} - {!!tags.length && } +
+ {heading} +
+ {meta} + {tags} {description &&

{description}

} {hasActions && (
- {!!actions.length && ( -
{actions.map(action => cloneElement(action, { disabled }))}
- )} - {!!secondaryActions.length && ( -
- {secondaryActions.map(action => cloneElement(action, { disabled }))} -
- )} + {actions} + {secondaryActions}
)}
@@ -147,44 +133,50 @@ export const Card: FC = ({ }; /** - * @alpha + * @public */ -export const getCardStyles = stylesFactory((theme: GrafanaTheme, disabled = false, disableHover = false) => { - return { - container: css` - display: flex; - width: 100%; - color: ${theme.colors.textStrong}; - background: ${theme.colors.bg2}; +export const getContainerStyles = stylesFactory((theme: GrafanaTheme, disabled = false, disableHover = false) => { + return css` + display: flex; + width: 100%; + color: ${theme.colors.textStrong}; + background: ${theme.colors.bg2}; + border-radius: ${theme.border.radius.sm}; + padding: ${theme.spacing.md}; + position: relative; + pointer-events: ${disabled ? 'none' : 'auto'}; + margin-bottom: ${theme.spacing.sm}; + + &::after { + content: ''; + display: ${disabled ? 'block' : 'none'}; + position: absolute; + top: 1px; + left: 1px; + right: 1px; + bottom: 1px; + background: linear-gradient(180deg, rgba(75, 79, 84, 0.5) 0%, rgba(82, 84, 92, 0.5) 100%); + width: calc(100% - 2px); + height: calc(100% - 2px); border-radius: ${theme.border.radius.sm}; - padding: ${theme.spacing.md}; - position: relative; - pointer-events: ${disabled ? 'none' : 'auto'}; - margin-bottom: ${theme.spacing.sm}; + } - &::after { - content: ''; - display: ${disabled ? 'block' : 'none'}; - position: absolute; - top: 1px; - left: 1px; - right: 1px; - bottom: 1px; - background: linear-gradient(180deg, rgba(75, 79, 84, 0.5) 0%, rgba(82, 84, 92, 0.5) 100%); - width: calc(100% - 2px); - height: calc(100% - 2px); - border-radius: ${theme.border.radius.sm}; - } + &:hover { + background: ${disableHover ? theme.colors.bg2 : styleMixins.hoverColor(theme.colors.bg2, theme)}; + cursor: ${disableHover ? 'default' : 'pointer'}; + } - &:hover { - background: ${disableHover ? theme.colors.bg2 : styleMixins.hoverColor(theme.colors.bg2, theme)}; - cursor: ${disableHover ? 'default' : 'pointer'}; - } + &:focus { + ${styleMixins.focusCss(theme)}; + } + `; +}); - &:focus { - ${styleMixins.focusCss(theme)}; - } - `, +/** + * @public + */ +export const getCardStyles = stylesFactory((theme: GrafanaTheme) => { + return { inner: css` width: 100%; `, @@ -227,6 +219,8 @@ export const getCardStyles = stylesFactory((theme: GrafanaTheme, disabled = fals display: flex; align-items: center; color: ${theme.colors.textSemiWeak}; + // align to the right + margin-left: auto; & > * { margin-right: ${theme.spacing.sm} !important; } @@ -243,3 +237,73 @@ export const getCardStyles = stylesFactory((theme: GrafanaTheme, disabled = fals `, }; }); + +interface ChildProps { + styles?: ReturnType; + disabled?: boolean; +} + +const Tags: FC = ({ children, styles }) => { + return
{children}
; +}; +Tags.displayName = 'Tags'; + +const Figure: FC = ({ children, styles }) => { + return
{children}
; +}; + +Figure.displayName = 'Figure'; + +const Meta: FC = memo(({ children, styles }) => { + let meta = children; + + // Join meta data elements by '|' + if (Array.isArray(children)) { + meta = React.Children.toArray(children).reduce((prev, curr, i) => [ + prev, + + | + , + curr, + ]); + } + return
{meta}
; +}); + +Meta.displayName = 'Meta'; + +interface ActionsProps extends ChildProps { + children: JSX.Element[]; + variant?: 'primary' | 'secondary'; +} + +const BaseActions: FC = ({ children, styles, disabled, variant }) => { + const css = variant === 'primary' ? styles?.actions : styles?.secondaryActions; + return
{React.Children.map(children, child => cloneElement(child, { disabled }))}
; +}; + +const Actions: FC = ({ children, styles, disabled }) => { + return ( + + {children} + + ); +}; + +Actions.displayName = 'Actions'; + +const SecondaryActions: FC = ({ children, styles, disabled }) => { + return ( + + {children} + + ); +}; + +SecondaryActions.displayName = 'SecondaryActions'; + +Card.Tags = Tags; +Card.Figure = Figure; +Card.Meta = Meta; +Card.Actions = Actions; +Card.SecondaryActions = SecondaryActions; diff --git a/public/app/features/alerting/AlertRuleItem.test.tsx b/public/app/features/alerting/AlertRuleItem.test.tsx index 680cd69dc31..e619e713334 100644 --- a/public/app/features/alerting/AlertRuleItem.test.tsx +++ b/public/app/features/alerting/AlertRuleItem.test.tsx @@ -1,11 +1,7 @@ import React from 'react'; -import { shallow } from 'enzyme'; +import { render, screen, fireEvent } from '@testing-library/react'; import AlertRuleItem, { Props } from './AlertRuleItem'; -jest.mock('react-redux', () => ({ - connect: () => (params: any) => params, -})); - const setup = (propOverrides?: object) => { const props: Props = { rule: { @@ -26,13 +22,20 @@ const setup = (propOverrides?: object) => { Object.assign(props, propOverrides); - return shallow(); + return render(); }; -describe('Render', () => { +describe('AlertRuleItem', () => { it('should render component', () => { - const wrapper = setup(); + const mockToggle = jest.fn(); + setup({ onTogglePause: mockToggle }); - expect(wrapper).toMatchSnapshot(); + expect(screen.getByText('Some rule')).toBeInTheDocument(); + expect(screen.getByText('state text')).toBeInTheDocument(); + expect(screen.getByText('Pause')).toBeInTheDocument(); + expect(screen.getByText('Edit alert')).toBeInTheDocument(); + + fireEvent.click(screen.getByText('Pause')); + expect(mockToggle).toHaveBeenCalled(); }); }); diff --git a/public/app/features/alerting/AlertRuleItem.tsx b/public/app/features/alerting/AlertRuleItem.tsx index ad3c4482234..466b151e0e9 100644 --- a/public/app/features/alerting/AlertRuleItem.tsx +++ b/public/app/features/alerting/AlertRuleItem.tsx @@ -31,21 +31,20 @@ const AlertRuleItem = ({ rule, search, onTogglePause }: Props) => { width: 100%; `} > - {renderText(rule.name)}} - image={ + {renderText(rule.name)}}> + - } - metadata={[ + + {renderText(rule.stateText)}{' '} for {rule.stateAge} - , - rule.info ? renderText(rule.info) : null, - ]} - actions={[ + + {rule.info ? renderText(rule.info) : null} + + , + Edit alert - , - ]} - /> + + + ); }; diff --git a/public/app/features/alerting/__snapshots__/AlertRuleItem.test.tsx.snap b/public/app/features/alerting/__snapshots__/AlertRuleItem.test.tsx.snap deleted file mode 100644 index 2e77c00710a..00000000000 --- a/public/app/features/alerting/__snapshots__/AlertRuleItem.test.tsx.snap +++ /dev/null @@ -1,73 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Render should render component 1`] = ` -
  • - - Pause - , - - Edit alert - , - ] - } - heading={ - - - - } - image={ - - } - metadata={ - Array [ - - - - - - for - age - , - null, - ] - } - /> -
  • -`;