All files / TextFieldValidated TextFieldValidated.tsx

92.3% Statements 24/26
90.47% Branches 19/21
33.33% Functions 1/3
92.3% Lines 24/26

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                        2x   2x           2x   2x 2x     2x     2x                                                     2x   2x   2x         2x   2x                                                             2x   15x   15x         15x       15x 4x       11x 2x 9x 2x       15x   15x                                            
import _ from 'lodash';
import React from 'react';
import PropTypes, { shape } from 'prop-types';
 
import Validation, { IValidationProps } from '../Validation/Validation';
import TextField, {
	ITextFieldPropsWithPassThroughs,
} from '../TextField/TextField';
import reducers from '../TextField/TextField.reducers';
import { lucidClassNames } from '../../util/style-helpers';
import { findTypes } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-TextFieldValidated');
 
const { any, object, string, bool } = PropTypes;
 
export interface ITextFieldValidatedErrorProps extends IValidationProps {
	description?: string;
}
 
const TextFieldValidatedError = (_props: ITextFieldValidatedErrorProps): null =>
	null;
TextFieldValidatedError.displayName = 'TextFieldValidated.Error';
TextFieldValidatedError.peek = {
	description: `Content that will be displayed as an error message.`,
};
TextFieldValidatedError.propName = 'Error';
 
/** TODO: Remove the nonPassThroughs when the component is converted to a functional component */
const nonPassThroughs = [
	'style',
	'className',
	'Error',
	'Info',
	'Success',
	'initialState',
];
 
export interface ITextFieldValidatedProps
	extends ITextFieldPropsWithPassThroughs {
	Error?: React.ReactNode;
	Info?: string;
	Success?: {
		message: string;
		disappearing?: boolean;
	};
}
 
export interface ITextFieldValidatedState {
	value: string | number;
}
 
class TextFieldValidated extends React.Component<
	ITextFieldValidatedProps,
	ITextFieldValidatedState
> {
	static displayName = 'TextFieldValidated';
 
	static Error = TextFieldValidatedError;
 
	static peek = {
		description: `A composite of \`TextField\` and \`Validation\`.`,
		categories: ['controls', 'text'],
	};
 
	static reducers = reducers;
 
	static propTypes = {
		/**
			Passed to the root container.
		*/
		style: object,
 
		/**
			Passed to the root container.
		*/
		className: string,
 
		/**
			Prop alternative to Error child component
		*/
		Error: any,
 
		/**
			Optional information text that is styled less aggressively than an error
		 */
		Info: string,
 
		/**
			Optional information text that is styled as "success". Also contains
		  `disappearing` prop for message that appears and fades away.
		 */
		Success: shape({
			message: string,
			disappearing: bool,
		}),
	};
 
	static defaultProps = TextField.defaultProps;
 
	private textFieldRef = React.createRef<TextField>();
 
	focus = () => {
		this.textFieldRef.current && this.textFieldRef.current.focus();
	};
 
	render() {
		const { className, style, ...passThroughs } = this.props;
 
		let childProps;
 
		if (this.props.Error) {
			childProps = _.map(
				findTypes(this.props, TextFieldValidated.Error),
				'props'
			);
		} else if (this.props.Info) {
			childProps = [this.props.Info];
		} else if (this.props.Success && this.props.Success.message) {
			childProps = [this.props.Success.message];
		}
 
		const isSuccess =
			!this.props.Error && !this.props.Info && this.props.Success;
 
		return (
			<Validation
				className={cx('&', className, {
					'-info': !this.props.Error && this.props.Info,
					'-success':
						!this.props.Error && !this.props.Info && this.props.Success,
					'-disappearing':
						isSuccess && this.props.Success && this.props.Success.disappearing,
				})}
				style={style}
				Error={childProps}
			>
				<TextField
					{...(_.omit(passThroughs, nonPassThroughs) as any)}
					ref={this.textFieldRef}
				/>
			</Validation>
		);
	}
}
 
export default TextFieldValidated;