All files / Button Button.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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                    176x   176x                                                                                                                 176x                 176x                       52x   52x     14x       14x 14x   14x     52x                                                               176x   176x   176x               176x   176x                                                                              
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import _ from 'lodash';
import { lucidClassNames } from '../../util/style-helpers';
import {
	omitProps,
	StandardProps,
	Overwrite,
} from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Button');
 
const { arrayOf, bool, func, node, oneOf, oneOfType, string } = PropTypes;
 
export interface IButtonPropsRaw extends StandardProps {
	/**
	 * Disables the Button by greying it out
	 *
	 * @default false
	 **/
	isDisabled: boolean;
 
	/**
	 * Activates the Button by giving it a "pressed down" look
	 *
	 * @default false
	 **/
	isActive: boolean;
 
	/**
	 * Set this to `true` if you want the Button to only contain an icon.
	 *
	 * @default false
	 * */
	hasOnlyIcon: boolean;
 
	/** Style variations of the Button */
	kind?: 'primary' | 'link' | 'danger' | 'invisible';
 
	/** Size variations of the Button */
	size?: 'short' | 'small' | 'large';
 
	/** Called when the user clicks the \`Button\`. */
	onClick: ({
		event,
		props,
	}: {
		event: React.MouseEvent<HTMLButtonElement>;
		props: IButtonProps;
	}) => void;
 
	/**
	 * Form element type variations of Button. Passed through to DOM Button.
	 *
	 * @default "button"
	 * */
	type: 'submit' | 'reset' | 'button';
 
	callbackId?: any;
}
 
export type IButtonProps = Overwrite<
	React.DetailedHTMLProps<
		React.ButtonHTMLAttributes<HTMLButtonElement>,
		HTMLButtonElement
	>,
	IButtonPropsRaw
>;
 
const defaultProps = {
	isDisabled: false,
	isActive: false,
	onClick: _.noop,
	type: 'button' as const,
	hasOnlyIcon: false,
};
 
/** Test Button description */
export const Button = (props: IButtonProps): React.ReactElement => {
	const {
		isDisabled,
		isActive,
		onClick,
		hasOnlyIcon,
		kind,
		size,
		className,
		children,
		type,
		...passThroughs
	} = props;
 
	const buttonRef = React.createRef<HTMLButtonElement>();
 
	function handleClick(event: React.MouseEvent<HTMLButtonElement>): void {
		Eif (!isDisabled) {
			// required to correctly apply the focus state in Safari and Firefox
			// (still valid 2019-07-22)
 
			Eif (buttonRef.current) {
				buttonRef.current.focus();
			}
			onClick({ event, props: props });
		}
	}
	return (
		<button
			{...omitProps(passThroughs, undefined, [
				..._.keys(Button.propTypes),
				'callbackId',
			])}
			ref={buttonRef}
			className={cx(
				'&',
				{
					'&-is-disabled': isDisabled,
					'&-is-active': isActive,
					'&-primary': kind === 'primary',
					'&-link': kind === 'link',
					'&-invisible': kind === 'invisible',
					'&-danger': kind === 'danger',
					'&-short': size === 'short',
					'&-small': size === 'small',
					'&-large': size === 'large',
					'&-has-only-icon': hasOnlyIcon,
				},
				className
			)}
			onClick={handleClick}
			disabled={isDisabled}
			type={type}
		>
			<span className={cx('&-content')}>{children}</span>
		</button>
	);
};
 
Button.defaultProps = defaultProps;
 
Button.displayName = 'Button';
 
Button.peek = {
	description: `
		A basic button. Any props that are not explicitly called out below will
		be passed through to the native \`button\` component.
	`,
	categories: ['controls', 'buttons'],
};
 
Button.propName = 'Button';
 
Button.propTypes = {
	isDisabled: bool`
		Disables the Button by greying it out
	`,
 
	isActive: bool`
		Activates the Button by giving it a "pressed down" look
	`,
 
	className: string`
		Class names that are appended to the defaults
	`,
 
	hasOnlyIcon: bool`
		Set this to \`true\` if you want the Button to only contain an icon.
	`,
 
	children: oneOfType([node, arrayOf(node)])`
		Any valid React children
	`,
 
	kind: oneOf(['primary', 'link', 'danger', 'invisible'])`
		Style variations of the Button
	`,
 
	size: oneOf(['short', 'small', 'large'])`
		Size variations of the Button
	`,
 
	onClick: func`
		Called when the user clicks the \`Button\`.
	`,
 
	type: string`
		Form element type variations of Button. Passed through to DOM Button.
	`,
};
 
export default Button;