All files / components/RadioGroup RadioGroup.jsx

100% Statements 15/15
75% Branches 3/4
100% Functions 1/1
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                  120x             120x                     120x                                                                                                                   120x                           19x   19x 19x             19x       19x           50x                                   3x 3x       3x 3x     3x          
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, getFirst, findTypes, rejectTypes, omitProps } from '../../util/component-types';
import reducers from './RadioGroup.reducers';
 
import RadioButtonLabeled from '../RadioButtonLabeled/RadioButtonLabeled';
import RadioButton from '../RadioButton/RadioButton';
 
const cx = lucidClassNames.bind('&-RadioGroup');
 
const {
	func,
	node,
	number,
	string,
} = React.PropTypes;
 
/**
 * {"categories": ["controls", "toggles"], "madeFrom": ["RadioButton"]}
 *
 * This is a group of related radio buttons whose values are mutually exclusive
 * and one whom must be selected any given moment in time.
 
 * Any props that are not explicitly defined in `propTypes` are spread onto the
 * root element.
 */
const RadioGroup = createClass({
	displayName: 'RadioGroup',
 
	components: {
		RadioButton,
 
		/**
		 * Support radio button labels as `RadioGroup.Label` component which can
		 * be provided as a child of a `RadioGroup.RadioButton` component.
		 */
		Label: createClass({
			displayName: 'RadioGroup.Label',
			propTypes: {
				children: node,
			},
		}),
	},
 
	reducers,
 
	propTypes: {
		/**
		 * Should be instances of `RadioGroup.RadioButton` which supports the
		 * same props as `RadioButton`.
		 */
		children: node,
 
		/**
		 * Appended to the component-specific class names set on the root
		 * element.
		 */
		className: string,
 
		/**
		 * Passed along to the `RadioGroup.RadioButton` children whose `name`
		 * props are ignored.
		 */
		name: string,
 
		/**
		 * Called when the user clicks on one of the child radio buttons or when
		 * they press the space key while one is in focus, and only called when
		 * the component is in the unselected state. `props` refers to the child
		 * `RadioButton` props.
		 *
		 * Signature: `(selectedIndex, { event, props }) => {}`
		 */
		onSelect: func,
 
		/**
		 * Indicates which of the `RadioGroup.RadioButton` children is currently
		 * selected. The index of the last `RadioGroup.RadioButton` child with
		 * `isSelected` equal to true takes precedence over this prop.
		 */
		selectedIndex: number,
	},
 
	getDefaultProps() {
		return {
			name: _.uniqueId(`${cx('&')}-`),
			onSelect: _.noop,
			selectedIndex: 0,
		};
	},
 
	render() {
		const {
			children,
			className,
			name,
			selectedIndex,
			...passThroughs,
		} = this.props;
 
		const radioButtonChildProps = _.map(findTypes(this.props, RadioGroup.RadioButton), 'props')
		const selectedIndexFromChildren = _.findLastIndex(radioButtonChildProps, {
			isSelected: true,
		});
 
		// If there are any `RadioGroup.RadioButton` children with `isSelected`
		// equal to true, then the index of the last one should override the
		// value of the `selectedIndex` prop.
		const actualSelectedIndex = selectedIndexFromChildren !== -1
			? selectedIndexFromChildren
			: selectedIndex;
 
		return (
			<span
				{...omitProps(passThroughs, RadioGroup)}
				className={cx('&', className)}
			>
				{_.map(radioButtonChildProps, (radioButtonChildProp, index) => {
					return (
						<RadioButtonLabeled
							{...radioButtonChildProp}
							isSelected={actualSelectedIndex === index}
							key={index}
							callbackId={index}
							name={name}
							onSelect={this.handleSelected}
							Label={_.get(getFirst(radioButtonChildProp, RadioGroup.Label), 'props', null)}
						/>
					);
				})}
				{rejectTypes(children, RadioGroup.RadioButton)}
			</span>
		);
	},
 
	handleSelected(isSelected, { event, props: childProps }) {
		const { callbackId } = childProps;
		const clickedRadioButtonProps = _.map(findTypes(this.props, RadioGroup.RadioButton), 'props')[callbackId];
 
		// If the `RadioGroup.RadioButton` child has an `onSelect` prop that is
		// a function, call that prior to calling the group's `onSelect` prop.
		Eif (_.isFunction(clickedRadioButtonProps.onSelect)) {
			clickedRadioButtonProps.onSelect(isSelected, { event, props: childProps });
		}
 
		this.props.onSelect(callbackId, { event, props: childProps });
	},
});
 
export default RadioGroup;