blob: c680edc7e4944f1cfc552d561c5d4ebad239e91e (
plain)
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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
import React from "react";
export class TopSiteFormInput extends React.PureComponent {
constructor(props) {
super(props);
this.state = { validationError: this.props.validationError };
this.onChange = this.onChange.bind(this);
this.onMount = this.onMount.bind(this);
this.onClearIconPress = this.onClearIconPress.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.shouldFocus && !this.props.shouldFocus) {
this.input.focus();
}
if (nextProps.validationError && !this.props.validationError) {
this.setState({ validationError: true });
}
// If the component is in an error state but the value was cleared by the parent
if (this.state.validationError && !nextProps.value) {
this.setState({ validationError: false });
}
}
onClearIconPress(event) {
// If there is input in the URL or custom image URL fields,
// and we hit 'enter' while tabbed over the clear icon,
// we should execute the function to clear the field.
if (event.key === "Enter") {
this.props.onClear();
}
}
onChange(ev) {
if (this.state.validationError) {
this.setState({ validationError: false });
}
this.props.onChange(ev);
}
onMount(input) {
this.input = input;
}
renderLoadingOrCloseButton() {
const showClearButton = this.props.value && this.props.onClear;
if (this.props.loading) {
return (
<div className="loading-container">
<div className="loading-animation" />
</div>
);
} else if (showClearButton) {
return (
<button
type="button"
className="icon icon-clear-input icon-button-style"
onClick={this.props.onClear}
onKeyPress={this.onClearIconPress}
/>
);
}
return null;
}
render() {
const { typeUrl } = this.props;
const { validationError } = this.state;
return (
<label>
<span data-l10n-id={this.props.titleId} />
<div
className={`field ${typeUrl ? "url" : ""}${
validationError ? " invalid" : ""
}`}
>
<input
type="text"
value={this.props.value}
ref={this.onMount}
onChange={this.onChange}
data-l10n-id={this.props.placeholderId}
// Set focus on error if the url field is valid or when the input is first rendered and is empty
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={this.props.autoFocusOnOpen}
disabled={this.props.loading}
/>
{this.renderLoadingOrCloseButton()}
{validationError && (
<aside
className="error-tooltip"
data-l10n-id={this.props.errorMessageId}
/>
)}
</div>
</label>
);
}
}
TopSiteFormInput.defaultProps = {
showClearButton: false,
value: "",
validationError: false,
};
|