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

LG-4620: ChartCard and Header components #2550

Merged
merged 30 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e690783
Add BaseHeader
tsck Nov 8, 2024
3758bbd
Change package name to BaseHeader
tsck Nov 8, 2024
ead54a1
Fix BaseHeader names
tsck Nov 8, 2024
9887996
Add BaseHeader tests
tsck Nov 8, 2024
69750a4
Add Header component
tsck Nov 8, 2024
b11645e
Add Header tests
tsck Nov 8, 2024
2e4ca63
Add collapsible state
tsck Nov 8, 2024
4ebe6c5
Updating
tsck Nov 14, 2024
3427bc1
Working state
tsck Nov 14, 2024
ff8d20a
Remove all inputs for slot prop
tsck Nov 18, 2024
215c193
WIP
tsck Nov 18, 2024
8a977d7
Revert "WIP"
tsck Nov 19, 2024
eac05d0
Add context around animation choice
tsck Nov 19, 2024
91efd2a
Bring props more inline w/ ExpandableCard
tsck Nov 19, 2024
7df0a8a
Fix BaseHeader test
tsck Nov 19, 2024
01715bc
Remove base dep from Header
tsck Nov 19, 2024
fb116ec
Remove base dep from ChartCard
tsck Nov 19, 2024
2a4b3cc
Remove BaseHeader completely
tsck Nov 19, 2024
46525bd
Accessibility and tests
tsck Nov 20, 2024
44ff882
Update READMEs
tsck Nov 20, 2024
82d60a6
Changeset
tsck Nov 20, 2024
2715b66
Remove card removal option from storybook
tsck Nov 20, 2024
34d098f
Remove forgotten default
tsck Nov 20, 2024
9aa3101
Correct deps
tsck Nov 20, 2024
ce8968a
Correct deps take 2
tsck Nov 20, 2024
51fc083
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4620/…
tsck Nov 21, 2024
e0b9ce1
CR changes
tsck Nov 22, 2024
a61d79d
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4620/…
tsck Nov 22, 2024
fd0bb1f
Utilize cx
tsck Nov 22, 2024
78c8b34
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4620/…
tsck Nov 22, 2024
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
5 changes: 5 additions & 0 deletions .changeset/early-parents-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lg-charts/core': minor
---

Adds both `Header` and `ChartCard` components.
106 changes: 79 additions & 27 deletions charts/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ yarn add @lg-charts/core
npm install @lg-charts/core
```

## Basic Example
## Basic Chart Example

```js
import { Chart, Line, Grid, XAxis, YAxis } from '@lg-charts/core';

<Chart>
<Header title="My Chart" />
<Grid vertical={false}>
<XAxis type="time" />
<YAxis type="value" formatter={(value) => `${value}GB`} />
Expand All @@ -50,6 +51,31 @@ import { Chart, Line, Grid, XAxis, YAxis } from '@lg-charts/core';
</Chart>;
```

## Grouped Charts

```js
import { ChartCard, Chart, Line, XAxis, YAxis } from '@lg-charts/core';

