Skip to content

Instantly share code, notes, and snippets.

@yankeyhotel
Created August 25, 2022 15:24
Show Gist options
  • Save yankeyhotel/ecde37d6838c63e2b883ca9c3b9dab6e to your computer and use it in GitHub Desktop.
Save yankeyhotel/ecde37d6838c63e2b883ca9c3b9dab6e to your computer and use it in GitHub Desktop.
React button group | target parent onClick w/ props
import React from 'react';
const Button = (props) => {
const name = props.name;
const msg = props.msg;
const id = props.id;
const color = (msg === 'on') ? 'green' : 'red';
return (
<div style={{ padding: '1rem', border: '1px solid' + color }}>
<button onClick={() => props.onClick(props.id)}>
{name} is {msg}
</button>
</div>
);
}
export default Button;
import React, { useState } from "react";
import classnames from "classnames";
import classnamesBind from 'classnames/bind';
import Button from "./Button";
import scss from "./OnOff.module.scss";
const cx = classnamesBind.bind(scss);
const OnOff = ({ buttons }) => {
const [clickedId, setClickId] = useState(-1);
return (
{buttons.map( (buttonLabel, i) => (
<Button
key={i}
id={i}
msg={(i === clickedId) ? 'on' : 'off'}
name={buttonLabel}
onClick={(id) => setClickId(id)}
/>
))}
</div>
);
}
export default OnOff;
.customButton {
background-color: #E0314B;
border: 1.5px solid #E0314B;
border-radius: 5px;
cursor: pointer;
color: white;
height: 50px;
font-size: 32px;
letter-spacing: 0.5px;
width: 150px;
}
.active {
background-color: rgb(25, 26, 24);
border: 1.5px solid rgb(25, 26, 24);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment