All files / Switch Switch.tsx

100% Statements 16/16
50% Branches 2/4
100% Functions 2/2
100% Lines 16/16

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            3x 3x                                                                                                   3x             3x                 66x   66x         25x   25x 25x 25x 25x         66x                                                                             3x 3x 3x         3x                                                                          
import _, { omit } from 'lodash';
import React, { createRef } from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { StandardProps, Overwrite } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Switch');
const { bool, func, object, string } = PropTypes;
 
export interface ISwitchPropsRaw 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;
 
	/** Called when the user clicks on the component or when they press the space
	 * key while the component is in focus.
	 *
	 * @default _.noop
	 * */
	onSelect: (
		isSelected: boolean,
		{
			event,
			props,
		}: {
			event:
				| React.MouseEvent<HTMLSpanElement>
				| React.TouchEvent<HTMLSpanElement>;
			props: ISwitchProps;
		}
	) => void;
 
	/** Offers a red/green styling to the switch.
	 *
	 * @default false
	 */
	isIncludeExclude: boolean;
}
 
export type ISwitchProps = Overwrite<
	React.DetailedHTMLProps<
		React.HTMLAttributes<HTMLSpanElement>,
		HTMLSpanElement
	>,
	ISwitchPropsRaw
>;
 
const defaultProps = {
	isDisabled: false,
	isSelected: false,
	onSelect: _.noop,
	isIncludeExclude: false,
};
 
export const Switch = (props: ISwitchProps): React.ReactElement => {
	const {
		className,
		isDisabled,
		isSelected,
		style,
		isIncludeExclude,
		onSelect,
		...passThroughs
	} = props;
 
	const nativeElement = createRef<HTMLInputElement>();
 
	function handleClicked(
		event: React.MouseEvent<HTMLSpanElement> | React.TouchEvent<HTMLSpanElement>
	): void {
		event.preventDefault();
 
		if (!isDisabled) {
			onSelect(!isSelected, { event, props });
			if (nativeElement.current) {
				nativeElement.current.focus();
			}
		}
	}
 
	return (
		<span
			className={cx(
				'&',
				{
					'&-is-disabled': isDisabled,
					'&-is-selected': isSelected,
					'&-is-include-exclude': isIncludeExclude,
				},
				className
			)}
			onClick={handleClicked}
			onTouchEnd={handleClicked}
			style={style}
		>
			<input
				onChange={_.noop}
				{...omit(
					passThroughs,
					[
						'className',
						'isDisabled',
						'isSelected',
						'onSelect',
						'style',
						'isIncludeExclude',
					].concat(['initialState', 'callbackId', 'children'])
				)}
				checked={isSelected}
				className={cx('&-native')}
				disabled={isDisabled}
				ref={nativeElement}
				type='checkbox'
			/>
			<span className={cx('&-visualization-container')} />
			<span className={cx('&-visualization-handle')} />
		</span>
	);
};
Switch.defaultProps = defaultProps;
Switch.displayName = 'Switch';
Switch.peek = {
	description: `A toggle -- a component that is in one of two particular states at any given moment in time -- that uses a visualization of a physical on/off switch made popular by smartphone OSes to reflect its current state. It uses a hidden native check box control under the hood but leverages other \`HTML\` elements to visualize its state.`,
	categories: ['controls', 'toggles'],
};
 
Switch.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,
 
	/**
		Called when the user clicks on the component or when they press the space
		key while the component is in focus.  Signature:
		\`(isSelected, { event, props }) => {}\`
	*/
	onSelect: func,
 
	/**
		Passed through to the root element.
	*/
	style: object,
 
	/**
		Offers a red/green styling to the switch.
	*/
	isIncludeExclude: bool,
};
 
export default Switch;