<ChartCard title="My Group of Charts">
<Chart>
<XAxis type="time" />
<YAxis type="value" formatter={(value) => `${value}GB`} />
<Line
name="Series 1"
data={seriesData}
/>
</Chart>
<Chart>
<XAxis type="time" />
<YAxis type="value" formatter={(value) => `${value}GB`} />
<Line
name="Series 1"
data={seriesData}
/>
</Chart>
<ChartCard>;
```

## Parent Components

### `Chart`
Expand All @@ -58,66 +84,92 @@ Chart container component.

#### Props

| Name | Description | Type | Default |
| -------------- | ------------------------------------------------------- | ---------- | ------- |
| `onChartReady` | Callback to be called when chart is finished rendering. | () => void | |
| Name | Description | Type | Default |
| -------------- | ------------------------------------------------------- | ------------ | ------- |
| `onChartReady` | Callback to be called when chart is finished rendering. | `() => void` | |
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some drive-by style changes in the README, mostly to use backticks on some code that wasn't previously formatted


## Child Components

### `ChartCard`

Expandable card component for visually wrapping multiple charts.

#### Props

| Name | Description | Type | Default |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | ------- |
| `title` | The title to display in the chart header. | `string` | |
| `defaultOpen` _(optional)_ | Defines the default state of the card. | `boolean` | `true` |
| `isOpen` _(optional)_ | Forces the card state. | `boolean` | |
| `onToggleButtonClick` _(optional)_ | Callback fired when a user clicks the open/close toggle button. | `(event: MouseEventHandler<HTMLButtonElement>) => void` | |
| `headerContent` _(optional)_ | Content that will be rendered to the right of the title. Useful for adding components such as `IconButton`'s that control functionality in the chart. | `React.ReactNode` | |

### `Line`

Component that takes in data points and renders a single line on the chart.

#### Props

| Name | Description | Type | Default |
| ------ | ------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------- | ------- |
| `name` | Unique name used to identify the series. **Important note:** If two lines have the same name, only one will be rendered. | string | |
| `data` | Data array of tuples that represent x and y coordinates in the series. | Array<[string \| number \| Date, string \| number \| Date]> | |
| Name | Description | Type | Default |
| ------ | ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------- | ------- |
| `name` | Unique name used to identify the series. **Important note:** If two lines have the same name, only one will be rendered. | `string` | |
| `data` | Data array of tuples that represent x and y coordinates in the series. | `Array<[string \| number \| Date, string \| number \| Date]>` | |

### `Header`

Component for rendering a header in a chart.

#### Props

| Name | Description | Type | Default |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | ------- |
| `title` | The title to display in the chart header. | `string` | |
| `showDivider` _(optional)_ | When true, renders a dividing line on top of header. Useful when stacking charts, such as in a `ChartGroup`. | `boolean` | |
| `headerContent` _(optional)_ | Content that will be rendered to the right of the title. Useful for adding components such as `IconButton`'s that control functionality in the chart. | `React.ReactNode` | |
shaneeza marked this conversation as resolved.
Show resolved Hide resolved

### `Grid`

Component that displays grid lines on the chart.

#### Props

| Name | Description | Type | Default |
| ------------ | --------------------------- | ------- | ------- |
| `horizontal` | Show horizontal grid lines. | boolean | true |
| `vertical` | Show vertical grid lines. | boolean | true |
| Name | Description | Type | Default |
| ------------------------- | --------------------------- | --------- | ------- |
| `horizontal` _(optional)_ | Show horizontal grid lines. | `boolean` | `true` |
| `vertical` _(optional)_ | Show vertical grid lines. | `boolean` | `true` |

### `XAxis`

Renders an x-axis.

#### Props

| Name | Description | Type | Default |
| ------------------------ | --------------------------------------------- | ---------------------------------------- | ------- |
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | |
| `label` _(optional)_ | Label name to be rendered on the axis. | string | |
| `formatter` _(optional)_ | Callback function for formatting tick values. | (value: string, index: number) => string | |
| Name | Description | Type | Default |
| ------------------------ | --------------------------------------------- | ------------------------------------------ | ------- |
| `type` | Type of axis. | `'category' \| 'value' \| 'time' \| 'log'` | |
| `label` _(optional)_ | Label name to be rendered on the axis. | `string` | |
| `formatter` _(optional)_ | Callback function for formatting tick values. | `(value: string, index: number) => string` | |

### `YAxis`

Renders a y-axis.

#### Props

| Name | Description | Type | Default |
| ------------------------ | --------------------------------------------- | ---------------------------------------- | ------- |
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | |
| `label` _(optional)_ | Label name to be rendered on the axis. | string | |
| `formatter` _(optional)_ | Callback function for formatting tick values. | (value: string, index: number) => string | |
| Name | Description | Type | Default |
| ------------------------ | --------------------------------------------- | ------------------------------------------ | ------- |
| `type` | Type of axis. | `'category' \| 'value' \| 'time' \| 'log'` | |
| `label` _(optional)_ | Label name to be rendered on the axis. | `string` | |
| `formatter` _(optional)_ | Callback function for formatting tick values. | `(value: string, index: number) => string` | |

### `Tooltip`

Renders a tooltip onto the chart.

#### Props

| Name | Description | Type | Default |
| ----------------------------- | --------------------------------------------------- | ----------------------------------- | ------- |
| `sortDirection` _(optional)_ | What direction to sort tooltip values in. | 'asc' \| 'desc' | 'desc' |
| `sortKey` _(optional)_ | Whether to sort by name or value. | 'name' \| 'value' | 'value' |
| `valueFormatter` _(optional)_ | Callback function for formatting each value string. | (value: number \| string) => string | |
| Name | Description | Type | Default |
| ----------------------------- | --------------------------------------------------- | ------------------------------------- | --------- |
| `sortDirection` _(optional)_ | What direction to sort tooltip values in. | `'asc' \| 'desc'` | `'desc'` |
| `sortKey` _(optional)_ | Whether to sort by name or value. | `'name' \| 'value'` | `'value'` |
| `valueFormatter` _(optional)_ | Callback function for formatting each value string. | `(value: number \| string) => string` | |
5 changes: 5 additions & 0 deletions charts/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
"access": "public"
},
"dependencies": {
"@leafygreen-ui/combobox": "^9.1.5",
"@leafygreen-ui/emotion": "^4.0.7",
"@leafygreen-ui/icon": "^12.7.0",
"@leafygreen-ui/icon-button": "^15.0.23",
"@leafygreen-ui/info-sprinkle": "^2.1.0",
"@leafygreen-ui/lib": "13.7.0",
"@leafygreen-ui/tokens": "^2.11.0",
"@leafygreen-ui/typography": "^19.3.0",
"@lg-tools/storybook-utils": "^0.1.1",
"echarts": "^5.5.1",
"lodash.debounce": "^4.0.8"
Expand Down
24 changes: 9 additions & 15 deletions charts/core/src/Chart/Chart.styles.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { css } from '@leafygreen-ui/emotion';
import { Theme } from '@leafygreen-ui/lib';
import {
borderRadius,
color,
InteractionState,
Variant,
} from '@leafygreen-ui/tokens';

export const chartStyles = css`
grid-row: 2;
export const chartContainerStyles = css`
display: grid;
grid-template-areas:
'chartHeader'
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
'chart';
width: 100%;
`;

