Skip to content

Incubator.TextField - should float with defaultValue #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/incubator/TextField/FloatingPlaceholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useContext, useRef, useCallback, useState, useMemo} from 'react';
import {Animated, LayoutChangeEvent, StyleSheet, Platform} from 'react-native';
import {useDidUpdate} from 'hooks';
import {FloatingPlaceholderProps, ValidationMessagePosition} from './types';
import {getColorByState} from './Presenter';
import {getColorByState, shouldPlaceholderFloat} from './Presenter';
import {Colors} from '../../style';
import {Constants} from '../../commons/new';
import View from '../../components/view';
Expand All @@ -11,31 +11,32 @@ import FieldContext from './FieldContext';

const FLOATING_PLACEHOLDER_SCALE = 0.875;

const FloatingPlaceholder = ({
placeholder,
floatingPlaceholderColor = Colors.$textNeutralLight,
floatingPlaceholderStyle,
floatOnFocus,
validationMessagePosition,
extraOffset = 0,
testID
}: FloatingPlaceholderProps) => {
const FloatingPlaceholder = (props: FloatingPlaceholderProps) => {
const {
placeholder,
floatingPlaceholderColor = Colors.$textNeutralLight,
floatingPlaceholderStyle,
validationMessagePosition,
extraOffset = 0,
testID
} = props;
const context = useContext(FieldContext);
const [placeholderOffset, setPlaceholderOffset] = useState({
top: 0,
left: 0
});
const animation = useRef(new Animated.Value(Number((floatOnFocus && context.isFocused) || context.hasValue))).current;

const shouldFloat = shouldPlaceholderFloat(props, context.isFocused, context.hasValue, context.value);
const animation = useRef(new Animated.Value(Number(shouldFloat))).current;
const hidePlaceholder = !context.isValid && validationMessagePosition === ValidationMessagePosition.TOP;

useDidUpdate(() => {
const toValue = floatOnFocus ? context.isFocused || context.hasValue : context.hasValue;
Animated.timing(animation, {
toValue: Number(toValue),
toValue: Number(shouldFloat),
duration: 200,
useNativeDriver: true
}).start();
}, [floatOnFocus, context.isFocused, context.hasValue]);
}, [shouldFloat]);

const animatedStyle = useMemo(() => {
return {
Expand Down
10 changes: 9 additions & 1 deletion src/incubator/TextField/Presenter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import {Colors} from './../../style';
import {ColorType, Validator, FieldContextType} from './types';
import {ColorType, Validator, FieldContextType, FloatingPlaceholderProps} from './types';
// TODO: Fix this import after moving all TextField types to a single file after we move to the new docs
import {TextFieldProps} from './index';
import formValidators from './validators';
Expand Down Expand Up @@ -75,3 +75,11 @@ export function shouldHidePlaceholder({floatingPlaceholder, hint, floatOnFocus}:
return false;
}
}

export function shouldPlaceholderFloat({defaultValue, floatOnFocus}: FloatingPlaceholderProps,
isFocused: boolean,
hasValue: boolean,
value?: string) {
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)
return (floatOnFocus && isFocused) || hasValue || useDefaultValue;
}
1 change: 1 addition & 0 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const TextField = (props: InternalTextFieldProps) => {
<View flex={!centered} flexG={centered} /* flex row */>
{floatingPlaceholder && (
<FloatingPlaceholder
defaultValue={others.defaultValue}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass the input's 'value'? Why do we need a separate prop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a prop from RN which we inherit and the user that opened the bug is using

placeholder={placeholder}
floatingPlaceholderStyle={_floatingPlaceholderStyle}
floatingPlaceholderColor={floatingPlaceholderColor}
Expand Down
3 changes: 2 additions & 1 deletion src/incubator/TextField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface FloatingPlaceholderProps {
floatOnFocus?: boolean;
validationMessagePosition?: ValidationMessagePosition;
extraOffset?: number;
defaultValue?: TextInputProps['defaultValue'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'TextInputProps' are the OLD TextField's prop... why use them in the incubator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's RN's props

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok...

testID: string;
}

Expand Down Expand Up @@ -142,7 +143,7 @@ export interface InputProps
/**
* Use react-native-gesture-handler instead of react-native for the base TextInput
*/
useGestureHandlerInput?: boolean;
useGestureHandlerInput?: boolean;
}

export type TextFieldProps = MarginModifiers &
Expand Down