From 0fca19f7927bdca661b7fac62211d37788e52c4a Mon Sep 17 00:00:00 2001 From: guilherme-ferraz1 <62260157+guilherme-ferraz1@users.noreply.github.com> Date: Mon, 19 Dec 2022 05:40:42 -0300 Subject: [PATCH] feat: add customInputStyle to TextInput (#3509) --- example/src/Examples/TextInputExample.tsx | 14 ++ example/utils/index.ts | 1 + src/components/TextInput/Label/InputLabel.tsx | 2 + src/components/TextInput/TextInput.tsx | 9 + src/components/TextInput/TextInputFlat.tsx | 3 + .../TextInput/TextInputOutlined.tsx | 3 + src/components/TextInput/types.tsx | 4 + src/components/__tests__/TextInput.test.js | 13 ++ .../__snapshots__/TextInput.test.js.snap | 203 ++++++++++++++++++ 9 files changed, 252 insertions(+) diff --git a/example/src/Examples/TextInputExample.tsx b/example/src/Examples/TextInputExample.tsx index 1c4a08acaf..0c3f9eadf6 100644 --- a/example/src/Examples/TextInputExample.tsx +++ b/example/src/Examples/TextInputExample.tsx @@ -32,6 +32,7 @@ const initialState: State = { outlinedLargeText: '', outlinedTextPassword: '', nameNoPadding: '', + customStyleText: '', nameRequired: '', flatDenseText: '', flatDense: '', @@ -86,6 +87,7 @@ const TextInputExample = () => { outlinedLargeText, outlinedTextPassword, nameNoPadding, + customStyleText, nameRequired, flatDenseText, flatDense, @@ -531,6 +533,18 @@ const TextInputExample = () => { } /> + + inputActionHandler('customStyleText', customStyleText) + } + contentStyle={{ paddingLeft: 50 }} + /> + { labelTranslationXOffset, maxFontSizeMultiplier, testID, + contentStyle, } = props.labelProps; const labelTranslationX = { @@ -138,6 +139,7 @@ const InputLabel = (props: InputLabelProps) => { color: textColor, opacity: placeholderOpacity, }, + contentStyle, ]} numberOfLines={1} maxFontSizeMultiplier={maxFontSizeMultiplier} diff --git a/src/components/TextInput/TextInput.tsx b/src/components/TextInput/TextInput.tsx index ef35ea1538..5950cdafb2 100644 --- a/src/components/TextInput/TextInput.tsx +++ b/src/components/TextInput/TextInput.tsx @@ -142,6 +142,12 @@ export type Props = React.ComponentPropsWithRef & { * testID to be used on tests. */ testID?: string; + /** + * Pass custom style directly to the input itself. + * Overrides input style + * Example: `paddingLeft`, `backgroundColor` + */ + contentStyle?: StyleProp; /** * Pass style to override the default style of outlined wrapper. * Overrides style when mode is set to `outlined` @@ -223,6 +229,7 @@ const TextInput = React.forwardRef( error: errorProp = false, multiline = false, editable = true, + contentStyle, render = (props: RenderProps) => , ...rest }: Props, @@ -458,6 +465,7 @@ const TextInput = React.forwardRef( onLeftAffixLayoutChange={onLeftAffixLayoutChange} onRightAffixLayoutChange={onRightAffixLayoutChange} maxFontSizeMultiplier={maxFontSizeMultiplier} + contentStyle={contentStyle} /> ); } @@ -493,6 +501,7 @@ const TextInput = React.forwardRef( onLeftAffixLayoutChange={onLeftAffixLayoutChange} onRightAffixLayoutChange={onRightAffixLayoutChange} maxFontSizeMultiplier={maxFontSizeMultiplier} + contentStyle={contentStyle} /> ); } diff --git a/src/components/TextInput/TextInputFlat.tsx b/src/components/TextInput/TextInputFlat.tsx index 9a7fa1bf16..8d8fdd2d89 100644 --- a/src/components/TextInput/TextInputFlat.tsx +++ b/src/components/TextInput/TextInputFlat.tsx @@ -73,6 +73,7 @@ const TextInputFlat = ({ right, placeholderTextColor, testID = 'text-input-flat', + contentStyle, ...rest }: ChildTextInputProps) => { const isAndroid = Platform.OS === 'android'; @@ -274,6 +275,7 @@ const TextInputFlat = ({ roundness, maxFontSizeMultiplier: rest.maxFontSizeMultiplier, testID, + contentStyle, }; const affixTopPosition = { [AdornmentSide.Left]: leftAffixTopPosition, @@ -380,6 +382,7 @@ const TextInputFlat = ({ }, Platform.OS === 'web' && { outline: 'none' }, adornmentStyleAdjustmentForNativeInput, + contentStyle, ], })} diff --git a/src/components/TextInput/TextInputOutlined.tsx b/src/components/TextInput/TextInputOutlined.tsx index 6918d9d11f..ea312b5206 100644 --- a/src/components/TextInput/TextInputOutlined.tsx +++ b/src/components/TextInput/TextInputOutlined.tsx @@ -71,6 +71,7 @@ const TextInputOutlined = ({ right, placeholderTextColor, testID = 'text-input-outlined', + contentStyle, ...rest }: ChildTextInputProps) => { const adornmentConfig = getAdornmentConfig({ left, right }); @@ -209,6 +210,7 @@ const TextInputOutlined = ({ roundness, maxFontSizeMultiplier: rest.maxFontSizeMultiplier, testID, + contentStyle, }; const minHeight = (height || @@ -354,6 +356,7 @@ const TextInputOutlined = ({ }, Platform.OS === 'web' && { outline: 'none' }, adornmentStyleAdjustmentForNativeInput, + contentStyle, ], } as RenderProps)} diff --git a/src/components/TextInput/types.tsx b/src/components/TextInput/types.tsx index 6dcda11b91..b5b3ca66ea 100644 --- a/src/components/TextInput/types.tsx +++ b/src/components/TextInput/types.tsx @@ -4,6 +4,8 @@ import type { TextStyle, LayoutChangeEvent, ColorValue, + StyleProp, + ViewProps, } from 'react-native'; import type { $Omit } from './../../types'; @@ -38,6 +40,7 @@ export type State = { labelLayout: { measured: boolean; width: number; height: number }; leftLayout: { height: number | null; width: number | null }; rightLayout: { height: number | null; width: number | null }; + contentStyle?: StyleProp; }; export type ChildTextInputProps = { parentState: State; @@ -79,6 +82,7 @@ export type LabelProps = { roundness: number; maxFontSizeMultiplier?: number | undefined | null; testID?: string; + contentStyle?: StyleProp; }; export type InputLabelProps = { parentState: State; diff --git a/src/components/__tests__/TextInput.test.js b/src/components/__tests__/TextInput.test.js index 4d9dc6663c..2b6b1b4eee 100644 --- a/src/components/__tests__/TextInput.test.js +++ b/src/components/__tests__/TextInput.test.js @@ -181,6 +181,19 @@ it('correctly applies a component as the text label', () => { expect(toJSON()).toMatchSnapshot(); }); +it('correctly applies paddingLeft from contentStyleProp', () => { + const { toJSON } = render( + + ); + + expect(toJSON()).toMatchSnapshot(); +}); + it('renders label with correct color when active', () => { const { getByTestId } = render( `; + +exports[`correctly applies paddingLeft from contentStyleProp 1`] = ` + + + + + + With padding + + + With padding + + + + + +`; +