export const getWrapperStyles = (theme: Theme) => css`
export const chartStyles = css`
display: block;
grid-area: chart;
height: 280px;
width: 100%;
border: 1px solid
${color[theme].border[Variant.Disabled][InteractionState.Default]};
border-radius: ${borderRadius[200]}px;
display: grid;
grid-template-rows: auto 1fr;
`;
17 changes: 13 additions & 4 deletions charts/core/src/Chart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import LeafyGreenProvider, {

import { ChartProvider } from '../ChartContext';

import { chartStyles, getWrapperStyles } from './Chart.styles';
import { chartContainerStyles, chartStyles } from './Chart.styles';
import { ChartProps } from './Chart.types';
import { useChart } from './hooks';

Expand Down Expand Up @@ -45,12 +45,21 @@ export function Chart({
addChartSeries={addChartSeries}
removeChartSeries={removeChartSeries}
>
<div className={cx(getWrapperStyles(theme), className)} {...rest}>
{children}
<div className={cx(chartContainerStyles, className)}>
<div>
{/**
* Children other than Header are not expected to be rendered to the DOM,
* but are used to provide a more declarative API for adding functionality
* to the chart canvas. They have access to the ChartContext and can be
* used to add components like Line, Grid, etc.
*/}
{children}
</div>
<div
ref={chartRef}
className={`echart ${chartStyles}`}
className={cx('echart', chartStyles)}
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
data-testid="echart"
{...rest}
/>
</div>
</ChartProvider>
Expand Down
61 changes: 61 additions & 0 deletions charts/core/src/ChartCard/ChartCard.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { axe } from 'jest-axe';

import { ChartCard } from './ChartCard';
import { ChartCardProps } from './ChartCard.types';

const defaultContentText = 'shenanigans';

const defaultProps = {
title: 'test',
headerContent: <div>{defaultContentText}</div>,
children: 'Chartchartchartchartchart',
};

const renderChartCard = (props?: Partial<ChartCardProps>) =>
render(<ChartCard {...defaultProps} {...props} />);

const clickToggleButton = () => {
userEvent.click(screen.getByLabelText('Toggle button'));
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
};

describe('@lg-charts/core/src/ChartCard/ChartCard', () => {
test('does not have basic accessibility issues', async () => {
const { container } = renderChartCard();
const results = await axe(container);
expect(results).toHaveNoViolations();
});

test('should display title value', () => {
renderChartCard();
expect(screen.getByText(defaultProps.title)).toBeInTheDocument();
});

test('render component passed to headerContent', () => {
renderChartCard();
expect(screen.getByText(defaultContentText)).toBeInTheDocument();
});

test('should call onToggleButtonClick when button is clicked', () => {
const onToggleButtonClick = jest.fn();
renderChartCard({ onToggleButtonClick });
clickToggleButton();
expect(onToggleButtonClick).toHaveBeenCalledTimes(1);
});

test('content is visible by default', () => {
renderChartCard();
expect(screen.getByText(defaultProps.children)).toBeVisible();
});

test('content is not visible to accessible devices after click', () => {
renderChartCard();
clickToggleButton();
expect(screen.getByTestId('chart-card-children')).toHaveAttribute(
'aria-hidden',
'true',
);
});
});
63 changes: 63 additions & 0 deletions charts/core/src/ChartCard/ChartCard.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { css } from '@leafygreen-ui/emotion';
import { Theme } from '@leafygreen-ui/lib';
import {
borderRadius,
color,
InteractionState,
spacing,
transitionDuration,
Variant,
} from '@leafygreen-ui/tokens';

export const getContainerStyles = (
theme: Theme,
height?: number,
headerHeight?: number,
) => css`
border: 1px solid
${color[theme].border[Variant.Disabled][InteractionState.Default]};
border-radius: ${borderRadius[200]}px;
overflow: hidden;
width: 100%;

