Commit 0f8e0fca by jaebradley

prevent unnecessary rerenders from radio buttons

increase package version
parent 7e05e7f1
{ {
"name": "@edx/paragon", "name": "@edx/paragon",
"version": "1.1.4", "version": "1.1.5",
"description": "Accessible, responsive UI component library based on Bootstrap.", "description": "Accessible, responsive UI component library based on Bootstrap.",
"main": "src/index.js", "main": "src/index.js",
"author": "arizzitano", "author": "arizzitano",
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"prop-types": "^15.5.8", "prop-types": "^15.5.8",
"react": "^15.5.4", "react": "^15.5.4",
"react-dom": "^15.5.4", "react-dom": "^15.5.4",
"react-element-proptypes": "^1.0.0",
"react-proptype-conditional-require": "^1.0.4" "react-proptype-conditional-require": "^1.0.4"
}, },
"devDependencies": { "devDependencies": {
......
/* eslint-disable react/no-multi-comp */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import ElementPropTypes from 'react-element-proptypes';
function RadioButton(props) { class RadioButton extends React.PureComponent {
const { constructor(props) {
children, super(props);
index,
isChecked, const {
name, onBlur,
onBlur, onClick,
onClick, onFocus,
onFocus, onKeyDown,
onKeyDown, } = props;
value,
...other this.onBlur = onBlur.bind(this);
} = props; this.onClick = onClick.bind(this);
this.onFocus = onFocus.bind(this);
return ( this.onKeyDown = onKeyDown.bind(this);
<div> }
<input
type={'radio'} render() {
name={name} const {
aria-checked={isChecked} children,
defaultChecked={isChecked} index,
value={value} isChecked,
data-index={index} name,
onBlur={event => onBlur(event)} value,
onClick={event => onClick(event)} ...other
onFocus={event => onFocus(event)} } = this.props;
onKeyDown={event => onKeyDown(event)}
{...other} return (
/>{children} <div>
</div> <input
); type={'radio'}
name={name}
aria-checked={isChecked}
defaultChecked={isChecked}
value={value}
data-index={index}
onBlur={this.onBlur}
onClick={this.onClick}
onFocus={this.onFocus}
onKeyDown={this.onKeyDown}
{...other}
/>{children}
</div>
);
}
} }
...@@ -40,6 +57,8 @@ class RadioButtonGroup extends React.Component { ...@@ -40,6 +57,8 @@ class RadioButtonGroup extends React.Component {
super(); super();
// Bind the method to the component context // Bind the method to the component context
this.renderChildren = this.renderChildren.bind(this); this.renderChildren = this.renderChildren.bind(this);
this.onChange = this.onChange.bind(this);
this.state = { this.state = {
selectedIndex: props.selectedIndex, selectedIndex: props.selectedIndex,
}; };
...@@ -57,15 +76,24 @@ class RadioButtonGroup extends React.Component { ...@@ -57,15 +76,24 @@ class RadioButtonGroup extends React.Component {
renderChildren() { renderChildren() {
return React.Children.map((this.props.children), (child, index) => const {
children,
name,
onBlur,
onClick,
onFocus,
onKeyDown,
} = this.props;
return React.Children.map((children), (child, index) =>
React.cloneElement(child, { React.cloneElement(child, {
name: this.props.name, name,
value: child.props.value, value: child.props.value,
isChecked: index === this.state.selectedIndex, isChecked: index === this.state.selectedIndex,
onBlur: this.props.onBlur, onBlur,
onClick: this.props.onClick, onClick,
onFocus: this.props.onFocus, onFocus,
onKeyDown: this.props.onKeyDown, onKeyDown,
index, index,
}), }),
); );
...@@ -89,7 +117,7 @@ class RadioButtonGroup extends React.Component { ...@@ -89,7 +117,7 @@ class RadioButtonGroup extends React.Component {
<div <div
role={'radiogroup'} role={'radiogroup'}
aria-label={label} aria-label={label}
onChange={event => this.onChange(event)} onChange={this.onChange}
tabIndex={-1} tabIndex={-1}
{...other} {...other}
> >
...@@ -141,7 +169,7 @@ RadioButtonGroup.defaultProps = { ...@@ -141,7 +169,7 @@ RadioButtonGroup.defaultProps = {
}; };
RadioButtonGroup.propTypes = { RadioButtonGroup.propTypes = {
children: PropTypes.arrayOf(RadioButton).isRequired, children: PropTypes.arrayOf(ElementPropTypes.elementOfType(RadioButton)).isRequired,
label: PropTypes.string.isRequired, label: PropTypes.string.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
onBlur: PropTypes.func, onBlur: PropTypes.func,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment