Skip to content

Commit ad2d8a0

Browse files
authored
Support dynamicFieldStyle for Incubator.TextField (#1682)
1 parent da56dca commit ad2d8a0

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export default class TextFieldScreen extends Component {
214214
label="Label"
215215
placeholder="Enter text..."
216216
preset={preset}
217-
fieldStyle={(_state, {preset}) => (preset === 'withUnderline' ? styles.withUnderline : styles.withFrame)}
217+
dynamicFieldStyle={(_state, {preset}) => (preset === 'withUnderline' ? styles.withUnderline : styles.withFrame)}
218218
editable={!shouldDisable}
219219
/>
220220

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
77
*/
88
import React, { PropsWithChildren, ReactElement } from 'react';
9-
import { ViewStyle, TextStyle } from 'react-native';
9+
import { ViewStyle, TextStyle, StyleProp } from 'react-native';
1010
import { ForwardRefInjectedProps, BaseComponentInjectedProps, MarginModifiers, PaddingModifiers, TypographyModifiers, ColorsModifiers } from '../../commons/new';
1111
import { ValidationMessagePosition, Validator } from './types';
1212
import { InputProps } from './Input';
@@ -60,9 +60,13 @@ export declare type TextFieldProps = MarginModifiers & PaddingModifiers & Typogr
6060
/**
6161
* Internal style for the field container
6262
*/
63-
fieldStyle?: ViewStyle | ((context: FieldContextType, props: {
63+
fieldStyle?: StyleProp<ViewStyle>;
64+
/**
65+
* Internal dynamic style callback for the field container
66+
*/
67+
dynamicFieldStyle?: (context: FieldContextType, props: {
6468
preset: TextFieldProps['preset'];
65-
}) => ViewStyle);
69+
}) => StyleProp<ViewStyle>;
6670
/**
6771
* Container style of the whole component
6872
*/

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
159159
validateOnChange?: boolean | undefined;
160160
validateOnBlur?: boolean | undefined;
161161
onChangeValidity?: ((isValid: boolean) => void) | undefined;
162-
fieldStyle?: import("react-native").ViewStyle | ((context: import("./FieldContext").FieldContextType, props: {
162+
fieldStyle?: import("react-native").StyleProp<import("react-native").ViewStyle>;
163+
dynamicFieldStyle?: ((context: import("./FieldContext").FieldContextType, props: {
163164
preset: string | null | undefined;
164-
}) => import("react-native").ViewStyle) | undefined;
165+
}) => import("react-native").StyleProp<import("react-native").ViewStyle>) | undefined;
165166
containerStyle?: import("react-native").ViewStyle | undefined;
166167
modifiers: import("../../commons/modifiers").ExtractedStyle;
167168
forwardedRef: any;
@@ -487,9 +488,10 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
487488
validateOnChange?: boolean | undefined;
488489
validateOnBlur?: boolean | undefined;
489490
onChangeValidity?: ((isValid: boolean) => void) | undefined;
490-
fieldStyle?: import("react-native").ViewStyle | ((context: import("./FieldContext").FieldContextType, props: {
491+
fieldStyle?: import("react-native").StyleProp<import("react-native").ViewStyle>;
492+
dynamicFieldStyle?: ((context: import("./FieldContext").FieldContextType, props: {
491493
preset: string | null | undefined;
492-
}) => import("react-native").ViewStyle) | undefined;
494+
}) => import("react-native").StyleProp<import("react-native").ViewStyle>) | undefined;
493495
containerStyle?: import("react-native").ViewStyle | undefined;
494496
modifiers: import("../../commons/modifiers").ExtractedStyle;
495497
forwardedRef: any;
@@ -1047,13 +1049,14 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
10471049
validateOnChange?: boolean | undefined;
10481050
validateOnBlur: boolean;
10491051
onChangeValidity?: ((isValid: boolean) => void) | undefined;
1050-
fieldStyle: import("react-native").ViewStyle | {
1052+
fieldStyle: false | import("react-native").ViewStyle | import("react-native").RegisteredStyle<import("react-native").ViewStyle> | import("react-native").RecursiveArray<import("react-native").ViewStyle | import("react-native").Falsy | import("react-native").RegisteredStyle<import("react-native").ViewStyle>> | {
10511053
borderBottomWidth: number;
10521054
borderBottomColor: string;
10531055
paddingBottom: number;
1054-
} | ((context: import("./FieldContext").FieldContextType, props: {
1056+
} | null;
1057+
dynamicFieldStyle?: ((context: import("./FieldContext").FieldContextType, props: {
10551058
preset: string | null | undefined;
1056-
}) => import("react-native").ViewStyle);
1059+
}) => import("react-native").StyleProp<import("react-native").ViewStyle>) | undefined;
10571060
containerStyle?: import("react-native").ViewStyle | undefined;
10581061
modifiers: import("../../commons/modifiers").ExtractedStyle;
10591062
forwardedRef: any;

src/incubator/TextField/index.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
77
*/
88
import React, {PropsWithChildren, ReactElement, useMemo} from 'react';
9-
import {ViewStyle, TextStyle} from 'react-native';
10-
import {omit, isFunction} from 'lodash';
9+
import {ViewStyle, TextStyle, StyleProp} from 'react-native';
10+
import {omit} from 'lodash';
1111
import {
1212
asBaseComponent,
1313
forwardRef,
@@ -86,7 +86,11 @@ export type TextFieldProps = MarginModifiers &
8686
/**
8787
* Internal style for the field container
8888
*/
89-
fieldStyle?: ViewStyle | ((context: FieldContextType, props: {preset: TextFieldProps['preset']}) => ViewStyle);
89+
fieldStyle?: StyleProp<ViewStyle>;
90+
/**
91+
* Internal dynamic style callback for the field container
92+
*/
93+
dynamicFieldStyle?: (context: FieldContextType, props: {preset: TextFieldProps['preset']}) => StyleProp<ViewStyle>;
9094
/**
9195
* Container style of the whole component
9296
*/
@@ -120,6 +124,7 @@ const TextField = (props: InternalTextFieldProps) => {
120124
modifiers,
121125
// General
122126
fieldStyle: fieldStyleProp,
127+
dynamicFieldStyle,
123128
containerStyle,
124129
floatingPlaceholder,
125130
floatingPlaceholderColor,
@@ -156,7 +161,7 @@ const TextField = (props: InternalTextFieldProps) => {
156161
const typographyStyle = useMemo(() => omit(typography, 'lineHeight'), [typography]);
157162
const colorStyle = useMemo(() => color && {color}, [color]);
158163

159-
const fieldStyle = isFunction(fieldStyleProp) ? fieldStyleProp(context, {preset: props.preset}) : fieldStyleProp;
164+
const fieldStyle = [fieldStyleProp, dynamicFieldStyle?.(context, {preset: props.preset})];
160165
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);
161166

162167
return (

0 commit comments

Comments
 (0)