Skip to content

Commit a1a32c1

Browse files
authored
centralized all textfield types in a single file (#1933)
1 parent 45f27d9 commit a1a32c1

File tree

11 files changed

+246
-241
lines changed

11 files changed

+246
-241
lines changed

src/incubator/TextField/CharCounter.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import React, {useContext} from 'react';
2-
import {TextStyle, StyleSheet} from 'react-native';
2+
import {StyleSheet} from 'react-native';
33
import _ from 'lodash';
44
import Text from '../../components/text';
55
import FieldContext from './FieldContext';
6-
7-
export interface CharCounterProps {
8-
/**
9-
* Should show a character counter (works only with maxLength)
10-
*/
11-
showCharCounter?: boolean;
12-
maxLength?: number;
13-
/**
14-
* Pass custom style to character counter text
15-
*/
16-
charCounterStyle?: TextStyle;
17-
testID: string;
18-
}
6+
import {CharCounterProps} from './types';
197

208
const CharCounter = ({maxLength, charCounterStyle, testID}: CharCounterProps) => {
219
const {value} = useContext(FieldContext);

src/incubator/TextField/FieldContext.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import _ from 'lodash';
22
import {createContext} from 'react';
3-
4-
export type FieldContextType = {
5-
value?: string;
6-
isFocused: boolean;
7-
hasValue: boolean;
8-
isValid: boolean;
9-
failingValidatorIndex?: number;
10-
disabled: boolean;
11-
validateField: () => void
12-
};
3+
import {FieldContextType} from './types';
134

145
const FieldContext = createContext<FieldContextType>({
156
isFocused: false,

src/incubator/TextField/FloatingPlaceholder.tsx

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
import React, {useContext, useEffect, useRef, useCallback, useState, useMemo} from 'react';
2-
import {Animated, LayoutChangeEvent, StyleSheet, Platform, TextStyle, StyleProp} from 'react-native';
3-
import {ColorType, ValidationMessagePosition} from './types';
2+
import {Animated, LayoutChangeEvent, StyleSheet, Platform} from 'react-native';
3+
import {FloatingPlaceholderProps, ValidationMessagePosition} from './types';
44
import {getColorByState} from './Presenter';
55
import {Colors} from '../../style';
66
import {Constants} from '../../commons/new';
77
import View from '../../components/view';
88
import Text from '../../components/text';
99
import FieldContext from './FieldContext';
1010

11-
export interface FloatingPlaceholderProps {
12-
/**
13-
* The placeholder for the field
14-
*/
15-
placeholder?: string;
16-
/**
17-
* The floating placeholder color
18-
*/
19-
floatingPlaceholderColor?: ColorType;
20-
/**
21-
* Custom style to pass to the floating placeholder
22-
*/
23-
floatingPlaceholderStyle?: StyleProp<TextStyle>;
24-
/**
25-
* Should placeholder float on focus or when start typing
26-
*/
27-
floatOnFocus?: boolean;
28-
validationMessagePosition?: ValidationMessagePosition;
29-
extraOffset?: number;
30-
testID: string;
31-
}
32-
3311
const FLOATING_PLACEHOLDER_SCALE = 0.875;
3412

3513
const FloatingPlaceholder = ({

src/incubator/TextField/Input.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useContext} from 'react';
2-
import {TextInput, TextInputProps, StyleSheet, Platform} from 'react-native';
2+
import {TextInput, StyleSheet, Platform} from 'react-native';
33
import {Constants, ForwardRefInjectedProps} from '../../commons/new';
4-
import {ColorType} from './types';
4+
import {InputProps, ColorType} from './types';
55
import {getColorByState} from './Presenter';
66
import {Colors} from '../../style';
77
import FieldContext from './FieldContext';
@@ -11,26 +11,6 @@ const DEFAULT_INPUT_COLOR: ColorType = {
1111
default: Colors.$textDefault,
1212
disabled: Colors.$textDisabled
1313
};
14-
export interface InputProps
15-
extends Omit<TextInputProps, 'placeholderTextColor'>,
16-
Omit<React.ComponentPropsWithRef<typeof TextInput>, 'placeholderTextColor'> {
17-
/**
18-
* A hint text to display when focusing the field
19-
*/
20-
hint?: string;
21-
/**
22-
* Input color
23-
*/
24-
color?: ColorType;
25-
/**
26-
* placeholder text color
27-
*/
28-
placeholderTextColor?: ColorType;
29-
/**
30-
* Custom formatter for the input value (used only when input if not focused)
31-
*/
32-
formatter?: (value?: string) => string | undefined;
33-
}
3414

3515
const Input = ({
3616
style,

src/incubator/TextField/Label.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
import React, {useContext} from 'react';
2-
import {StyleSheet, TextStyle} from 'react-native';
2+
import {StyleSheet} from 'react-native';
33
import {Colors} from '../../style';
4-
import Text, {TextProps} from '../../components/text';
5-
import {ColorType, ValidationMessagePosition} from './types';
4+
import Text from '../../components/text';
5+
import {LabelProps, ValidationMessagePosition} from './types';
66
import {getColorByState} from './Presenter';
77
import FieldContext from './FieldContext';
88

9-
export interface LabelProps {
10-
/**
11-
* Field label
12-
*/
13-
label?: string;
14-
/**
15-
* Field label color. Either a string or color by state map ({default, focus, error, disabled})
16-
*/
17-
labelColor?: ColorType;
18-
/**
19-
* Custom style for the field label
20-
*/
21-
labelStyle?: TextStyle;
22-
/**
23-
* Pass extra props to the label Text element
24-
*/
25-
labelProps?: TextProps;
26-
validationMessagePosition?: ValidationMessagePosition;
27-
floatingPlaceholder?: boolean;
28-
testID?: string;
29-
}
309

3110
const Label = ({
3211
label,

src/incubator/TextField/Presenter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import _ from 'lodash';
22
import {Colors} from './../../style';
3-
import {FieldContextType} from './FieldContext';
4-
import {ColorType, Validator} from './types';
3+
import {ColorType, Validator, FieldContextType} from './types';
54
// TODO: Fix this import after moving all TextField types to a single file after we move to the new docs
65
import {TextFieldProps} from './index';
76
import formValidators from './validators';

src/incubator/TextField/ValidationMessage.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
import React, {useContext} from 'react';
2-
import {TextStyle, StyleSheet} from 'react-native';
2+
import {StyleSheet} from 'react-native';
33
import Text from '../../components/text';
44
import FieldContext from './FieldContext';
55
import {getRelevantValidationMessage} from './Presenter';
6-
import {FieldStateProps} from './useFieldState';
7-
8-
export interface ValidationMessageProps {
9-
/**
10-
* Should support showing validation error message
11-
*/
12-
enableErrors?: boolean;
13-
/**
14-
* The validation message to display when field is invalid (depends on validate)
15-
*/
16-
validationMessage?: string | string[];
17-
/**
18-
* Custom style for the validation message
19-
*/
20-
validationMessageStyle?: TextStyle;
21-
retainSpace?: boolean;
22-
validate?: FieldStateProps['validate'];
23-
testID?: string;
24-
}
6+
import {ValidationMessageProps} from './types';
257

268
const ValidationMessage = ({
279
validationMessage,

src/incubator/TextField/index.tsx

Lines changed: 18 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,113 +5,22 @@
55
* 3. Passing typography preset that includes lineHeight usually cause alignment issues with
66
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
77
*/
8-
import React, {PropsWithChildren, ReactElement, useMemo} from 'react';
9-
import {ViewStyle, TextStyle, StyleProp} from 'react-native';
8+
import React, {useMemo} from 'react';
109
import {omit} from 'lodash';
11-
import {
12-
asBaseComponent,
13-
forwardRef,
14-
ForwardRefInjectedProps,
15-
BaseComponentInjectedProps,
16-
MarginModifiers,
17-
PaddingModifiers,
18-
TypographyModifiers,
19-
ColorsModifiers
20-
} from '../../commons/new';
10+
import {asBaseComponent, forwardRef} from '../../commons/new';
2111
import View from '../../components/view';
2212
import {Colors} from '../../style';
2313
import {useMeasure} from '../../hooks';
24-
import {ValidationMessagePosition, Validator} from './types';
14+
import {TextFieldProps, InternalTextFieldProps, ValidationMessagePosition, FieldContextType} from './types';
2515
import {shouldHidePlaceholder} from './Presenter';
26-
import Input, {InputProps} from './Input';
27-
import ValidationMessage, {ValidationMessageProps} from './ValidationMessage';
28-
import Label, {LabelProps} from './Label';
29-
import FieldContext, {FieldContextType as _FieldContextType} from './FieldContext';
30-
import useFieldState /* , FieldStateProps */ from './useFieldState';
16+
import Input from './Input';
17+
import ValidationMessage from './ValidationMessage';
18+
import Label from './Label';
19+
import FieldContext from './FieldContext';
20+
import useFieldState from './useFieldState';
3121
import usePreset from './usePreset';
32-
import FloatingPlaceholder, {FloatingPlaceholderProps} from './FloatingPlaceholder';
33-
import CharCounter, {CharCounterProps} from './CharCounter';
34-
35-
export type FieldContextType = _FieldContextType;
36-
export type TextFieldProps = MarginModifiers &
37-
PaddingModifiers &
38-
TypographyModifiers &
39-
ColorsModifiers &
40-
InputProps &
41-
LabelProps &
42-
Omit<FloatingPlaceholderProps, 'testID'> &
43-
// We're declaring these props explicitly here for react-docgen (which can't read hooks)
44-
// FieldStateProps &
45-
ValidationMessageProps &
46-
Omit<CharCounterProps, 'maxLength' | 'testID'> & {
47-
/**
48-
* Pass to render a leading element
49-
*/
50-
leadingAccessory?: ReactElement;
51-
/**
52-
* Pass to render a trailing element
53-
*/
54-
trailingAccessory?: ReactElement;
55-
/**
56-
* Pass to render a bottom element below the input
57-
*/
58-
bottomAccessory?: ReactElement;
59-
/**
60-
* Pass to add floating placeholder support
61-
*/
62-
floatingPlaceholder?: boolean;
63-
/**
64-
* Custom style for the floating placeholder
65-
*/
66-
floatingPlaceholderStyle?: TextStyle;
67-
/**
68-
* A single or multiple validator. Can be a string (required, email) or custom function.
69-
*/
70-
validate?: Validator | Validator[];
71-
/**
72-
* Should validate when the TextField mounts
73-
*/
74-
validateOnStart?: boolean;
75-
/**
76-
* Should validate when the TextField value changes
77-
*/
78-
validateOnChange?: boolean;
79-
/**
80-
* Should validate when losing focus of TextField
81-
*/
82-
validateOnBlur?: boolean;
83-
/**
84-
* Callback for when field validity has changed
85-
*/
86-
onChangeValidity?: (isValid: boolean) => void;
87-
/**
88-
* The position of the validation message (top/bottom)
89-
*/
90-
validationMessagePosition?: ValidationMessagePosition;
91-
/**
92-
* Internal style for the field container
93-
*/
94-
fieldStyle?: StyleProp<ViewStyle>;
95-
/**
96-
* Internal dynamic style callback for the field container
97-
*/
98-
dynamicFieldStyle?: (context: FieldContextType, props: {preset: TextFieldProps['preset']}) => StyleProp<ViewStyle>;
99-
/**
100-
* Container style of the whole component
101-
*/
102-
containerStyle?: ViewStyle;
103-
/**
104-
* Predefined preset to use for styling the field
105-
*/
106-
preset?: 'default' | null | string;
107-
};
108-
109-
export type InternalTextFieldProps = PropsWithChildren<
110-
TextFieldProps &
111-
// Omit<FieldStateInjectedProps, keyof InputProps> &
112-
BaseComponentInjectedProps &
113-
ForwardRefInjectedProps
114-
>;
22+
import FloatingPlaceholder from './FloatingPlaceholder';
23+
import CharCounter from './CharCounter';
11524

11625
interface StaticMembers {
11726
validationMessagePositions: typeof ValidationMessagePosition;
@@ -243,7 +152,13 @@ const TextField = (props: InternalTextFieldProps) => {
243152
/>
244153
)}
245154
{bottomAccessory}
246-
{showCharCounter && <CharCounter maxLength={others.maxLength} charCounterStyle={charCounterStyle} testID={`${props.testID}.charCounter`}/>}
155+
{showCharCounter && (
156+
<CharCounter
157+
maxLength={others.maxLength}
158+
charCounterStyle={charCounterStyle}
159+
testID={`${props.testID}.charCounter`}
160+
/>
161+
)}
247162
</View>
248163
</View>
249164
</FieldContext.Provider>
@@ -253,4 +168,5 @@ const TextField = (props: InternalTextFieldProps) => {
253168
TextField.displayName = 'Incubator.TextField';
254169
TextField.validationMessagePositions = ValidationMessagePosition;
255170

171+
export {TextFieldProps, FieldContextType};
256172
export default asBaseComponent<TextFieldProps, StaticMembers>(forwardRef(TextField as any));

0 commit comments

Comments
 (0)