1- var React = require ( 'react' ) ;
2- var createReactClass = require ( 'create-react-class' ) ;
3- var PropTypes = require ( 'prop-types' ) ;
4- var win = typeof global === 'undefined' ? window : global ;
1+ import React , { Component } from 'react' ;
52
6- module . exports = function ( chartType , Highcharts ) {
7- var displayName = 'Highcharts' + chartType ;
8- var result = createReactClass ( {
9- displayName : displayName ,
3+ const win = typeof global === 'undefined' ? window : global ;
104
11- propTypes : {
12- config : PropTypes . object ,
13- isPureConfig : PropTypes . bool ,
14- neverReflow : PropTypes . bool ,
15- callback : PropTypes . func ,
16- domProps : PropTypes . object
17- } ,
18- defaultProps : {
19- callback : ( ) => { } ,
20- domProps : { }
21- } ,
22- setChartRef : function ( chartRef ) {
23- this . chartRef = chartRef ;
24- } ,
25- renderChart : function ( config ) {
26- if ( ! config ) {
27- throw new Error ( 'Config must be specified for the ' + displayName + ' component' ) ;
28- }
29- let chartConfig = config . chart ;
30- this . chart = new Highcharts [ chartType ] ( {
31- ...config ,
32- chart : {
33- ...chartConfig ,
34- renderTo : this . chartRef
5+ function chartsFactory ( chartType , Highcharts ) {
6+
7+ class Chart extends Component {
8+ constructor ( ) {
9+ super ( )
10+ this . chartType = chartType ;
11+ this . Highcharts = Highcharts ;
12+ this . displayName = 'Highcharts' + chartType ;
13+ }
14+
15+ setChartRef ( chartRef ) {
16+ this . chartRef = chartRef ;
3517 }
36- } , this . props . callback ) ;
3718
38- if ( ! this . props . neverReflow ) {
39- win && win . requestAnimationFrame && requestAnimationFrame ( ( ) => {
40- this . chart && this . chart . options && this . chart . reflow ( ) ;
41- } ) ;
42- }
43- } ,
19+ renderChart ( config ) {
20+ if ( ! config ) {
21+ throw new Error ( 'Config must be specified for the ' + this . displayName + ' component' ) ;
22+ }
23+ const chartConfig = config . chart ;
24+ this . chart = new this . Highcharts [ this . chartType ] ( {
25+ ...config ,
26+ chart : {
27+ ...chartConfig ,
28+ renderTo : this . chartRef
29+ }
30+ } , this . props . callback ) ;
4431
45- shouldComponentUpdate ( nextProps ) {
46- if ( nextProps . neverReflow || ( nextProps . isPureConfig && this . props . config === nextProps . config ) ) {
47- return true ;
48- }
49- this . renderChart ( nextProps . config ) ;
50- return false ;
51- } ,
32+ if ( ! this . props . neverReflow ) {
33+ win && win . requestAnimationFrame && requestAnimationFrame ( ( ) => {
34+ this . chart && this . chart . options && this . chart . reflow ( ) ;
35+ } ) ;
36+ }
37+ }
5238
53- getChart : function ( ) {
54- if ( ! this . chart ) {
55- throw new Error ( 'getChart() should not be called before the component is mounted' ) ;
56- }
57- return this . chart ;
58- } ,
39+ shouldComponentUpdate ( nextProps ) {
40+ if ( nextProps . neverReflow || ( nextProps . isPureConfig && this . props . config === nextProps . config ) ) {
41+ return true ;
42+ }
43+ this . renderChart ( nextProps . config ) ;
44+ return false ;
45+ }
5946
60- componentDidMount : function ( ) {
61- this . renderChart ( this . props . config ) ;
62- } ,
47+ getChart ( ) {
48+ if ( ! this . chart ) {
49+ throw new Error ( 'getChart() should not be called before the component is mounted' ) ;
50+ }
51+ return this . chart ;
52+ }
6353
64- componentWillUnmount ( ) {
65- this . chart . destroy ( ) ;
66- } ,
54+ componentDidMount ( ) {
55+ this . renderChart ( this . props . config ) ;
56+ }
6757
68- render : function ( ) {
69- return < div ref = { this . setChartRef } { ...this . props . domProps } /> ;
58+ componentWillUnmount ( ) {
59+ this . chart . destroy ( ) ;
60+ }
61+
62+ render ( ) {
63+ return < div ref = { this . setChartRef . bind ( this ) } { ...this . props . domProps } /> ;
64+ }
65+ }
66+
67+ if ( isProdMode ) {
68+ let PropTypes = require ( 'prop-types' )
69+
70+ Chart . propTypes = {
71+ config : PropTypes . object ,
72+ isPureConfig : PropTypes . bool ,
73+ neverReflow : PropTypes . bool ,
74+ callback : PropTypes . func ,
75+ domProps : PropTypes . object
76+ }
77+ }
78+ Chart . defaultProps = {
79+ callback : ( ) => {
80+ } ,
81+ domProps : { }
7082 }
71- } ) ;
83+ let result = Chart ;
84+ result . Highcharts = Highcharts ;
85+ result . withHighcharts = Highcharts => module . exports ( chartType , Highcharts ) ;
7286
73- result . Highcharts = Highcharts ;
74- result . withHighcharts = ( Highcharts ) => {
75- return module . exports ( chartType , Highcharts ) ;
76- } ;
77- return result ;
78- } ;
87+ return result
88+ }
7989
90+ export default chartsFactory ;
0 commit comments