All files / RadioButton RadioButton.tsx

100% Statements 18/18
83.33% Branches 5/6
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183              6x 6x                                                                                         6x                     6x           6x               101x   101x     20x 15x 15x 15x           9x     101x                   20x                                           6x   6x   6x                           6x                                                                          
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
 
import { lucidClassNames } from '../../util/style-helpers';
import { StandardProps, Overwrite } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-RadioButton');
const { bool, func, object, string } = PropTypes;
 
export interface IRadioButtonPropsRaw extends StandardProps {
	/** Indicates whether the component should appear and act disabled by having
	 * a "greyed out" palette and ignoring user interactions.
	 *
	 * @default false
	 */
	isDisabled: boolean;
 
	/** Indicates that the component is in the "selected" state when true and in
	 * the "unselected" state when false.
	 *
	 * @default false
	 */
	isSelected: boolean;
 
	/** Optional name for the input element */
	name?: string;
 
	/** Called when the user clicks on the component or when they press the space
	 * key while the component is in focus, and only called when the component
	 * is in the unselected state.
	 */
	onSelect: (
		isSelected: boolean,
		{
			event,
			props,
		}: {
			event: React.MouseEvent<HTMLSpanElement>;
			props: IRadioButtonProps;
		}
	) => void;
}
 
export type IRadioButtonProps = Overwrite<
	React.DetailedHTMLProps<
		React.HTMLAttributes<HTMLSpanElement>,
		HTMLSpanElement
	>,
	IRadioButtonPropsRaw
>;
 
/** TODO: Remove nonPassThroughs when the component is converted to a functional component */
const nonPassThroughs = [
	'callbackId',
	'children',
	'className',
	'isDisabled',
	'isSelected',
	'name',
	'onSelect',
	'style',
];
 
export const defaultProps = {
	isDisabled: false,
	isSelected: false,
	onSelect: _.noop,
};
 
export const RadioButton = (props: IRadioButtonProps): React.ReactElement => {
	const {
		className,
		isDisabled,
		isSelected,
		onSelect,
		style,
		...passThroughs
	} = props;
 
	const nativeElement = React.createRef<HTMLInputElement>();
 
	function handleClicked(event: React.MouseEvent<HTMLSpanElement>): void {
		if (!isDisabled && !isSelected) {
			onSelect(true, { event, props });
			if (nativeElement.current) {
				nativeElement.current.focus();
			}
		}
	}
 
	function handleSpanClick(e: React.MouseEvent<HTMLSpanElement>): void {
		e.preventDefault();
	}
 
	return (
		<span
			className={cx(
				'&',
				{
					'&-is-disabled': isDisabled,
					'&-is-selected': isSelected,
				},
				className
			)}
			onClick={(e) => handleClicked(e)}
			style={style}
		>
			<input
				onChange={_.noop}
				{..._.omit(passThroughs, nonPassThroughs)}
				checked={isSelected}
				className={cx('&-native')}
				disabled={isDisabled}
				ref={nativeElement}
				type='radio'
			/>
			<span onClick={handleSpanClick} className={cx('&-visualization-glow')} />
			<span
				onClick={handleSpanClick}
				className={cx('&-visualization-container')}
			/>
			<span onClick={handleSpanClick} className={cx('&-visualization-dot')} />
		</span>
	);
};
 
RadioButton.defaultProps = defaultProps;
 
RadioButton.displayName = 'RadioButton';
 
RadioButton.peek = {
	description: `\`RadioButton\` is a round two-state toggle used to create \`RadioButtonLabeled\`. It uses a hidden native checkbox control under the hood but leverages other \`HTML\` elements to visualize its state.`,
	notes: {
		overview: `RadioButton is a round two-state toggle. Use \`RadioButtonLabeled\` or \`RadioGroup\` in your applications.`,
		intendedUse: `Used to create \`RadioButtonLabeled\` and \`RadioGroup\`.`,
		technicalRecommendations: `
			- Use the Selected state when a filter or setting will be applied.
			- Use the Unselected state when a filter or setting will not be applied.
			- Any props that are not explicitly defined in \`propTypes\` are passed to the native radio button control.
		`,
	},
	categories: ['controls', 'toggles'],
};
 
RadioButton.propTypes = {
	/**
		Appended to the component-specific class names set on the root element.
	*/
	className: string,
 
	/**
		Indicates whether the component should appear and act disabled by having
		a "greyed out" palette and ignoring user interactions.
	*/
	isDisabled: bool,
 
	/**
		Indicates that the component is in the "selected" state when true and in
		the "unselected" state when false.
	*/
	isSelected: bool,
 
	/**
	Optional name for the input element.
	*/
	name: string,
 
	/**
		Called when the user clicks on the component or when they press the space
		key while the component is in focus, and only called when the component
		is in the unselected state.  Signature: \`(true, { event, props }) => {}\`
	*/
	onSelect: func,
 
	/**
		Passed through to the root element.
	*/
	style: object,
};
 
export default RadioButton;