Skip to content

Commit 1f9478f

Browse files
chore(text fields): refactored use text input colors hook
1 parent 21dc332 commit 1f9478f

File tree

7 files changed

+79
-68
lines changed

7 files changed

+79
-68
lines changed

src/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ export {SuggestionChip, type SuggestionChipProps} from './chips/suggestion-chip/
1616
export {PrimaryTabs, type PrimaryTabsProps} from './navigation/tabs/primary-tabs/PrimaryTabs.component';
1717
export {SecondaryTabs, type SecondaryTabsProps} from './navigation/tabs/secondary-tabs/SecondaryTabs.component';
1818

19-
export {type TextInputProps} from './text-inputs/text-input.type';
20-
export {FilledTextInput} from './text-inputs/filled-text-input/FilledTextInput.component';
21-
export {OutlinedTextInput} from './text-inputs/outlined-text-input/OutlinedTextInput.component';
19+
export {FilledTextInput, type FilledTextInputProps} from './text-inputs/filled-text-input/FilledTextInput.component';
20+
export {OutlinedTextInput, type OutlinedTextInputProps} from './text-inputs/outlined-text-input/OutlinedTextInput.component';
2221

2322
export {
2423
CenterAlignedTopAppBar,

src/text-inputs/filled-text-input/FilledTextInput.component.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, {forwardRef, useRef} from 'react';
2-
import {Text, View, TextInput, TouchableWithoutFeedback} from 'react-native';
32
import Animated, {useAnimatedStyle, interpolateColor, interpolate} from 'react-native-reanimated';
3+
import {Text, View, TextInput, TouchableWithoutFeedback, type StyleProp, type ViewStyle} from 'react-native';
44

55
import {ErrorIcon} from '../../icons';
66
import {styles} from './filled-text-input.styles';
77
import {type IconProps} from '../../icons/icon-props';
8-
import type {TextInputProps} from '../text-input.type';
8+
import type {TextInputProps} from '../text-input.types';
99
import {useTextInputColors} from '../use-text-input-colors.hook';
1010
import {useTextInputFocus} from '../use-text-input-focus-anim.hook';
1111
import {useTypography} from '../../typography/useTypography.component';
@@ -16,6 +16,10 @@ const DEFAULT_LABEL_LARGE_FONT_SIZE = 16;
1616
const ACTIVE_INDICATOR_FOCUSED_HEIGHT = 3;
1717
const ACTIVE_INDICATOR_UNFOCUSED_HEIGHT = 1;
1818

19+
export interface FilledTextInputProps<T> extends TextInputProps<T> {
20+
activeIndicatorStyle?: StyleProp<ViewStyle>;
21+
}
22+
1923
export const FilledTextInput = forwardRef(
2024
<T extends IconProps>(
2125
{
@@ -45,7 +49,7 @@ export const FilledTextInput = forwardRef(
4549
onBlur,
4650
onOuterContainerLayout,
4751
...props
48-
}: TextInputProps<T>,
52+
}: FilledTextInputProps<T>,
4953
ref: React.Ref<TextInput>
5054
) => {
5155
const {bodyLarge, bodySmall} = useTypography();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {type ColorValue} from 'react-native';
2+
3+
import {type Theme} from '../theme/theme.types';
4+
import {TextInputColors} from './text-input.types';
5+
6+
export const getTextInputActiveColors = (theme: Theme, primaryColorBasedOnError: ColorValue, surfaceVariantColorBasedOnError: ColorValue) => ({
7+
[TextInputColors.VALUE_COLOR]: theme.surface.text,
8+
[TextInputColors.CONTAINER_COLOR]: theme.surfaceContainer.backgroundHighest,
9+
[TextInputColors.SELECTION_COLOR]: primaryColorBasedOnError,
10+
[TextInputColors.PLACEHOLDER_COLOR]: theme.surface.textVariant,
11+
[TextInputColors.LABEL_FOCUSED_COLOR]: primaryColorBasedOnError,
12+
[TextInputColors.TRAILING_ICON_COLOR]: surfaceVariantColorBasedOnError,
13+
[TextInputColors.LEADING_ICON_COLOR]: theme.surface.textVariant,
14+
[TextInputColors.LABEL_UNFOCUSED_COLOR]: surfaceVariantColorBasedOnError,
15+
[TextInputColors.SUPPORING_TEXT_COLOR]: surfaceVariantColorBasedOnError,
16+
[TextInputColors.ACTIVE_INDICATOR_FOCUSED_COLOR]: primaryColorBasedOnError,
17+
[TextInputColors.ACTIVE_INDICATOR_UNFOCUSED_COLOR]: surfaceVariantColorBasedOnError,
18+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {type ColorValue} from 'react-native';
2+
3+
import {TextInputColors} from './text-input.types';
4+
5+
export const getTextInputDisabledColors = (disabledOnContainerColor: ColorValue, disabledContaienerColor: ColorValue) => {
6+
const colors = Object.values(TextInputColors).reduce((acc, key) => {
7+
acc[key] = disabledOnContainerColor;
8+
9+
return acc;
10+
}, {} as Record<TextInputColors, ColorValue>);
11+
12+
colors[TextInputColors.CONTAINER_COLOR] = disabledContaienerColor;
13+
14+
return colors;
15+
};

src/text-inputs/outlined-text-input/OutlinedTextInput.component.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, {forwardRef, useCallback, useRef, useState} from 'react';
22
import Animated, {useAnimatedStyle, interpolateColor, interpolate} from 'react-native-reanimated';
3-
import {Text, View, TextInput, TouchableWithoutFeedback, type LayoutChangeEvent} from 'react-native';
3+
import {Text, View, TextInput, TouchableWithoutFeedback, type StyleProp, type ViewStyle, type LayoutChangeEvent} from 'react-native';
44

55
import {ErrorIcon} from '../../icons';
66
import {useTheme} from '../../theme/useTheme.hook';
77
import {type IconProps} from '../../icons/icon-props';
8-
import {type TextInputProps} from '../text-input.type';
8+
import {type TextInputProps} from '../text-input.types';
99
import {useTextInputColors} from '../use-text-input-colors.hook';
1010
import {useTextInputFocus} from '../use-text-input-focus-anim.hook';
1111
import {useTypography} from '../../typography/useTypography.component';
@@ -19,6 +19,10 @@ const DEFAULT_LABEL_LARGE_FONT_SIZE = 16;
1919
const DEFAULT_LABEL_SMALL_LINE_HEIGHT = 15;
2020
const FOCUSED_LABEL_SLOT_PADDING_HORIZONTAL = 4;
2121

22+
export interface OutlinedTextInputProps<T> extends TextInputProps<T> {
23+
labelSlotStyle?: StyleProp<ViewStyle>;
24+
}
25+
2226
export const OutlinedTextInput = forwardRef(
2327
<T extends IconProps>(
2428
{
@@ -39,6 +43,7 @@ export const OutlinedTextInput = forwardRef(
3943

4044
style,
4145
labelStyle,
46+
labelSlotStyle,
4247
outerContainerStyle,
4348
innerContainerStyle,
4449
supportingTextStyle,
@@ -47,7 +52,7 @@ export const OutlinedTextInput = forwardRef(
4752
onBlur,
4853
onOuterContainerLayout,
4954
...props
50-
}: Omit<TextInputProps<T>, 'activeIndicatorStyle'>,
55+
}: OutlinedTextInputProps<T>,
5156
ref: React.Ref<TextInput>
5257
) => {
5358
const [labelWidth, setLabeWidth] = useState(0);
@@ -142,7 +147,7 @@ export const OutlinedTextInput = forwardRef(
142147
<Animated.View style={[styles.container, containerAnimatedStyles, innerContainerStyle]}>
143148
{leadingComponent}
144149
{LeadingIcon ? <LeadingIcon color={leadingIconColor} style={styles.leadingIcon} {...leadingIconProps} /> : null}
145-
<Animated.View style={[styles.labelSlot, {backgroundColor: surfaceContainer.backgroundLow}, focusedLabelSlot]} />
150+
<Animated.View style={[styles.labelSlot, {backgroundColor: surfaceContainer.backgroundLow}, focusedLabelSlot, labelSlotStyle]} />
146151
<View onLayout={getLabelDistanceToContainerStart} style={[styles.inputWithLabelContainer]}>
147152
<Animated.Text onLayout={getLabelWidth} style={[styles.label, bodyLarge, labelAnimatedStyles, labelStyle]}>
148153
{label}
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ export interface TextInputProps<T> extends RNTextInputProps {
2020
supportingTextStyle?: StyleProp<TextStyle>;
2121
innerContainerStyle?: StyleProp<ViewStyle>;
2222
outerContainerStyle?: StyleProp<ViewStyle>;
23-
activeIndicatorStyle?: StyleProp<ViewStyle>;
2423

2524
onOuterContainerLayout?: (e: LayoutChangeEvent) => void;
2625
}
26+
27+
export enum TextInputColors {
28+
VALUE_COLOR = 'valueColor',
29+
CONTAINER_COLOR = 'containerColor',
30+
SELECTION_COLOR = 'selectionColor',
31+
PLACEHOLDER_COLOR = 'placeholderColor',
32+
LABEL_FOCUSED_COLOR = 'labelFocusedColor',
33+
LABEL_COLOR = 'labelFocusedColor',
34+
TRAILING_ICON_COLOR = 'trailingIconColor',
35+
LEADING_ICON_COLOR = 'leadingIconColor',
36+
LABEL_UNFOCUSED_COLOR = 'labelUnfocusedColor',
37+
SUPPORING_TEXT_COLOR = 'supportingTextColor',
38+
ACTIVE_INDICATOR_FOCUSED_COLOR = 'activeIndicatorFocusedColor',
39+
ACTIVE_INDICATOR_UNFOCUSED_COLOR = 'activeIndicatorUnfocusedColor',
40+
}
Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,34 @@
11
import {useMemo} from 'react';
2+
23
import {useTheme} from '../theme/useTheme.hook';
34
import {convertToRGBA} from '../utils/convert-to-rgba';
5+
import {getTextInputActiveColors} from './get-text-input-active-colors';
6+
import {getTextInputDisabledColors} from './get-text-input-disabled-colors';
47

58
interface UseTextInputColorsParams {
69
isError: boolean;
710
disabled: boolean;
811
}
912

1013
export const useTextInputColors = ({disabled, isError}: UseTextInputColorsParams) => {
11-
const {surface, surfaceContainer, error, primary} = useTheme();
14+
const theme = useTheme();
1215

13-
const primaryColorBasedOnError = isError ? error.background : primary.background;
14-
const surfaceVariantColorBasedOnError = isError ? error.background : surface.textVariant;
16+
const [primaryColorBasedOnError, surfaceVariantColorBasedOnError] = isError
17+
? [theme.error.background, theme.error.background]
18+
: [theme.primary.background, theme.surface.textVariant];
1519

1620
const [disabledOnSurfaceColor, disabledSurfaceContaienerHighestColor] = useMemo(
17-
() => [convertToRGBA(surface.text as string, 0.38), convertToRGBA(surfaceContainer.backgroundHighest as string, 0.38)],
18-
[surface, surfaceContainer]
21+
() => [convertToRGBA(theme.surface.text as string, 0.38), convertToRGBA(theme.surfaceContainer.backgroundHighest as string, 0.38)],
22+
[theme]
1923
);
2024

21-
const {
22-
valueColor,
23-
containerColor,
24-
selectionColor,
25-
placeholderColor,
26-
labelFocusedColor,
27-
trailingIconColor,
28-
leadingIconColor,
29-
labelUnfocusedColor,
30-
supportingTextColor,
31-
activeIndicatorFocusedColor,
32-
activeIndicatorUnfocusedColor,
33-
} = useMemo(
25+
const colors = useMemo(
3426
() =>
3527
disabled
36-
? {
37-
valueColor: disabledOnSurfaceColor,
38-
containerColor: disabledSurfaceContaienerHighestColor,
39-
selectionColor: disabledOnSurfaceColor,
40-
placeholderColor: disabledOnSurfaceColor,
41-
labelFocusedColor: disabledOnSurfaceColor,
42-
trailingIconColor: disabledOnSurfaceColor,
43-
leadingIconColor: disabledOnSurfaceColor,
44-
labelUnfocusedColor: disabledOnSurfaceColor,
45-
supportingTextColor: disabledOnSurfaceColor,
46-
activeIndicatorFocusedColor: disabledOnSurfaceColor,
47-
activeIndicatorUnfocusedColor: disabledOnSurfaceColor,
48-
}
49-
: {
50-
valueColor: surface.text,
51-
containerColor: surfaceContainer.backgroundHighest,
52-
selectionColor: primaryColorBasedOnError,
53-
placeholderColor: surface.textVariant,
54-
labelFocusedColor: primaryColorBasedOnError,
55-
trailingIconColor: surfaceVariantColorBasedOnError,
56-
leadingIconColor: surface.textVariant,
57-
labelUnfocusedColor: surfaceVariantColorBasedOnError,
58-
supportingTextColor: surfaceVariantColorBasedOnError,
59-
activeIndicatorFocusedColor: primaryColorBasedOnError,
60-
activeIndicatorUnfocusedColor: surfaceVariantColorBasedOnError,
61-
},
28+
? getTextInputDisabledColors(disabledOnSurfaceColor, disabledSurfaceContaienerHighestColor)
29+
: getTextInputActiveColors(theme, primaryColorBasedOnError, surfaceVariantColorBasedOnError),
6230
[disabled, disabledOnSurfaceColor, disabledSurfaceContaienerHighestColor, primaryColorBasedOnError, surfaceVariantColorBasedOnError]
6331
);
6432

65-
return {
66-
valueColor,
67-
containerColor,
68-
selectionColor,
69-
placeholderColor,
70-
labelFocusedColor,
71-
trailingIconColor,
72-
leadingIconColor,
73-
labelUnfocusedColor,
74-
supportingTextColor,
75-
activeIndicatorFocusedColor,
76-
activeIndicatorUnfocusedColor,
77-
};
33+
return colors;
7834
};

0 commit comments

Comments
 (0)