Skip to content

Commit

Permalink
feat(package): extends root div interface for styling
Browse files Browse the repository at this point in the history
Able to specify 'style' and 'class' attribute

Fix #3162
Close #3163
  • Loading branch information
BillionaireDY authored and netil committed Apr 7, 2023
1 parent 21eea52 commit 0a1fe08
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ Frank Tackitt <franklyn@tackitt.net>
jongwooo <jongwooo.han@gmail.com>
J酶rn Andre Sundt <jorn.andre.sundt@jasmin.no>
Erik Hopp <me@erikhopp.com>
Do Yoon Kim <doyoonkim1002@gmail.com>
39 changes: 32 additions & 7 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ $ npm install billboard.js @billboard.js/react

## How to use

> 鈩癸笍 The component will create a `<div>` element and append it to the parent element and [`bindto`](https://naver.github.io/billboard.js/release/latest/doc/Options.html#.bindto) option is not supported in this case.
### Basic Usage

```jsx
import React, {useEffect, useRef} from "react";

Expand Down Expand Up @@ -45,9 +48,21 @@ function App() {
}
}, []);

return <div style={{width: "500px"}}>
<BillboardJS bb={bb} options={options} ref={chartComponent} />
</div>;
return <BillboardJS
bb={bb}
options={options}
ref={chartComponent}

/* The values will be specified to the bound element as inlined styles */
style={{
width: "500px",
...
}}

/* When class name doesn't contains `bb`,
then you also need to update the default CSS to be rendered correctly. */
className={"bb my-classname"}
/>;
}
```
Expand All @@ -70,6 +85,14 @@ const options = {

<App {...options} />

// or
// <App data={
// columns: [
// ["data1", 300, 350, 300]
// ],
// type: "bar"
// } />

// App.tsx
import * as Chart from "billboard.js";
import "billboard.js/dist/billboard.css"; // default css
Expand All @@ -90,9 +113,11 @@ export function App(props: IChartOptions) {
}
}, []);

return <div style={{width: "500px"}}>
<BillboardJS bb={Chart.bb} options={props} ref={chartComponent} />
</div>;
return <BillboardJS
bb={Chart.bb}
options={props}
ref={chartComponent}
/>;
}
```
Expand All @@ -103,7 +128,7 @@ export function App(props: IChartOptions) {
interface IChartOptions;

// @billboard.js/react props
interface IProp {
interface IProp extends Pick<HTMLProps<HTMLDivElement>, "className" | "style"> {
bb: typeof bb;
options: ChartOptions;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/react/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ function App(props) {
return (
<div className="App" style={{width: "500px"}}>
<BillboardJS bb={bb} options={d} ref={chartComponent} />
<BillboardJS bb={bb} options={d2} />
<BillboardJS bb={bb} options={d2} style={{
width: "800px",
border: "solid 1px red"
}} />

{/* passing given props */}
<BillboardJS bb={bb} options={props} />
<BillboardJS bb={bb} options={props} className={"bb my-class"} />
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@billboard.js/react",
"version": "1.0.1",
"version": "1.1.0",
"description": "React component for billboard.js",
"main": "dist/billboardjs-react.js",
"types": "dist/types/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Copyright (c) 2022 ~ present NAVER Corp.
* @billboard.js/react project is licensed under the MIT license
*/
import React, {forwardRef, useEffect, useImperativeHandle, useRef} from "react";
import React, {forwardRef, HTMLProps, useEffect, useImperativeHandle, useRef} from "react";
import type {bb, Chart, ChartOptions} from "billboard.js";

export {ChartOptions as IChartOptions};

export interface IProp {
export interface IProp extends Pick<HTMLProps<HTMLDivElement>, "className" | "style"> {
bb: typeof bb;
options: ChartOptions;
}
Expand All @@ -19,7 +19,7 @@ export interface IChart {
export default forwardRef<IChart, IProp>((props, ref) => {
const container = useRef<HTMLDivElement>(null);
const instance = useRef<Chart | null>();
const {bb, options: {...options}} = props;
const {bb, options: {...options}, ...htmlDivProps} = props;

// generate chart
const generate = () => {
Expand Down Expand Up @@ -57,5 +57,5 @@ export default forwardRef<IChart, IProp>((props, ref) => {
})
);

return <div ref={container} />;
return <div ref={container} {...htmlDivProps} />;
});
12 changes: 11 additions & 1 deletion packages/react/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ describe("basic", async () => {

const legendText = await page.locator(".bb .bb-legend-item-data4 text");

await expect(legendText).toHaveText("data4");
expect(legendText).toHaveText("data4");

// check if 2nd chart has 'style' attribute
const style = await page.evaluate(() => document.querySelector(".bb:nth-child(2)")?.getAttribute("style"));

expect(style).not.toBeNull();

// check if 3rd chart has 'class' attribute
const className = await page.evaluate(() => document.querySelector(".bb:nth-child(3)")?.className);

expect(className).not.toBeNull();
});

test("should charts lengend be interactable.", async () => {
Expand Down

0 comments on commit 0a1fe08

Please sign in to comment.