Skip to content

Commit 6b0800d

Browse files
authored
Support passing custom preset and use it in fieldStyle callback (#1633)
1 parent 195b93b commit 6b0800d

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export default class TextFieldScreen extends Component {
1515
errorPosition: TextField.validationMessagePositions.TOP,
1616
shouldDisable: false,
1717
value: 'Initial Value',
18-
searching: false
18+
searching: false,
19+
preset: 'withUnderline'
1920
};
2021

2122
componentDidMount() {
@@ -49,7 +50,7 @@ export default class TextFieldScreen extends Component {
4950
}
5051

5152
render() {
52-
const {errorPosition, shouldDisable, price} = this.state;
53+
const {errorPosition, shouldDisable, price, preset} = this.state;
5354
return (
5455
<ScrollView keyboardShouldPersistTaps="always">
5556
<View flex padding-page>
@@ -198,6 +199,25 @@ export default class TextFieldScreen extends Component {
198199
editable={!shouldDisable}
199200
/>
200201

202+
<View row spread centerV>
203+
<Text h3 blue50 marginV-s4>
204+
Custom Field Style
205+
</Text>
206+
<Button
207+
label={preset}
208+
onPress={() => this.setState({preset: preset === 'withUnderline' ? 'withFrame' : 'withUnderline'})}
209+
size={Button.sizes.xSmall}
210+
/>
211+
</View>
212+
213+
<TextField
214+
label="Label"
215+
placeholder="Enter text..."
216+
preset={preset}
217+
fieldStyle={(_state, {preset}) => (preset === 'withUnderline' ? styles.withUnderline : styles.withFrame)}
218+
editable={!shouldDisable}
219+
/>
220+
201221
<Text h3 blue50 marginV-s4>
202222
Char Counter
203223
</Text>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ export declare type TextFieldProps = MarginModifiers & PaddingModifiers & Typogr
6060
/**
6161
* Internal style for the field container
6262
*/
63-
fieldStyle?: ViewStyle | ((context: FieldContextType) => ViewStyle);
63+
fieldStyle?: ViewStyle | ((context: FieldContextType, props: {
64+
preset: TextFieldProps['preset'];
65+
}) => ViewStyle);
6466
/**
6567
* Container style of the whole component
6668
*/
6769
containerStyle?: ViewStyle;
6870
/**
6971
* Predefined preset to use for styling the field
7072
*/
71-
preset?: 'default' | null;
73+
preset?: 'default' | null | string;
7274
};
7375
export declare type InternalTextFieldProps = PropsWithChildren<TextFieldProps & BaseComponentInjectedProps & ForwardRefInjectedProps>;
7476
interface StaticMembers {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ 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) => import("react-native").ViewStyle) | undefined;
162+
fieldStyle?: import("react-native").ViewStyle | ((context: import("./FieldContext").FieldContextType, props: {
163+
preset: string | null | undefined;
164+
}) => import("react-native").ViewStyle) | undefined;
163165
containerStyle?: import("react-native").ViewStyle | undefined;
164166
modifiers: import("../../commons/modifiers").ExtractedStyle;
165167
forwardedRef: any;
@@ -485,7 +487,9 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
485487
validateOnChange?: boolean | undefined;
486488
validateOnBlur?: boolean | undefined;
487489
onChangeValidity?: ((isValid: boolean) => void) | undefined;
488-
fieldStyle?: import("react-native").ViewStyle | ((context: import("./FieldContext").FieldContextType) => import("react-native").ViewStyle) | undefined;
490+
fieldStyle?: import("react-native").ViewStyle | ((context: import("./FieldContext").FieldContextType, props: {
491+
preset: string | null | undefined;
492+
}) => import("react-native").ViewStyle) | undefined;
489493
containerStyle?: import("react-native").ViewStyle | undefined;
490494
modifiers: import("../../commons/modifiers").ExtractedStyle;
491495
forwardedRef: any;
@@ -1047,7 +1051,9 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
10471051
borderBottomWidth: number;
10481052
borderBottomColor: string;
10491053
paddingBottom: number;
1050-
} | ((context: import("./FieldContext").FieldContextType) => import("react-native").ViewStyle);
1054+
} | ((context: import("./FieldContext").FieldContextType, props: {
1055+
preset: string | null | undefined;
1056+
}) => import("react-native").ViewStyle);
10511057
containerStyle?: import("react-native").ViewStyle | undefined;
10521058
modifiers: import("../../commons/modifiers").ExtractedStyle;
10531059
forwardedRef: any;

src/incubator/TextField/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ export type TextFieldProps = MarginModifiers &
8484
/**
8585
* Internal style for the field container
8686
*/
87-
fieldStyle?: ViewStyle | ((context: FieldContextType) => ViewStyle);
87+
fieldStyle?: ViewStyle | ((context: FieldContextType, props: {preset: TextFieldProps['preset']}) => ViewStyle);
8888
/**
8989
* Container style of the whole component
9090
*/
9191
containerStyle?: ViewStyle;
9292
/**
9393
* Predefined preset to use for styling the field
9494
*/
95-
preset?: 'default' | null;
95+
preset?: 'default' | null | string;
9696
};
9797

9898
export type InternalTextFieldProps = PropsWithChildren<
@@ -154,7 +154,7 @@ const TextField = (props: InternalTextFieldProps) => {
154154
const typographyStyle = useMemo(() => omit(typography, 'lineHeight'), [typography]);
155155
const colorStyle = useMemo(() => color && {color}, [color]);
156156

157-
const fieldStyle = isFunction(fieldStyleProp) ? fieldStyleProp(context) : fieldStyleProp;
157+
const fieldStyle = isFunction(fieldStyleProp) ? fieldStyleProp(context, {preset: props.preset}) : fieldStyleProp;
158158

159159
return (
160160
<FieldContext.Provider value={context}>

0 commit comments

Comments
 (0)