Skip to content

Commit

Permalink
fix(RadioButtons): allow for options to be updated via change of the …
Browse files Browse the repository at this point in the history
…options prop
  • Loading branch information
agreatscott committed Oct 28, 2021
1 parent 9f34fba commit b9f258f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
24 changes: 6 additions & 18 deletions src/RadioButtons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@ i.e.
/>
*/

const RadioButtons = (props) => {
const [radioOptions, setRadioOptions] = useState([]);

useEffect(() => {
setRadioOptions(props.options);
}, []);

const { initialValue, ...nativeElementProps } = props;

const RadioButtons = ({ options, name, initialValue, ...containerProps }) => {
return (
<div
className="nds-radiobutton-group nds-typography"
{...nativeElementProps}
>
{Object.entries(radioOptions).map(([label, value]) => (
<div className="nds-radiobutton-group nds-typography" {...containerProps}>
{Object.entries(options).map(([label, value]) => (
<div className="nds-radiobutton-container" key={value}>
<label className="nds-label" key={label}>
<label className="nds-label">
{label}
<input
type="radio"
defaultChecked={initialValue === value}
value={value}
name={props.name}
name={name}
/>
<div className="nds-checkmark"></div>
</label>
Expand All @@ -47,12 +36,11 @@ const RadioButtons = (props) => {

RadioButtons.propTypes = {
options: PropTypes.object,
disabled: PropTypes.bool,
name: PropTypes.string,
initialValue: PropTypes.any,
};

RadioButtons.defaultProps = {
disabled: false,
initialValue: false,
};

Expand Down
42 changes: 42 additions & 0 deletions src/RadioButtons/index.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useState } from "react";
import RadioButtons from "./";

const Template = (args) => <RadioButtons {...args} />;

export const Overview = Template.bind({});
Overview.args = {
options: {
OptionA: "A",
OptionB: "B",
OptionC: "C",
},
name: "options",
};

export const Example = () => {
const [value, setValue] = useState();
return (
<div className="nds-typography">
<h3> What is your favourite colour? </h3>
<RadioButtons
options={{
Red: "red",
Blue: "blue",
Green: "green",
Yellow: "yellow",
}}
name="colours"
onChange={(e) => {
setValue(e.target.value);
}}
value={value}
/>
<div>{value && `You have selected ${value}`}</div>
</div>
);
};

export default {
title: "Components/RadioButtons",
component: RadioButtons,
};

0 comments on commit b9f258f

Please sign in to comment.