Skip to content

Commit f602ca8

Browse files
fix: Fix initial label position when value is set on React Native >= 0.61
1 parent 9ac6407 commit f602ca8

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
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: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,20 @@ 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(
188+
(this.props.value !== undefined
189+
? this.props.value
190+
: this.props.defaultValue)
191+
? 0
192+
: 1
193+
),
188194
error: new Animated.Value(this.props.error ? 1 : 0),
189195
focused: false,
190-
placeholder: this.props.error ? this.props.placeholder : '',
191-
value: this.props.value || this.props.defaultValue,
196+
placeholder: '',
197+
value:
198+
this.props.value !== undefined
199+
? this.props.value
200+
: this.props.defaultValue,
192201
labelLayout: {
193202
measured: false,
194203
width: 0,
@@ -202,27 +211,27 @@ class TextInput extends React.Component<TextInputProps, State> {
202211
if (
203212
prevState.focused !== this.state.focused ||
204213
prevState.value !== this.state.value ||
205-
prevProps.error !== this.props.error ||
206-
this.props.defaultValue
214+
// workaround for animated regression for react native > 0.61
215+
// https://github.com/callstack/react-native-paper/pull/1440
216+
prevState.labelLayout !== this.state.labelLayout
207217
) {
208218
// The label should be minimized if the text input is focused, or has text
209219
// In minimized mode, the label moves up and becomes small
210-
if (this.state.value || this.state.focused || this.props.error) {
211-
this.minmizeLabel();
220+
if (this.state.value || this.state.focused) {
221+
this.minimizeLabel();
212222
} else {
213223
this.restoreLabel();
214224
}
215225
}
216226

217227
if (
218228
prevState.focused !== this.state.focused ||
219-
prevProps.label !== this.props.label ||
220-
prevProps.error !== this.props.error
229+
prevProps.label !== this.props.label
221230
) {
222-
// Show placeholder text only if the input is focused, or has error, or there's no label
231+
// Show placeholder text only if the input is focused, or there's no label
223232
// We don't show placeholder if there's a label because the label acts as placeholder
224233
// When focused, the label moves up, so we can show a placeholder
225-
if (this.state.focused || this.props.error || !this.props.label) {
234+
if (this.state.focused || !this.props.label) {
226235
this.showPlaceholder();
227236
} else {
228237
this.hidePlaceholder();
@@ -305,7 +314,7 @@ class TextInput extends React.Component<TextInputProps, State> {
305314
}),
306315
}).start();
307316

308-
private minmizeLabel = () =>
317+
private minimizeLabel = () =>
309318
Animated.timing(this.state.labeled, {
310319
toValue: 0,
311320
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)