All files / Checkbox Checkbox.tsx

100% Statements 16/16
80% Branches 8/10
100% Functions 3/3
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                    176x   176x                                                                                           176x             176x                   30x   30x     9x       16x 16x 16x 16x         30x                                                                                                         176x 176x                                             176x   176x                                                                            
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import {
	omitProps,
	StandardProps,
	Overwrite,
} from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Checkbox');
 
const { bool, func, object, string } = PropTypes;
 
export interface ICheckboxPropsRaw extends StandardProps {
	/** Indicates whether the component should appear in an "indeterminate" or
	 * "partially checked" state. This prop takes precedence over
	 * \`isSelected\`.
	 */
	isIndeterminate: boolean;
 
	/** Indicates whether the component should appear and act disabled by having
	 * a "greyed out" palette and ignoring user interactions.
	 */
	isDisabled: boolean;
 
	/** Indicates that the component is in the "selected" state when true and in
	 * the "unselected" state when false. This props is ignored if
	 * \`isIndeterminate\` is \`true\`.
	 */
	isSelected: boolean;
 
	/** Called when the user clicks on the component or when they press the space
	 * key while the component is in focus.
	 */
	onSelect: (
		isSelected: boolean,
		{
			event,
			props,
		}: {
			event: React.MouseEvent<HTMLInputElement>;
			props: ICheckboxProps;
		}
	) => void;
 
	/** A string title that is displayed on hover. */
	title?: string;
}
 
export type ICheckboxProps = Overwrite<
	React.DetailedHTMLProps<
		React.InputHTMLAttributes<HTMLInputElement>,
		HTMLInputElement
	>,
	ICheckboxPropsRaw
>;
 
export const defaultProps = {
	isIndeterminate: false,
	isDisabled: false,
	isSelected: false,
	onSelect: _.noop,
};
 
export const Checkbox = (props: ICheckboxProps): React.ReactElement => {
	const {
		className,
		isIndeterminate,
		isSelected,
		isDisabled,
		style,
		title,
		onSelect,
		...passThroughs
	} = props;
 
	const nativeElement = React.createRef<HTMLInputElement>();
 
	function handleSpanClick(e: React.MouseEvent<HTMLInputElement>): void {
		e.preventDefault();
	}
 
	function handleClicked(event: React.MouseEvent<HTMLInputElement>): void {
		Eif (!isDisabled && onSelect) {
			onSelect(!isSelected, { event, props });
			Eif (nativeElement.current) {
				nativeElement.current.focus();
			}
		}
	}
 
	return (
		<div
			className={cx(
				'&',
				{
					'&-is-disabled': isDisabled,
					'&-is-selected': isIndeterminate || isSelected,
				},
				className
			)}
			onClick={handleClicked}
			style={style}
			title={title}
		>
			<input
				onChange={_.noop}
				{...omitProps(
					passThroughs,
					undefined,
					['children'].concat(_.keys(Checkbox.propTypes))
				)}
				checked={isSelected}
				className={cx('&-native')}
				disabled={isDisabled}
				ref={nativeElement}
				title={title}
				type='checkbox'
			/>
			<span onClick={handleSpanClick} className={cx('&-visualization-glow')} />
			<span
				onClick={handleSpanClick}
				className={cx('&-visualization-container')}
			/>
			{isIndeterminate ? (
				<span
					onClick={handleSpanClick}
					className={cx('&-visualization-indeterminate')}
				>
					<span className={cx('&-visualization-indeterminate-line')} />
				</span>
			) : (
				<span
					onClick={handleSpanClick}
					className={cx('&-visualization-checkmark')}
				>
					<span className={cx('&-visualization-checkmark-stem')} />
					<span className={cx('&-visualization-checkmark-kick')} />
				</span>
			)}
		</div>
	);
};
 
Checkbox.displayName = 'Checkbox';
Checkbox.peek = {
	description: `
		Checkbox is a square two-state toggle used to create \`CheckboxLabeled\`.
 
		It uses a hidden native checkbox control under the hood but leverages
		other HTML elements to visualize its state.
	`,
	notes: {
		overview: `
			Checkbox is a square two-state toggle. Use \`CheckboxLabeled\` in your applications.
		`,
		intendedUse: `
			Used to create \`CheckboxLabeled\`. 			
		`,
		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.
			- Use the Indeterminate state for parent checkboxes where some of the child checkboxes are Selected and some are Unselected. For example, the master checkbox in the header row of the interactive table example in \`DataTable\`.
		`,
	},
	categories: ['controls', 'toggles'],
};
 
Checkbox.defaultProps = defaultProps;
 
Checkbox.propTypes = {
	className: string`
		Appended to the component-specific class names set on the root element.
	`,
 
	isIndeterminate: bool`
		Indicates whether the component should appear in an "indeterminate" or
		"partially checked" state. This prop takes precedence over
		\`isSelected\`.
	`,
 
	isDisabled: bool`
		Indicates whether the component should appear and act disabled by having
		a "greyed out" palette and ignoring user interactions.
	`,
 
	isSelected: bool`
		Indicates that the component is in the "selected" state when true and in
		the "unselected" state when false. This props is ignored if
		\`isIndeterminate\` is \`true\`.
	`,
 
	onSelect: func`
		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 }) => {}\`
	`,
 
	style: object`
		Passed through to the root element.
	`,
 
	title: string`
		A string title that is displayed on hover.
	`,
};
 
export default Checkbox;