Skip to content

Commit

Permalink
[IOAPPX-426] Adjust SearchInput and TextInputBase spacing based o…
Browse files Browse the repository at this point in the history
…n the `fontScale` value (#356)

> [!caution]
> This PR depends on:
> * #348

## Short description
This PR enables dynamic size on the `SearchInput` and `TextInputBase`
components

## List of changes proposed in this pull request
- Add `allowFontScaling` prop to the icons contained in the both
components
- Add `allowScaleSpacing` prop to `VSpacer` and `HSpacer` components

## How to test
Go to the **Text Inputs** and **Search Input** screens in the example
app
  • Loading branch information
dmnplb authored Dec 17, 2024
1 parent ad30a4a commit 3798f98
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
15 changes: 10 additions & 5 deletions src/components/searchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "react-native";
import Animated, {
Easing,
Extrapolate,
Extrapolation,
interpolate,
interpolateColor,
useAnimatedStyle,
Expand Down Expand Up @@ -173,7 +173,7 @@ export const SearchInput = forwardRef<SearchInputRef, SearchInputProps>(
showCancelButton,
[0, 1],
[cancelButtonWidth + IOVisualCostants.appMarginDefault, 0],
Extrapolate.CLAMP
Extrapolation.CLAMP
)
}
],
Expand Down Expand Up @@ -235,7 +235,12 @@ export const SearchInput = forwardRef<SearchInputRef, SearchInputProps>(
pointerEvents={pressable ? "none" : "auto"}
>
<View style={styles.iconContainer}>
<Icon name="search" size={iconSize} color={iconColor} />
<Icon
allowFontScaling
name="search"
size={iconSize}
color={iconColor}
/>
</View>
<AnimatedTextInput
testID={testID}
Expand Down Expand Up @@ -328,15 +333,15 @@ const styles = StyleSheet.create({
inputFontSizePlaceholder,
"Titillio",
undefined,
"Regular"
"Medium"
)
},
placeholderLegacy: {
...makeFontStyleObject(
inputFontSizePlaceholder,
"TitilliumSansPro",
undefined,
"Regular"
"Semibold"
)
},
cancelButton: {
Expand Down
27 changes: 21 additions & 6 deletions src/components/spacer/Spacer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React from "react";
import React, { useMemo } from "react";
import { View } from "react-native";
import { hexToRgba, IOColors, IOSpacer } from "../../core";
import { useIOFontDynamicScale } from "../../utils/accessibility";

type BaseSpacerProps = {
orientation: "vertical" | "horizontal";
size: IOSpacer;
allowScaleSpacing?: boolean;
};

type SpacerProps = {
size?: IOSpacer;
allowScaleSpacing?: boolean;
};

const DEFAULT_SIZE: IOSpacer = 16;
Expand All @@ -22,20 +25,32 @@ Native `Spacer` component
@param {string} orientation
@param {IOSpacer} size
*/
const Spacer = ({ orientation, size }: BaseSpacerProps) => {
const style = React.useMemo(
const Spacer = ({ allowScaleSpacing, orientation, size }: BaseSpacerProps) => {
const { dynamicFontScale, spacingScaleMultiplier } = useIOFontDynamicScale();

const style = useMemo(
() => ({
...(orientation === "vertical" && {
height: size
height: allowScaleSpacing
? size * dynamicFontScale * spacingScaleMultiplier
: size
}),
...(orientation === "horizontal" && {
width: size
width: allowScaleSpacing
? size * dynamicFontScale * spacingScaleMultiplier
: size
}),
...((debugMode as boolean) && {
backgroundColor: debugBg
})
}),
[orientation, size]
[
allowScaleSpacing,
dynamicFontScale,
orientation,
size,
spacingScaleMultiplier
]
);

return <View style={style} />;
Expand Down
3 changes: 2 additions & 1 deletion src/components/textInput/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from "react";
import { ComponentProps } from "react";
import { TextInputBase } from "./TextInputBase";

type TextInputProps = Omit<
React.ComponentProps<typeof TextInputBase>,
ComponentProps<typeof TextInputBase>,
| "rightElement"
| "status"
| "bottomMessageColor"
Expand Down
55 changes: 44 additions & 11 deletions src/components/textInput/TextInputBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
useIOExperimentalDesign,
useIOTheme
} from "../../core";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { IOFontSize, makeFontStyleObject } from "../../utils/fonts";
import { RNTextInputProps, getInputPropsByType } from "../../utils/textInput";
import { InputType, WithTestID } from "../../utils/types";
Expand Down Expand Up @@ -78,8 +79,7 @@ const styles = StyleSheet.create({
flexDirection: "row",
alignItems: "center",
height: inputHeight,
paddingVertical: inputPaddingVertical,
paddingHorizontal: inputPaddingHorizontal
paddingVertical: inputPaddingVertical
},
textInputOuterBorder: {
...StyleSheet.absoluteFillObject,
Expand Down Expand Up @@ -107,7 +107,7 @@ const styles = StyleSheet.create({
...(Platform.OS === "android" && { marginLeft: -4 })
},
textInputStyleFont: {
...makeFontStyleObject(inputLabelFontSize, "Titillio", undefined, "Regular")
...makeFontStyleObject(inputLabelFontSize, "Titillio", undefined, "Medium")
},
// TODO: Remove this when legacy look is deprecated https://pagopa.atlassian.net/browse/IOPLT-153
textInputStyleLegacyFont: {
Expand All @@ -120,20 +120,23 @@ const styles = StyleSheet.create({
},
textInputLabelWrapper: {
position: "absolute",
paddingHorizontal: inputPaddingHorizontal,
zIndex: 10,
bottom: 0,
top: 0,
justifyContent: "center"
},
textInputLabel: {
color: inputLabelColor,
...makeFontStyleObject(inputLabelFontSize, "Titillio", undefined, "Regular")
},
textInputLabelLegacyFont: {
color: inputLabelColor,
...makeFontStyleObject(
inputLabelFontSize,
"TitilliumSansPro",
undefined,
"Regular"
),
color: inputLabelColor
)
}
});

Expand Down Expand Up @@ -227,6 +230,9 @@ export const TextInputBase = ({
disabled ? "disabled" : "initial"
);
const focusedState = useSharedValue<number>(0);

const { dynamicFontScale, spacingScaleMultiplier } = useIOFontDynamicScale();

const theme = useIOTheme();

/* Get the label width to enable the correct translation */
Expand Down Expand Up @@ -361,7 +367,11 @@ export const TextInputBase = ({
onPress={onTextInputPress}
style={[
inputStatus === "disabled" ? { opacity: inputDisabledOpacity } : {},
styles.textInput
styles.textInput,
{
paddingHorizontal:
inputPaddingHorizontal * dynamicFontScale * spacingScaleMultiplier
}
]}
accessible={false}
accessibilityRole={"none"}
Expand All @@ -384,8 +394,13 @@ export const TextInputBase = ({

{icon && (
<>
<Icon name={icon} color={iconColor} size={iconSize} />
<HSpacer size={iconMargin} />
<Icon
allowFontScaling
name={icon}
color={iconColor}
size={iconSize}
/>
<HSpacer allowScaleSpacing size={iconMargin} />
</>
)}
<TextInput
Expand Down Expand Up @@ -428,14 +443,32 @@ export const TextInputBase = ({
pointerEvents={"none"}
style={[
styles.textInputLabelWrapper,
icon ? { left: iconSize + iconMargin } : {}
{
paddingHorizontal:
inputPaddingHorizontal *
dynamicFontScale *
spacingScaleMultiplier
},
icon
? {
left:
(iconSize + iconMargin) *
dynamicFontScale *
spacingScaleMultiplier
}
: {}
]}
>
<Animated.Text
onLayout={getLabelWidth}
numberOfLines={1}
accessible={false}
style={[styles.textInputLabel, animatedLabelStyle]}
style={[
isExperimental
? styles.textInputLabel
: styles.textInputLabelLegacyFont,
animatedLabelStyle
]}
>
{placeholder}
</Animated.Text>
Expand Down

0 comments on commit 3798f98

Please sign in to comment.