0

I have a simple radio button group component on codepen here that is not rendering in codepen. I want to post this to the code review stackexchange, since it is one of the first components i've built and will be necessary in many places on a web app I am building. However for that post, I want my codepen example to be working.

I think I am probably breaking some rule about how to use es6 in react to get the app to render, but I am struggling to debug. My console.logs() are not helping, and the error messages in codepen arent helping a ton either.

Since I linked to my codepen, I have to accompany it with code, so here's what I have in my codepen at the moment:

import React, { Component } from 'react';
import { ToggleButton, ToggleButtonGroup } from 'react-bootstrap';


class ToolButtonGroup extends Component {

    constructor(props) {
        super(props);
    };
  
    render() {

        // Get Variables from the params prop
        const { header, buttons, initialVal } = this.props.params;
        const { borderRadius, margin, padding, fontsize, border } = this.props.params;
        const { gridID, gridColumns, minRowHeight } = this.props.params;

        // Create the individual buttons
        const pageButtons = buttons.map((buttoninfo, idx) => {
            return (
                <ToggleButton 
                    key={idx}
                    style={{
                        "borderRadius": borderRadius, 
                        "margin": margin,
                        "padding": padding,
                        "fontSize": fontsize,
                        "border": border
                    }}
                    bsSize="large"
                    value={buttoninfo.value}>
                {buttoninfo.label}
                </ToggleButton>
            )
        })

        // Return the button group
        return(
            <div 
              style={{"border": "1px solid red" }} 
              id={gridID}>
                <h2 style={{
                  "width": "100%",
                  "margin": "0 auto",
                  "fontSize": "1.75em",
                  "marginTop": "5px",   
                  "border": "none"
                }}
                >{header}</h2>
                <ToggleButtonGroup 
                    type="radio"
                    name="charttype-options" 
                    defaultValue={initialVal} 
                    onChange={this.props.handler}
                    style={{
                        "display": "grid",
                        "gridTemplateColumns": "repeat(" + gridColumns + ", 1fr)",
                        "gridAutoRows": "auto",
                        "gridGap": "8px"
                    }}
                >
                    {pageButtons}
                </ToggleButtonGroup>
            </div>
        )
    }   
}



class StarterApp extends Component {
    constructor(props){
        super(props);
        this.state = {
            pitchersOrHitters: "",
            position: ""
        }
    }

    // Button and Select Handlers!
    handlePitchHitChange = (pitchersOrHitters) => {
        this.setState({pitchersOrHitters})
    }
    handlePositionChange = (position) => {
        this.setState({ position: position });
    }

    render() {

        console.log("A")
        // 0. Load State and Props 
        const { pitchersOrHitters, position } = this.state;


        // Pitcher or Hitter Radio Button Group params
        const pitchOrHitButtonGroup = {
            borderRadius: "25px",
            margin: "1% 10%",
            padding: "5%",
            fontsize: "2em",
            border: "2px solid #BBB",

            gridColumns: 1, minRowHeight: "10px", "gridID": "buttons1",

            header: "Choose One:",          
            buttons: [
                { value: "Pitchers", label: "Pitchers" },
                { value: "Hitters", label: "Hitters" },
            ],
            initialVal: "Pitchers"}

        // Pitcher or Hitter Radio Button Group params
        const positionButtonGroup = {
            borderRadius: "10px",
            margin: "1% 10%",
            padding: "5%",
            fontsize: "1.25em",
            border: "2px solid #BBB",

            gridColumns: 4, minRowHeight: "20px", "gridID": "buttons2",

            header: "Choose One:",          
            buttons: [
                { value: "SP", label: "SP" },
                { value: "RP", label: "RP" },
                { value: "1B", label: "1B" },
                { value: "2B", label: "2B" },
                { value: "SS", label: "SS" },
                { value: "3B", label: "3B" },
                { value: "LF", label: "LF" },
                { value: "RF", label: "RF" },
                { value: "CF", label: "CF" }
            ],
            initialVal: "SP"}

        return(
            <div className="chart-grid-container">
                <ToolButtonGroup
                    params={pitchOrHitButtonGroup}
                    value={pitchersOrHitters}
                    handler={this.handlePitchHitChange} />    

                <ToolButtonGroup
                    params={positionButtonGroup}
                    value={position}
                    handler={this.handlePositionChange} />    
            </div>
        )
    }
}


ReactDOM.render(
  <StarterApp />,
  document.getElementById('root')
);
.chart-grid-container {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    grid-auto-rows: minmax(200px, auto);
    grid-gap: 5px;
    grid-template-areas:
        "btns1 btns1 btns2 btns2 btns2 btns2 . . . . . .";
}

#buttons1 { grid-area: btns1; }		
#buttons2 { grid-area: btns2; }		
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.min.js"></script>
<div id='root'>
  COME ON WORK!
</div>

Im unsurprisingly struggling to get this code snippet working as well. Although in this case, it is because I don't know how to include react-bootstrap, which is something I've already done in my codepen.

Thanks!

1
  • You are importing ToggleButtonGroup and then create a class again, why? Also, do not create functions in your render block. If you need functions do it outside of render. Also, I suggest using codesandbox.io or stackblitz.com to try React components. You can add dependencies easily on this platforms. Commented Apr 14, 2018 at 22:06

1 Answer 1

1

I noticed I got the errors when using import statements on that specific project.

This is probably a limitation of transpiling engine on the codepen. Better if you use some platform (ie: enter link description here) that already has all of these solved out for you.

Here is your code on codesandbox. enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. i dont understand what the codepen limitation is though? i added the required packages (react, react-bootstrap, react-dom) and all. oh well, i'll use this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.