File size: 3,646 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { FormInputValidation, FormLabel } from '@automattic/components';
import clsx from 'clsx';
import { Component } from 'react';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import FormTextInput from 'calypso/components/forms/form-text-input';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';
import scrollIntoViewport from 'calypso/lib/scroll-into-viewport';

export default class Input extends Component {
	static defaultProps = { autoFocus: false, autoComplete: 'on' };

	inputRef = ( element ) => {
		this.inputElement = element;

		if ( ! this.props.inputRef ) {
			return;
		}

		if ( typeof inputRef === 'function' ) {
			this.props.inputRef( element );
		} else {
			this.props.inputRef.current = element;
		}
	};

	componentDidMount() {
		this.setupInputModeHandlers();
		this.autoFocusInput();
	}

	setupInputModeHandlers = () => {
		const inputElement = this.inputRef.current;

		if ( inputElement && this.props.inputMode === 'numeric' ) {
			// This forces mobile browsers to use a numeric keyboard. We have to
			// toggle the pattern on and off to avoid getting errors against the
			// masked value (which could contain characters other than digits).
			//
			// This workaround is based on the following StackOverflow post:
			// http://stackoverflow.com/a/19998430/821706
			inputElement.addEventListener( 'touchstart', () => ( inputElement.pattern = '\\d*' ) );

			[ 'keydown', 'blur' ].forEach( ( eventName ) =>
				inputElement.addEventListener( eventName, () => ( inputElement.pattern = '.*' ) )
			);
		}
	};

	componentDidUpdate( oldProps ) {
		if ( oldProps.disabled && ! this.props.disabled ) {
			// We focus when the state goes from disabled to enabled. This is needed because we show a disabled input
			// until we receive data from the server.
			this.autoFocusInput();
		}
	}

	focus = () => {
		const node = this.inputElement;
		if ( node ) {
			node.focus();
			scrollIntoViewport( node, {
				behavior: 'smooth',
				scrollMode: 'if-needed',
			} );
		}
	};

	autoFocusInput = () => {
		if ( this.props.autoFocus ) {
			this.focus();
		}
	};

	recordFieldClick = () => {
		if ( this.props.eventFormName ) {
			gaRecordEvent( 'Upgrades', `Clicked ${ this.props.eventFormName } Field`, this.props.name );
		}
	};

	render() {
		const classes = clsx(
			this.props.additionalClasses,
			this.props.name,
			this.props.labelClass,
			this.props.classes
		);

		const validationId = `validation-field-${ this.props.name }`;

		return (
			<div className={ classes }>
				<FormLabel htmlFor={ this.props.name } { ...this.props.labelProps }>
					{ this.props.label }
				</FormLabel>
				<FormTextInput
					aria-invalid={ this.props.isError }
					aria-describedby={ validationId }
					placeholder={ this.props.placeholder ? this.props.placeholder : this.props.label }
					id={ this.props.name }
					value={ this.props.value }
					name={ this.props.name }
					autoFocus={ this.props.autoFocus } // eslint-disable-line jsx-a11y/no-autofocus
					autoComplete={ this.props.autoComplete }
					disabled={ this.props.disabled }
					maxLength={ this.props.maxLength }
					onBlur={ this.props.onBlur }
					onChange={ this.props.onChange }
					onClick={ this.recordFieldClick }
					isError={ this.props.isError }
					inputRef={ this.inputRef }
				/>
				{ this.props.errorMessage && (
					<FormInputValidation id={ validationId } text={ this.props.errorMessage } isError />
				) }
				{ this.props.description && ! this.props.errorMessage && (
					<FormSettingExplanation>{ this.props.description }</FormSettingExplanation>
				) }
			</div>
		);
	}
}