Skip to content

Commit

Permalink
Add a "depth" prop to limit the recursion depth when rendering radio …
Browse files Browse the repository at this point in the history
…buttons deeply inside a radio group
  • Loading branch information
cheton committed Feb 14, 2018
1 parent b4d24b1 commit 6750fe8
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 18 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ The label prop is optional, you can use children to pass through the component.

### RadioGroup

When rendering radio buttons deeply nested inside the radio group, you need to pass a `depth` prop to limit the recursion depth.

```jsx
<RadioGroup
name="comic"
value={this.state.value}
depth={3} // This is needed to minimize the recursion overhead
onChange={(value, event) => {
this.setState({ value: value });
}}
Expand All @@ -84,7 +87,7 @@ The label prop is optional, you can use children to pass through the component.
</RadioGroup>
```

## React onChange Propagation
## Prevent onChange Propagation

You may need to use `event.stopPropagation()` to stop **onChange** propagation when wrapping an input element inside the **RadioGroup** or **RadioButton** component.

Expand All @@ -93,11 +96,6 @@ You may need to use `event.stopPropagation()` to stop **onChange** propagation w
name="radiogroup"
value={this.state.value}
onChange={(value, event) => {
if (typeof value === 'object') {
// You can prevent onChange propagation in one place
return;
}
this.setState({ value: value });
}}
>
Expand Down Expand Up @@ -157,6 +155,7 @@ name | String | | Name for the input element group.
value | any | | The value of the radio group.
defaultValue | any | | The default value of the radio group.
onChange | Function | | Callback function that will be invoked when the value changes.
depth | Number | 1 | Limits the recursion depth when rendering radio buttons deeply inside a radio group.

### Class Properties

Expand Down
2 changes: 1 addition & 1 deletion dist/react-radio.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/react-radio.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
</head>
<body style="background-color: #eee">
<div id="container"></div>
<script type="text/javascript" src="bundle.js?d8b5f732f7a8a1aeeef2"></script></body>
<script type="text/javascript" src="bundle.js?c79412a76c8c8cdfb4de"></script></body>
</html>
1 change: 1 addition & 0 deletions examples/ControlledRadioGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class extends PureComponent {
<RadioGroup
name="ports"
value={this.state.ports}
depth={2}
onChange={this.handleChangeByKey('ports')}
>
<div>
Expand Down
1 change: 1 addition & 0 deletions examples/UncontrolledRadioGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class extends PureComponent {
<p>{`Selected: "${this.state.ports}"`}</p>
<RadioGroup
name="ports"
depth={2}
onChange={this.handleChangeByKey('ports')}
>
<div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trendmicro/react-radio",
"version": "3.1.3",
"version": "3.2.0",
"description": "React Radio component",
"main": "lib/index.js",
"files": [
Expand Down
17 changes: 12 additions & 5 deletions src/RadioGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import RadioButton from './RadioButton';
class RadioGroup extends PureComponent {
static propTypes = {
disabled: PropTypes.bool,
onChange: PropTypes.func,
name: PropTypes.string,
value: PropTypes.any,
defaultValue: PropTypes.any
defaultValue: PropTypes.any,
onChange: PropTypes.func,
depth: PropTypes.number
};

static defaultProps = {
disabled: false
disabled: false,
depth: 1
};

state = {
Expand All @@ -37,7 +40,11 @@ class RadioGroup extends PureComponent {
}
};

renderChildren = (children) => {
renderChildren = (children, depth = 1) => {
if (depth > this.props.depth) {
return children;
}

const mapChild = (child) => {
if (!React.isValidElement(child) || !child.props) {
return child;
Expand All @@ -63,7 +70,7 @@ class RadioGroup extends PureComponent {

if (child.props.children && typeof child.props.children === 'object') {
return cloneElement(child, {
children: this.renderChildren(child.props.children)
children: this.renderChildren(child.props.children, depth + 1)
});
}

Expand Down

0 comments on commit 6750fe8

Please sign in to comment.