-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(c3Chart): issues/283 add base c3 component
* c3Chart, adds a c3 wrapper component, initial tests
- Loading branch information
Showing
7 changed files
with
429 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/components/c3Chart/__tests__/__snapshots__/c3Chart.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`C3Chart Component should pass child components: child components 1`] = ` | ||
<div | ||
className="curiosity-c3chart null" | ||
style={Object {}} | ||
> | ||
<div | ||
className="curiosity-c3chart-container" | ||
/> | ||
<div | ||
className="curiosity-c3chart-description" | ||
> | ||
lorem | ||
<span> | ||
ipsum | ||
</span> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`C3Chart Component should render a basic component: basic 1`] = ` | ||
<div | ||
className="curiosity-c3chart null" | ||
style={Object {}} | ||
> | ||
<div | ||
className="curiosity-c3chart-container" | ||
/> | ||
<div | ||
className="curiosity-c3chart-description" | ||
/> | ||
</div> | ||
`; | ||
|
||
exports[`C3Chart Component should return a chart and config reference: references 1`] = ` | ||
<div | ||
className="curiosity-c3chart null" | ||
style={Object {}} | ||
> | ||
<div | ||
className="curiosity-c3chart-container" | ||
/> | ||
<div | ||
className="curiosity-c3chart-description" | ||
> | ||
<span> | ||
<Component /> | ||
and | ||
{"data":{"columns":[]}} | ||
</span> | ||
</div> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { mount, shallow } from 'enzyme'; | ||
import { C3Chart } from '../c3Chart'; | ||
|
||
describe('C3Chart Component', () => { | ||
it('should render a basic component', () => { | ||
const props = {}; | ||
const component = shallow(<C3Chart {...props} />); | ||
|
||
expect(component).toMatchSnapshot('basic'); | ||
}); | ||
|
||
it('should run componentWillUnmount method successfully', () => { | ||
const component = mount(<C3Chart />); | ||
const componentWillUnmount = jest.spyOn(component.instance(), 'componentWillUnmount'); | ||
component.unmount(); | ||
expect(componentWillUnmount).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should pass child components', () => { | ||
const props = {}; | ||
const component = shallow( | ||
<C3Chart {...props}> | ||
lorem <span>ipsum</span> | ||
</C3Chart> | ||
); | ||
|
||
expect(component).toMatchSnapshot('child components'); | ||
}); | ||
|
||
it('should return a chart and config reference', () => { | ||
const props = { | ||
config: { | ||
data: { | ||
columns: [] | ||
} | ||
} | ||
}; | ||
const component = shallow( | ||
<C3Chart {...props}> | ||
{({ chart, config }) => ( | ||
<span> | ||
{chart} and {JSON.stringify(config)} | ||
</span> | ||
)} | ||
</C3Chart> | ||
); | ||
|
||
expect(component).toMatchSnapshot('references'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import c3 from 'c3'; | ||
import _isEqual from 'lodash/isEqual'; | ||
import 'c3/c3.min.css'; | ||
import { helpers } from '../../common'; | ||
|
||
/** | ||
* C3 wrapper. | ||
* Uses aspects from https://github.com/bcbcarl/react-c3js and https://github.com/wuct/react-c3-component | ||
* | ||
* @augments React.Component | ||
*/ | ||
class C3Chart extends React.Component { | ||
state = { chart: null }; | ||
|
||
node = React.createRef(); | ||
|
||
componentDidMount() { | ||
this.generateChart(); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { config } = this.props; | ||
|
||
if (!_isEqual(prevProps.config.data, config.data)) { | ||
this.generateChart(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
const { chart } = this.state; | ||
if (chart) { | ||
chart.destroy(); | ||
} | ||
this.setState({ chart: null }); | ||
} | ||
|
||
generateChart() { | ||
const { chart } = this.state; | ||
const { config, onComplete } = this.props; | ||
|
||
let updatedChart = chart; | ||
if (!updatedChart) { | ||
updatedChart = c3.generate({ bindto: this.node.current, ...config }); | ||
} | ||
|
||
updatedChart.load({ | ||
...config.data, | ||
unload: config.unloadBeforeLoad || false, | ||
done: () => { | ||
this.setState({ chart: updatedChart }, () => { | ||
if (config.done) { | ||
config.done({ chart: updatedChart, config }); | ||
} else { | ||
onComplete({ chart: updatedChart, config }); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
const { chart } = this.state; | ||
const { className, children, config, style } = this.props; | ||
|
||
return ( | ||
<div className={`curiosity-c3chart ${className}`} style={style}> | ||
<div ref={this.node} className="curiosity-c3chart-container" /> | ||
{chart && ( | ||
<div className="curiosity-c3chart-description"> | ||
{(typeof children === 'function' && children({ chart, config })) || children} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Prop types. | ||
* | ||
* @type {{children: Node|Function, onComplete: Function, className: string, style: object, config}} | ||
*/ | ||
C3Chart.propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), | ||
className: PropTypes.string, | ||
config: PropTypes.shape({ | ||
unloadBeforeLoad: PropTypes.bool, | ||
data: PropTypes.object, | ||
done: PropTypes.func | ||
}), | ||
onComplete: PropTypes.func, | ||
style: PropTypes.object | ||
}; | ||
|
||
/** | ||
* Default props. | ||
* | ||
* @type {{children: null, onComplete: Function, className: null, style: {}, config: {}}} | ||
*/ | ||
C3Chart.defaultProps = { | ||
children: null, | ||
className: null, | ||
config: {}, | ||
onComplete: helpers.noop, | ||
style: {} | ||
}; | ||
|
||
export { C3Chart as default, C3Chart }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.