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!
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.
Related
Suppose I want to change the standard white background color of the Select component dynamically based on selection. I found a solution
on the internet .ant-select-selection { background-color: green; } but this is static.
It should be like this:
Here is my codesandbox link
Here is a sample code:
const RBICOptions = [
{
background: "#00CC00",
color: "#fff",
title: "Low",
value: "low"
},
{
background: "#FFFF00",
color: "#000",
title: "Medium",
value: "medium"
},
{
background: "#FFC000",
color: "#000",
title: "High",
value: "high"
},
{
background: "#FF0000",
color: "#fff",
title: "Very High",
value: "very_high"
}
];
const App: React.FC = () => {
const [selectedIPSS, setSelectedIPSS] = useState("#fff");
console.log("selectedIPSS", selectedIPSS);
return (
<Select
style={{
width: "50vw",
marginBottom: "5%",
backgroundColor: selectedIPSS
}}
onChange={(_, option) => {
setSelectedIPSS("style" in option ? option.style.background : "#fff");
}}
>
{RBICOptions.map((op) => (
<Select.Option
key={op.value}
value={op.value}
style={{ background: op.background, color: op.color }}
>
{op.title}
</Select.Option>
))}
</Select>
);
};
Please help me out with this issue. Thanks in advance.
Its because the ant-select class already defines a white background. You can remove it with setting the style to inherit:
.ant-select:not(.ant-select-customize-input) .ant-select-selector {
background-color: inherit;
}
Here is a codesandbox: https://codesandbox.io/s/select-with-search-field-antd-4-24-1-forked-rzln9z?file=/index.css:0-99
I want to color the background of my divs thru calling props on my components. I have a colors file in js from where I recall values, as it works like an object. I can recall the name of the color, but the background color does not display like I want it to.
Here are some files and code of my project.
export default [
{
paletteName: "Material UI Colors",
id: "material-ui-colors",
emoji: "🎨",
colors: [
{ name: "red", color: "#F44336" },
{ name: "pink", color: "#E91E63" },
{ name: "purple", color: "#9C27B0" },
{ name: "deeppurple", color: "#673AB7" },
{ name: "indigo", color: "#3F51B5" },
{ name: "blue", color: "#2196F3" },
{ name: "lightblue", color: "#03A9F4" },
{ name: "cyan", color: "#00BCD4" },
{ name: "teal", color: "#009688" },
{ name: "green", color: "#4CAF50" },
{ name: "lightgreen", color: "#8BC34A" },
{ name: "lime", color: "#CDDC39" },
{ name: "yellow", color: "#FFEB3B" },
{ name: "amber", color: "#FFC107" },
{ name: "orange", color: "#FF9800" },
{ name: "deeporange", color: "#FF5722" },
{ name: "brown", color: "#795548" },
{ name: "grey", color: "#9E9E9E" },
{ name: "bluegrey", color: "#607D8B" }
]
}```
My colorBox component
import React, { Component } from 'react';
import "./ColorBox.css";
class ColorBox extends Component {
render() {
const { name, background } = this.props;
return(
<div stlye={{ background }} className='ColorBox'>
<div className="copy-container">
<div className="box-content">
<span>{name}</span>
</div>
<button className="copy-button">Copy
</button>
<span className="see-more">More</span>
</div>
</div>
)
}
}
export default ColorBox; ```
Here is my Palette Component that renders colorBox component
import React, { Component } from 'react';
import ColorBox from "./ColorBox";
import "./Palette.css";
class Palette extends Component {
render() {
const colorBoxes = this.props.colors.map(color => (
<ColorBox background={color.color} name={color.name} />
));
return (
<div className="Palette">
<div className="Panel-colors">{colorBoxes}
</div>
</div>
);
}
}
export default Palette;
And here is App.js rendering Palette and selecting thru spreadOperator the values on my seed component
import React, { Component } from 'react';
import Palette from "./Palette";
import seedColors from "./seedColors";
class App extends Component {
render() {
return (
<div>
<Palette {...seedColors[0]} />
</div>
);
}
}
export default App;
Here is a Screenshot of how it runs the react app, it easily extrapolates the names of the colors thru props, but doesn't show the background color, any tips? Thank you.
I am using react-autosuggest in my Material-UI component to get suggestions when user types. And just not able to style the input field and the suggestions text.
Probably I am missing something basic here, and any guidance will be immensly helpful. The official dox of react-autosuggest is here for using the theme technique that uses react-themeable. But I could not implement that in my Material-UI component.
The below is my code that I am trying with.
import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { makeStyles } from '#material-ui/core/styles'
import Autosuggest from 'react-autosuggest';
import { defaultTheme } from 'react-autosuggest/dist/theme';
const useStyles = makeStyles(theme => ({
container: {
margin: 'auto',
backgroundColor: theme.background.default,
},
innerTableContainer: {
height: 'calc(100vh - 190px)',
borderRadius: theme.shape.borderRadius,
backgroundColor: theme.background.paper,
},
react_autosuggest__container: {
"position": "relative",
"width": "440px",
},
react_autosuggest__input: {
"width": "240px",
"height": "30px",
"padding": "10px 20px",
"fontFamily": "Helvetica, sans-serif",
"fontWeight": "300",
"fontSize": "16px",
"border": "1px solid #aaa",
"borderRadius": "4px"
},
react_autosuggest__input__focused: {
"outline": "none"
},
react_autosuggest__input__open: {
"borderBottomLeftRadius": "0",
"borderBottomRightRadius": "0"
},
react_autosuggest__suggestions_container__open: {
"display": "block",
"position": "absolute",
"top": "51px",
"width": "280px",
"border": "1px solid #aaa",
"backgroundColor": "#fff",
"fontFamily": "Helvetica, sans-serif",
"fontWeight": "300",
"fontSize": "16px",
"borderBottomLeftRadius": "4px",
"borderBottomRightRadius": "4px",
"zIndex": "2"
},
react_autosuggest__suggestions_list: {
"margin": "0",
"padding": "0",
"listStyleType": "none"
},
react_autosuggest__suggestion: {
"cursor": "pointer",
"padding": "10px 20px"
},
react_autosuggest__suggestion__highlighted: {
"backgroundColor": "#ddd"
}
}))
const GithubMostPopularList = () => {
const classes = useStyles()
const [value, setValue] = useState('')
const [suggestions, setSuggestions] = useState([])
const onChange = (event, { newValue, method }) => {
setValue(newValue)
};
const onSuggestionsFetchRequested = ({ value }) => {
setSuggestions(getSuggestions(value))
};
const onSuggestionsClearRequested = () => {
setSuggestions([])
};
const inputProps = {
placeholder: "Start typing your city name",
value,
onChange: onChange,
};
return (
<div className={classes.container}>
<div className={classes.react_autosuggest__container} >
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={onSuggestionsFetchRequested}
onSuggestionsClearRequested={onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
/>
</div>
)}
</div>
)
}
export default GithubMostPopularList
I have also tried this solution given in one of Github issue
<Autosuggest
//misc extra props I've cut out for brevity
theme={{
...defaultTheme,
...{
container: {
...defaultTheme.container,
display: 'visible',
width: '340px',
},
//more overrides
}
}}
/>
But in this case the component is not compiling at all.
Answering my own question.
I was able to solve it as below, the useStyles = makeStyles() portion remains the same and the below is how to change the defulat theme of react-autosuggest.
import { defaultTheme } from 'react-autosuggest/dist/theme';
....
....
const GithubMostPopularList = () => {
.....
.....
return (
<div className={classes.container}>
{console.log('GITHUB USER ', JSON.stringify(globalStore.githubUser))}
<div className={classes.tableAndFabContainer}>
{globalStore.loading ? (
<div className={classes.spinner}>
<LoadingSpinner />
</div>
) : (
<div className={classes.table}>
{console.log('VALUE IS ', value)}
<div className={classes.inputandButtonContainer} >
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={onSuggestionsFetchRequested}
onSuggestionsClearRequested={onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
theme={{
...defaultTheme,
container: classes.react_autosuggest__container,
input: classes.react_autosuggest__input,
inputOpen: classes.react_autosuggest__input__open,
inputFocused: classes.react_autosuggest__input__focused,
suggestionsContainer: classes.react_autosuggest__suggestions_container,
suggestionsContainerOpen: classes.react_autosuggest__suggestions_container__open,
suggestionsList: classes.react_autosuggest__suggestions_list,
suggestion: classes.react_autosuggest__suggestion,
suggestionHighlighted: classes.react_autosuggest__suggestion__highlighted,
}
}
/>
</div>
</div>
)}
</div>
</div>
)
}
export default GithubMostPopularList
Not sure about what you did. This is what I did, and it works well :
One component with the definition of your styling, and the Autosuggest component you render :
import { makeStyles } from '#material-ui/styles';
const useStyles = makeStyles({
container: {
position: "relative",
},
input: {
width: "240px",
height: "30px",
width: '80%',
padding: "10px 20px",
fontFamily: "Helvetica, sans-serif",
fontWeight: "bold",
fontSize: "16px",
border: "1px solid #aaa",
borderRadius: "4px"
},
inputFocused: {
outlineStyle: "none"
}
// add other styling here...
});
const MyAutosuggest = (props) => {
const inputProps = {
placeholder: 'Type a programming language',
value: props.value,
onChange: props.onChange
};
const theme = useStyles();
return(
<Autosuggest
suggestions={props.suggestions}
onSuggestionsFetchRequested={props.onSuggestionsFetchRequested}
onSuggestionsClearRequested={props.onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
theme={theme}
/>
)
}
I import MyAutosuggest in the component where I implement autosuggest :
import MyAutosuggest from './Autosuggest';
<MyAutosuggest
value={this.state.value}
onChange={this.onChange}
suggestions={this.state.suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
/>
I have installed react-bar-chart using
npm i react-bar-chart --save
And I was using below code to display the bar chart, I can see the bar chart rendering on UI, but color is defaulted to black, I tried adding style={{color: 'blue'}}, but it's not working.
import React, { Component } from "react";
import BarChart from "react-bar-chart";
class Metrics extends Component {
render() {
const data = [
{ text: "DOB", value: 500 },
{ text: "Address", value: 300 },
{ text: "Email", value: 900 },
{ text: "Phone", value: 100 },
{ text: "Name", value: 700 }
];
const margin = { top: 20, right: 20, bottom: 30, left: 40 };
return (
<div>
<div style={{ width: "50%" }}>
<BarChart
ylabel="Quantity"
width={500}
height={500}
margin={margin}
data={data}
style={{ color: "blue" }}
/>
</div>
</div>
);
}
}
export default Metrics;
As it renders as svg, you need to use fill property instead of color:
.bar { fill: blue; }
I have an instagram widget thas uses iframe, but when I switch between routes, the widget loads too slow and does'nt have time to render properly.
Can You tell me, how to set delay rendering of the component, jr another solution to this problem?
Here is the component:
import React, { Component } from 'react';
const divStyle = [
{
border: 'none',
overflow: 'hidden',
width: '100%'
},
{
font: "10px/14px 'Roboto','Helvetica Neue',Arial,Helvetica,sans-serif",
fontWeight: '400',
width: '100%',
textAlign: 'right'
},
{
color: '#777',
textDecoration: 'none'
}
];
class Instagram extends Component {
render() {
return (
<div id="instagram">
<iframe src="https://snapwidget.com/embed/711808" className="snapwidget-widget" allowtransparency="true" frameborder="0" scrolling="no" style={divStyle[0]}></iframe>
</div>
);
}
}
export default Instagram;
Also the code is located in the CodeSandbox.
Thanks for any help!
This can be possible solution from your code sandbox.
NOTE: Please Replace your loader with loading div.
CodeSandbox: https://codesandbox.io/s/damp-platform-950yw
import React, { Component } from "react";
const divStyle = [
{
border: "none",
overflow: "hidden",
width: "100%"
},
{
font: "10px/14px 'Roboto','Helvetica Neue',Arial,Helvetica,sans-serif",
fontWeight: "400",
width: "100%",
textAlign: "right"
},
{
color: "#777",
textDecoration: "none"
}
];
class Instagram extends Component {
state = {
loading: true
}
handelOnLoad = () => {
this.setState({
loading: false
})
}
render() {
return (
<>
{this.state.loading && <div style={{
position: "fixed",
background: "rgba(0,0,0,0.7)",
top: 0,
bottom: 0,
right: 0,
left: 0,
color: "#fff"
}}>Loading</div>}
<div id="instagram">
<iframe
src="https://snapwidget.com/embed/711808"
className="snapwidget-widget"
allowtransparency="true"
frameborder="0"
scrolling="no"
style={divStyle[0]}
onLoad={this.handelOnLoad}
/>
</div>
</>
);
}
}
export default Instagram;
You can make use of state to render,
class Instagram extends Component {
state={
show: false
}
componentDidMount(){
setTimeout(()=>{
this.setState({show: true})
},5000) //runs after 5sec
}
render() {
return (
<div id="instagram">
{ this.state.show && <iframe src="https://snapwidget.com/embed/711808" className="snapwidget-widget" allowtransparency="true" frameborder="0" scrolling="no" style={divStyle[0]}></iframe> }
</div>
);
}
}