All files / components/SwitchLabeled SwitchLabeled.jsx

0% Statements 0/21
0% Branches 0/6
0% Functions 0/4
0% Lines 0/21

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                                                                                                                                                                                                                                                                                                     
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { CSSTransition } from 'react-transition-group';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, getFirst, omitProps } from '../../util/component-types';
import Switch from '../Switch/Switch';
 
const cx = lucidClassNames.bind('&-SwitchLabeled');
const { any, node, object, string } = PropTypes;
 
const SwitchLabeled = createClass({
	displayName: 'SwitchLabeled',
 
	statics: {
		peek: {
			description: `
				This is a composite of the \`Switch\` component and the native
				\`label\` element.
			`,
			categories: ['controls', 'toggles'],
			madeFrom: ['Switch'],
		},
	},
 
	components: {
		Label: createClass({
			displayName: 'SwitchLabeled.Label',
			statics: {
				peek: {
					description: `
						Label to be shown alongside the switch.
					`,
				},
			},
			propName: 'Label',
			propTypes: {
				children: node`
					Used to identify the purpose of this switch to the user -- can be any
					renderable content.
				`,
			},
		}),
	},
 
	propTypes: {
		...Switch.propTypes,
 
		className: string`
			Appended to the component-specific class names set on the root element.
		`,
 
		style: object`
			Passed through to the root element.
		`,
 
		Label: any`
			Child element whose children are used to identify the purpose of this
			switch to the user.
		`,
	},
 
	getDefaultProps() {
		return {
			isDisabled: false,
			isSelected: false,
			onSelect: _.noop,
		};
	},
 
	UNSAFE_componentWillMount() {
		this._labelKey = 0;
	},
 
	UNSAFE_componentWillReceiveProps(nextProps) {
		const currentLabel = _.get(
			getFirst(this.props, SwitchLabeled.Label),
			'props.children',
			null
		);
		const nextLabel = _.get(
			getFirst(nextProps, SwitchLabeled.Label),
			'props.children',
			null
		);
 
		if (currentLabel !== nextLabel) {
			this._labelKey++;
		}
	},
 
	render() {
		const {
			className,
			isDisabled,
			isSelected,
			onSelect,
			style,
			...passThroughs
		} = this.props;
 
		const labelChildProps = _.get(
			getFirst(this.props, SwitchLabeled.Label),
			'props'
		);
		const isShown = !!labelChildProps;
 
		return (
			<label
				className={cx(
					'&',
					{
						'&-is-disabled': isDisabled,
						'&-is-selected': isSelected,
					},
					className
				)}
				style={style}
			>
				<Switch
					className={className}
					isDisabled={isDisabled}
					isSelected={isSelected}
					onSelect={onSelect}
					{...omitProps(passThroughs, SwitchLabeled, [], false)}
				/>
				{labelChildProps && (
					<CSSTransition
						in={isShown}
						classNames={cx('&-text')}
						timeout={100}
						style={{ position: 'relative' }}
						className={cx('&-text')}
						unmountOnExit
					>
						<span key={this._labelKey}>
							{labelChildProps.children || labelChildProps}
						</span>
					</CSSTransition>
				)}
			</label>
		);
	},
});
 
export default SwitchLabeled;