Skip to content

Commit

Permalink
fix: adjust label width
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak committed Oct 16, 2023
1 parent 23e7fd8 commit 9f7fc67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/components/TextInput/Label/InputLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from 'react';
import { Animated, Dimensions, StyleSheet } from 'react-native';
import {
Animated,
ColorValue,
Platform,
StyleSheet,
useWindowDimensions,
} from 'react-native';

import AnimatedText from '../../Typography/AnimatedText';
import type { InputLabelProps } from '../types';
Expand Down Expand Up @@ -41,7 +47,7 @@ const InputLabel = (props: InputLabelProps) => {
testID,
} = props;

const { width } = Dimensions.get('window');
const { width } = useWindowDimensions();

const paddingOffset =
paddingLeft && paddingRight ? { paddingLeft, paddingRight } : {};
Expand Down Expand Up @@ -100,7 +106,20 @@ const InputLabel = (props: InputLabelProps) => {
],
};

const textColor = labelError && errorColor ? errorColor : placeholderColor;
const commonStyles = [
placeholderStyle,
{
top: topPosition,
},
labelStyle,
paddingOffset || {},
];

const textColor = (
labelError && errorColor ? errorColor : placeholderColor
) as ColorValue;

const maxWidth = width - (placeholderStyle.paddingHorizontal || 0);

return (
// Position colored placeholder and gray placeholder on top of each other and crossfade them
Expand All @@ -110,7 +129,8 @@ const InputLabel = (props: InputLabelProps) => {
style={[
StyleSheet.absoluteFill,
styles.labelContainer,
{ opacity, width },
Platform.OS !== 'web' && { width },
{ opacity },
labelTranslationX,
]}
>
Expand All @@ -132,12 +152,7 @@ const InputLabel = (props: InputLabelProps) => {
onLayout={onLayoutAnimatedText}
onTextLayout={onLabelTextLayout}
style={[
placeholderStyle,
{
top: topPosition,
},
labelStyle,
paddingOffset || {},
commonStyles,
{
color: activeColor,
},
Expand All @@ -151,15 +166,11 @@ const InputLabel = (props: InputLabelProps) => {
<AnimatedText
variant={focused ? 'bodyLarge' : 'bodySmall'}
style={[
placeholderStyle,
{
top: topPosition,
},
labelStyle,
paddingOffset,
commonStyles,
{
color: textColor,
opacity: placeholderOpacity,
maxWidth,
},
]}
numberOfLines={1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ exports[`correctly applies a component as the text label 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 16,
Expand Down Expand Up @@ -330,6 +331,7 @@ exports[`correctly applies cursorColor prop 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 16,
Expand Down Expand Up @@ -525,6 +527,7 @@ exports[`correctly applies default textAlign based on default RTL 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 16,
Expand Down Expand Up @@ -755,6 +758,7 @@ exports[`correctly applies height to multiline Outline TextInput 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 734,
"opacity": 0,
"paddingHorizontal": 16,
"position": "absolute",
Expand Down Expand Up @@ -949,6 +953,7 @@ exports[`correctly applies paddingLeft from contentStyleProp 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 16,
Expand Down Expand Up @@ -1146,6 +1151,7 @@ exports[`correctly applies textAlign center 1`] = `
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 16,
Expand Down Expand Up @@ -1341,6 +1347,7 @@ exports[`correctly renders left-side affix adornment, and right-side icon adornm
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 16,
"paddingRight": 56,
Expand Down Expand Up @@ -1738,6 +1745,7 @@ exports[`correctly renders left-side icon adornment, and right-side affix adornm
"left": 0,
"letterSpacing": 0.15,
"lineHeight": 19.2,
"maxWidth": 750,
"opacity": 0,
"paddingLeft": 56,
"paddingRight": 56,
Expand Down

0 comments on commit 9f7fc67

Please sign in to comment.