Skip to content

Commit 8f2a148

Browse files
authored
Incubator.TextField - should float with defaultValue (#2344)
* Incubator.TextField - should float with defaultValue * Handle edge cases * Fix comment * Move logic to the Presenter
1 parent 950cb64 commit 8f2a148

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

src/incubator/TextField/FloatingPlaceholder.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {useContext, useRef, useCallback, useState, useMemo} from 'react';
22
import {Animated, LayoutChangeEvent, StyleSheet, Platform} from 'react-native';
33
import {useDidUpdate} from 'hooks';
44
import {FloatingPlaceholderProps, ValidationMessagePosition} from './types';
5-
import {getColorByState} from './Presenter';
5+
import {getColorByState, shouldPlaceholderFloat} from './Presenter';
66
import {Colors} from '../../style';
77
import {Constants} from '../../commons/new';
88
import View from '../../components/view';
@@ -11,31 +11,32 @@ import FieldContext from './FieldContext';
1111

1212
const FLOATING_PLACEHOLDER_SCALE = 0.875;
1313

14-
const FloatingPlaceholder = ({
15-
placeholder,
16-
floatingPlaceholderColor = Colors.$textNeutralLight,
17-
floatingPlaceholderStyle,
18-
floatOnFocus,
19-
validationMessagePosition,
20-
extraOffset = 0,
21-
testID
22-
}: FloatingPlaceholderProps) => {
14+
const FloatingPlaceholder = (props: FloatingPlaceholderProps) => {
15+
const {
16+
placeholder,
17+
floatingPlaceholderColor = Colors.$textNeutralLight,
18+
floatingPlaceholderStyle,
19+
validationMessagePosition,
20+
extraOffset = 0,
21+
testID
22+
} = props;
2323
const context = useContext(FieldContext);
2424
const [placeholderOffset, setPlaceholderOffset] = useState({
2525
top: 0,
2626
left: 0
2727
});
28-
const animation = useRef(new Animated.Value(Number((floatOnFocus && context.isFocused) || context.hasValue))).current;
28+
29+
const shouldFloat = shouldPlaceholderFloat(props, context.isFocused, context.hasValue, context.value);
30+
const animation = useRef(new Animated.Value(Number(shouldFloat))).current;
2931
const hidePlaceholder = !context.isValid && validationMessagePosition === ValidationMessagePosition.TOP;
3032

3133
useDidUpdate(() => {
32-
const toValue = floatOnFocus ? context.isFocused || context.hasValue : context.hasValue;
3334
Animated.timing(animation, {
34-
toValue: Number(toValue),
35+
toValue: Number(shouldFloat),
3536
duration: 200,
3637
useNativeDriver: true
3738
}).start();
38-
}, [floatOnFocus, context.isFocused, context.hasValue]);
39+
}, [shouldFloat]);
3940

4041
const animatedStyle = useMemo(() => {
4142
return {

src/incubator/TextField/Presenter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import {Colors} from './../../style';
3-
import {ColorType, Validator, FieldContextType} from './types';
3+
import {ColorType, Validator, FieldContextType, FloatingPlaceholderProps} from './types';
44
// TODO: Fix this import after moving all TextField types to a single file after we move to the new docs
55
import {TextFieldProps} from './index';
66
import formValidators from './validators';
@@ -75,3 +75,11 @@ export function shouldHidePlaceholder({floatingPlaceholder, hint, floatOnFocus}:
7575
return false;
7676
}
7777
}
78+
79+
export function shouldPlaceholderFloat({defaultValue, floatOnFocus}: FloatingPlaceholderProps,
80+
isFocused: boolean,
81+
hasValue: boolean,
82+
value?: string) {
83+
const useDefaultValue = !_.isEmpty(defaultValue) && value === undefined; // To consider a user that has deleted the defaultValue (and then the placeholder should un-float when losing focus)
84+
return (floatOnFocus && isFocused) || hasValue || useDefaultValue;
85+
}

src/incubator/TextField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const TextField = (props: InternalTextFieldProps) => {
133133
{children || <View flex={!centered} flexG={centered} /* flex row */>
134134
{floatingPlaceholder && (
135135
<FloatingPlaceholder
136+
defaultValue={others.defaultValue}
136137
placeholder={placeholder}
137138
floatingPlaceholderStyle={_floatingPlaceholderStyle}
138139
floatingPlaceholderColor={floatingPlaceholderColor}

src/incubator/TextField/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export interface FloatingPlaceholderProps {
8686
floatOnFocus?: boolean;
8787
validationMessagePosition?: ValidationMessagePosition;
8888
extraOffset?: number;
89+
defaultValue?: TextInputProps['defaultValue'];
8990
testID: string;
9091
}
9192

0 commit comments

Comments
 (0)