Skip to content

Commit a069062

Browse files
Noemi RozparaTrancever
authored andcommitted
fix: fix label behavior when TextInput is in error state (#1429)
1 parent 9ac6407 commit a069062

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/components/TextInput/Label/InputLabel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const InputLabel = (props: InputLabelProps) => {
2525
topPosition,
2626
paddingOffset,
2727
placeholderColor,
28+
errorColor,
2829
} = props.labelProps;
2930

3031
const labelTranslationX = {
@@ -123,7 +124,7 @@ const InputLabel = (props: InputLabelProps) => {
123124
labelStyle,
124125
paddingOffset,
125126
{
126-
color: placeholderColor,
127+
color: error && errorColor ? errorColor : placeholderColor,
127128
opacity: placeholderOpacity,
128129
},
129130
]}

src/components/TextInput/TextInput.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ class TextInput extends React.Component<TextInputProps, State> {
184184
}
185185

186186
state = {
187-
labeled: new Animated.Value(this.props.value || this.props.error ? 0 : 1),
187+
labeled: new Animated.Value(this.props.value ? 0 : 1),
188188
error: new Animated.Value(this.props.error ? 1 : 0),
189189
focused: false,
190-
placeholder: this.props.error ? this.props.placeholder : '',
190+
placeholder: '',
191191
value: this.props.value || this.props.defaultValue,
192192
labelLayout: {
193193
measured: false,
@@ -202,27 +202,25 @@ class TextInput extends React.Component<TextInputProps, State> {
202202
if (
203203
prevState.focused !== this.state.focused ||
204204
prevState.value !== this.state.value ||
205-
prevProps.error !== this.props.error ||
206205
this.props.defaultValue
207206
) {
208207
// The label should be minimized if the text input is focused, or has text
209208
// In minimized mode, the label moves up and becomes small
210-
if (this.state.value || this.state.focused || this.props.error) {
211-
this.minmizeLabel();
209+
if (this.state.value || this.state.focused) {
210+
this.minimizeLabel();
212211
} else {
213212
this.restoreLabel();
214213
}
215214
}
216215

217216
if (
218217
prevState.focused !== this.state.focused ||
219-
prevProps.label !== this.props.label ||
220-
prevProps.error !== this.props.error
218+
prevProps.label !== this.props.label
221219
) {
222-
// Show placeholder text only if the input is focused, or has error, or there's no label
220+
// Show placeholder text only if the input is focused, or there's no label
223221
// We don't show placeholder if there's a label because the label acts as placeholder
224222
// When focused, the label moves up, so we can show a placeholder
225-
if (this.state.focused || this.props.error || !this.props.label) {
223+
if (this.state.focused || !this.props.label) {
226224
this.showPlaceholder();
227225
} else {
228226
this.hidePlaceholder();
@@ -305,7 +303,7 @@ class TextInput extends React.Component<TextInputProps, State> {
305303
}),
306304
}).start();
307305

308-
private minmizeLabel = () =>
306+
private minimizeLabel = () =>
309307
Animated.timing(this.state.labeled, {
310308
toValue: 0,
311309
duration: BLUR_ANIMATION_DURATION,

src/components/TextInput/TextInputFlat.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ class TextInputFlat extends React.Component<ChildTextInputProps, {}> {
8888
paddingHorizontal: number;
8989
};
9090

91-
let inputTextColor, activeColor, underlineColorCustom, placeholderColor;
91+
let inputTextColor,
92+
activeColor,
93+
underlineColorCustom,
94+
placeholderColor,
95+
errorColor;
9296

9397
if (disabled) {
9498
inputTextColor = activeColor = color(colors.text)
@@ -101,6 +105,7 @@ class TextInputFlat extends React.Component<ChildTextInputProps, {}> {
101105
inputTextColor = colors.text;
102106
activeColor = error ? colors.error : colors.primary;
103107
placeholderColor = colors.placeholder;
108+
errorColor = colors.error;
104109
underlineColorCustom = underlineColor || colors.disabled;
105110
}
106111

@@ -202,6 +207,7 @@ class TextInputFlat extends React.Component<ChildTextInputProps, {}> {
202207
hasActiveOutline,
203208
activeColor,
204209
placeholderColor,
210+
errorColor,
205211
};
206212

207213
const minHeight =

src/components/TextInput/TextInputOutlined.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TextInputOutlined extends React.Component<ChildTextInputProps, {}> {
8484
activeColor,
8585
outlineColor,
8686
placeholderColor,
87+
errorColor,
8788
containerStyle;
8889

8990
if (disabled) {
@@ -96,6 +97,7 @@ class TextInputOutlined extends React.Component<ChildTextInputProps, {}> {
9697
inputTextColor = colors.text;
9798
activeColor = error ? colors.error : colors.primary;
9899
placeholderColor = outlineColor = colors.placeholder;
100+
errorColor = colors.error;
99101
}
100102

101103
const labelScale = MINIMIZED_LABEL_FONT_SIZE / fontSize;
@@ -177,6 +179,7 @@ class TextInputOutlined extends React.Component<ChildTextInputProps, {}> {
177179
activeColor,
178180
placeholderColor,
179181
backgroundColor,
182+
errorColor,
180183
};
181184

182185
const minHeight = height || (dense ? MIN_DENSE_HEIGHT : MIN_HEIGHT);

src/components/TextInput/types.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export type LabelProps = {
5757
label?: string | null | undefined;
5858
hasActiveOutline: boolean | null | undefined;
5959
activeColor: string;
60+
errorColor?: string;
6061
error: boolean | null | undefined;
6162
onLayoutAnimatedText: (args: any) => void;
6263
};

0 commit comments

Comments
 (0)