Skip to content

Fix issue with TextField hint is missing when using floatingPlaceholder #1659

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 3 commits into from
Nov 15, 2021
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
2 changes: 2 additions & 0 deletions generatedTypes/src/incubator/TextField/Presenter.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FieldContextType } from './FieldContext';
import { ColorType, Validator } from './types';
import { TextFieldProps } from './index';
export declare function getColorByState(color?: ColorType, context?: FieldContextType): string | undefined;
export declare function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?];
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;
export declare function shouldHidePlaceholder({ floatingPlaceholder, hint, floatOnFocus }: TextFieldProps, isFocused: boolean): boolean;
4 changes: 2 additions & 2 deletions generatedTypes/src/incubator/TextField/usePreset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
error: any;
disabled: string;
};
floatingPlaceholderStyle: {
floatingPlaceholderStyle: ((false | import("react-native").TextStyle | import("react-native").RegisteredStyle<import("react-native").TextStyle> | import("react-native").RecursiveArray<import("react-native").TextStyle | import("react-native").Falsy | import("react-native").RegisteredStyle<import("react-native").TextStyle>> | null) & import("react-native").TextStyle) | {
color?: import("react-native").ColorValue | undefined;
fontFamily?: string | undefined;
fontSize?: number | undefined;
Expand Down Expand Up @@ -1032,7 +1032,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
translateY?: number | undefined;
textAlignVertical?: "auto" | "center" | "top" | "bottom" | undefined;
includeFontPadding?: boolean | undefined;
} | ((false | import("react-native").TextStyle | import("react-native").RegisteredStyle<import("react-native").TextStyle> | import("react-native").RecursiveArray<import("react-native").TextStyle | import("react-native").Falsy | import("react-native").RegisteredStyle<import("react-native").TextStyle>> | null) & import("react-native").TextStyle);
};
floatOnFocus?: boolean | undefined;
enableErrors: boolean;
validationMessage?: string | string[] | undefined;
Expand Down
13 changes: 13 additions & 0 deletions src/incubator/TextField/Presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import _ from 'lodash';
import {Colors} from './../../style';
import {FieldContextType} from './FieldContext';
import {ColorType, Validator} 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';

export function getColorByState(color?: ColorType, context?: FieldContextType) {
Expand Down Expand Up @@ -62,3 +64,14 @@ export function getRelevantValidationMessage(validationMessage: string | string[
return validationMessage[failingValidatorIndex];
}
}

export function shouldHidePlaceholder({floatingPlaceholder, hint, floatOnFocus}: TextFieldProps, isFocused: boolean) {
if (floatingPlaceholder) {
if (hint && isFocused) {
return !floatOnFocus;
}
return true;
} else {
return false;
}
}
18 changes: 18 additions & 0 deletions src/incubator/TextField/__tests__/Presenter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ describe('TextField:Presenter', () => {
expect(uut.getRelevantValidationMessage(messages, 1)).toBe(messages[1]);
});
});

describe('Should hide placeholder', () => {
it('should keep it visible when floatingPlaceholder is false', () => {
expect(uut.shouldHidePlaceholder({floatingPlaceholder: false})).toBe(false);
});

it('should hide it when using floatingPlaceholder', () => {
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true})).toBe(true);
});

it('should show it when floatingPlaceholder is true, user passed a hint text, the field is focused and floatOnFocus is true', () => {
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: true}, true)).toBe(false);
});

it('should hide it when floatingPlaceholder is true, user passed a hint text, the field is focused but floatOnFocus is false', () => {
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: false}, true)).toBe(true);
});
});
});
5 changes: 4 additions & 1 deletion src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
ColorsModifiers
} from '../../commons/new';
import View from '../../components/view';
import {Colors} from '../../style';
import {ValidationMessagePosition, Validator} from './types';
import {shouldHidePlaceholder} from './Presenter';
import Input, {InputProps} from './Input';
import ValidationMessage, {ValidationMessageProps} from './ValidationMessage';
import Label, {LabelProps} from './Label';
Expand Down Expand Up @@ -155,6 +157,7 @@ const TextField = (props: InternalTextFieldProps) => {
const colorStyle = useMemo(() => color && {color}, [color]);

const fieldStyle = isFunction(fieldStyleProp) ? fieldStyleProp(context, {preset: props.preset}) : fieldStyleProp;
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);

return (
<FieldContext.Provider value={context}>
Expand Down Expand Up @@ -190,7 +193,7 @@ const TextField = (props: InternalTextFieldProps) => {
)}
{children || (
<Input
placeholderTextColor={floatingPlaceholder ? 'transparent' : undefined}
placeholderTextColor={hidePlaceholder ? 'transparent' : Colors.grey30}
{...others}
style={[typographyStyle, colorStyle, others.style]}
onFocus={onFocus}
Expand Down