Search code examples
svgreactjstypescriptjsxtsx

react-stockchart: cannot convert JSX to TSX


I have an example of the react-stockchart chart in JSX:
JSX plunker: http://plnkr.co/edit/gist:b993d5fcc5c09dd66a6e?p=preview

I want to add JSX to an existing TypeScript project, so I changed the file extension from JSX to TSX and did other convertions from this site: http://slidedeck.io/thewazir/Using-Typescript-with-JSX

But the problem still remains, this code doesn't compile:

CandleStickChartWithBollingerBandOverlay = fitWidth(CandleStickChartWithBollingerBandOverlay);
ReactDOM.render(<CandleStickChartWithBollingerBandOverlay data={data} type="hybrid"/>, document.getElementById("chart")); 

fitWidth source: https://github.com/rrag/react-stockcharts/blob/master/src/lib/helper/fitWidth.jsx
If I remove fitWidth it is drawn with incorrect width:

ReactDOM.render(<CandleStickChartWithBollingerBandOverlay data={data} type="hybrid" width={800}/>, document.getElementById("chart"));

I tried this code, it doesn't work (nothing is drawn at all):

var StockChartComponent = fitWidth(CandleStickChartWithBollingerBandOverlay);
ReactDOM.render(<StockChartComponent data={data} type="hybrid"/>, document.getElementById("chart"));

And this doesn't work too:

var StockChartComponent = fitWidth(new CandleStickChartWithBollingerBandOverlay());
ReactDOM.render(<StockChartComponent data={data} type="hybrid"/>, document.getElementById("chart"));

Solution

  • This is the main tsx file:

    export function initialize(data, element) {
        var StockChartComponent = fitWidth(CandleStickChartWithBollingerBandOverlay);    
        ReactDOM.render(<StockChartComponent data={data} type="hybrid" height={800} />, element);       
    }
    

    This is the fixed fitWidth.tsx:

    import React = require('react');
    import ReactDOM = require('react-dom');
    
    export function fitWidth(WrappedComponent, withRef = true) {
    class ResponsiveComponent extends React.Component<any, any> {
        static getDisplayName(Series) {
            var name = Series.displayName || Series.name || "Series";
            return name;
        }
    
        static defaultProps = {
            displayName: `fitWidth(${ResponsiveComponent.getDisplayName(WrappedComponent)})`
        }
        constructor(props) {
            super(props);
            this.handleWindowResize = this.handleWindowResize.bind(this);
            this.getWrappedInstance = this.getWrappedInstance.bind(this);
        }
        componentDidMount() {
            window.addEventListener("resize", this.handleWindowResize);
            var el = ReactDOM.findDOMNode(this);
            var w = (el.parentNode as Element).clientWidth;
    
            /* eslint-disable react/no-did-mount-set-state */
            this.setState({
                width: w
            });
            /* eslint-enable react/no-did-mount-set-state */
        }
        componentWillUnmount() {
            window.removeEventListener("resize", this.handleWindowResize);
        }
        handleWindowResize() {
            var el = ReactDOM.findDOMNode(this);
            var w = (el.parentNode as Element).clientWidth;
            this.setState({
                width: w
            });
        }
        getWrappedInstance() {
            return (this.refs as any).component;
        }
        render() {
            var ref = withRef ? { ref: "component" } : {};
    
            if (this.state && this.state.width) {
                return <WrappedComponent width={this.state.width} {...this.props} {...ref} />;
            } else {
                return <div />;
            }
        }
    }
    
    return ResponsiveComponent;
    

    }