/**
* Height adjustments are being made on the entire container instead of just
* a wrapper around the children because a bottom border is set on the container and header.
* By shrinking the container height we prevent a conflict between the container border
* and the header border when collapsed. By making the container height 1px shorter on collapse
* we just cutoff the header border entirely.
*/
/* Accounts for 1px border */
max-height: ${headerHeight ? headerHeight + 1 + 'px' : 'auto'};
transition: max-height ${transitionDuration.slower}ms ease-in-out;
&.open {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: why do you prefer using .open instead of conditionally rendering the styles with cx?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually not a preference, just not used to having cx as an option! Will update this.

max-height: ${height ? height + 'px' : 'auto'};
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
}
`;

export const getHeaderStyles = (theme: Theme) => css`
width: 100%;
padding: ${spacing[150]}px ${spacing[300]}px;
display: grid;
grid-template-columns: auto 1fr;
border-bottom: 1px solid
${color[theme].border[Variant.Disabled][InteractionState.Default]};
`;

export const toggleButtonStyles = css`
margin-right: ${spacing[100]}px;
`;

export const toggleIconStyles = css`
transform: rotate(-90deg);
transition: transform ${transitionDuration.slower}ms ease-in-out;

&.open {
transform: rotate(0deg);
}
`;

export const leftInnerContainerStyles = css`
display: flex;
align-items: center;
`;
Loading
Loading