Skip to content

Commit 6305921

Browse files
authored
Fix issue with TextField hint is missing when using floatingPlaceholder (#1659)
1 parent adeb3f3 commit 6305921

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { FieldContextType } from './FieldContext';
22
import { ColorType, Validator } from './types';
3+
import { TextFieldProps } from './index';
34
export declare function getColorByState(color?: ColorType, context?: FieldContextType): string | undefined;
45
export declare function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?];
56
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;
7+
export declare function shouldHidePlaceholder({ floatingPlaceholder, hint, floatOnFocus }: TextFieldProps, isFocused: boolean): boolean;

generatedTypes/src/incubator/TextField/usePreset.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
920920
error: any;
921921
disabled: string;
922922
};
923-
floatingPlaceholderStyle: {
923+
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) | {
924924
color?: import("react-native").ColorValue | undefined;
925925
fontFamily?: string | undefined;
926926
fontSize?: number | undefined;
@@ -1032,7 +1032,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
10321032
translateY?: number | undefined;
10331033
textAlignVertical?: "auto" | "center" | "top" | "bottom" | undefined;
10341034
includeFontPadding?: boolean | undefined;
1035-
} | ((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);
1035+
};
10361036
floatOnFocus?: boolean | undefined;
10371037
enableErrors: boolean;
10381038
validationMessage?: string | string[] | undefined;

src/incubator/TextField/Presenter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import _ from 'lodash';
22
import {Colors} from './../../style';
33
import {FieldContextType} from './FieldContext';
44
import {ColorType, Validator} from './types';
5+
// TODO: Fix this import after moving all TextField types to a single file after we move to the new docs
6+
import {TextFieldProps} from './index';
57
import formValidators from './validators';
68

79
export function getColorByState(color?: ColorType, context?: FieldContextType) {
@@ -62,3 +64,14 @@ export function getRelevantValidationMessage(validationMessage: string | string[
6264
return validationMessage[failingValidatorIndex];
6365
}
6466
}
67+
68+
export function shouldHidePlaceholder({floatingPlaceholder, hint, floatOnFocus}: TextFieldProps, isFocused: boolean) {
69+
if (floatingPlaceholder) {
70+
if (hint && isFocused) {
71+
return !floatOnFocus;
72+
}
73+
return true;
74+
} else {
75+
return false;
76+
}
77+
}

src/incubator/TextField/__tests__/Presenter.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,22 @@ describe('TextField:Presenter', () => {
5959
expect(uut.getRelevantValidationMessage(messages, 1)).toBe(messages[1]);
6060
});
6161
});
62+
63+
describe('Should hide placeholder', () => {
64+
it('should keep it visible when floatingPlaceholder is false', () => {
65+
expect(uut.shouldHidePlaceholder({floatingPlaceholder: false})).toBe(false);
66+
});
67+
68+
it('should hide it when using floatingPlaceholder', () => {
69+
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true})).toBe(true);
70+
});
71+
72+
it('should show it when floatingPlaceholder is true, user passed a hint text, the field is focused and floatOnFocus is true', () => {
73+
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: true}, true)).toBe(false);
74+
});
75+
76+
it('should hide it when floatingPlaceholder is true, user passed a hint text, the field is focused but floatOnFocus is false', () => {
77+
expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: false}, true)).toBe(true);
78+
});
79+
});
6280
});

src/incubator/TextField/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import {
1919
ColorsModifiers
2020
} from '../../commons/new';
2121
import View from '../../components/view';
22+
import {Colors} from '../../style';
2223
import {ValidationMessagePosition, Validator} from './types';
24+
import {shouldHidePlaceholder} from './Presenter';
2325
import Input, {InputProps} from './Input';
2426
import ValidationMessage, {ValidationMessageProps} from './ValidationMessage';
2527
import Label, {LabelProps} from './Label';
@@ -155,6 +157,7 @@ const TextField = (props: InternalTextFieldProps) => {
155157
const colorStyle = useMemo(() => color && {color}, [color]);
156158

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

159162
return (
160163
<FieldContext.Provider value={context}>
@@ -190,7 +193,7 @@ const TextField = (props: InternalTextFieldProps) => {
190193
)}
191194
{children || (
192195
<Input
193-
placeholderTextColor={floatingPlaceholder ? 'transparent' : undefined}
196+
placeholderTextColor={hidePlaceholder ? 'transparent' : Colors.grey30}
194197
{...others}
195198
style={[typographyStyle, colorStyle, others.style]}
196199
onFocus={onFocus}

0 commit comments

Comments
 (0)