Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 114 additions & 82 deletions src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type { TextInputProps as NativeTextInputProps } from 'react-native';
import type {
AccessibilityProps,
PressableStateCallbackType,
BlurEvent,
ColorValue,
FocusEvent,
Expand All @@ -23,8 +24,12 @@ import { useTextInput } from './hooks';
import { styles } from './styles';
import TextInputErrorIcon from './TextInputErrorIcon';
import type { TextInputAccessoryProps } from './TextInputIcon';
import { mergeAccessibilityText } from './utils';
import type { InternalTheme, ThemeProp } from '../../theme/types';

// React Native Web adds `hovered` to the Pressable state; React Native omits it.
type PressableState = PressableStateCallbackType & { hovered?: boolean };

export type TextInputAnimationState = {
animatedLabelWrapperStyle: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
animatedLabelTextStyle: StyleProp<AnimatedStyle<StyleProp<TextStyle>>>;
Expand Down Expand Up @@ -57,9 +62,12 @@ export type TextInputColors = {
};

export type GetAccessibilityDataReturn = {
input: AccessibilityProps & { 'aria-invalid'?: boolean };
supportingText: AccessibilityProps;
counter: AccessibilityProps;
input: AccessibilityProps & {
'aria-invalid'?: boolean;
'aria-describedby'?: string;
};
supportingText: AccessibilityProps & { nativeID: string };
counter: AccessibilityProps & { nativeID: string };
};

export type GetAccessibilityDataProps = {
Expand All @@ -68,6 +76,8 @@ export type GetAccessibilityDataProps = {
hasError: boolean;
hasCounter: boolean;
isDisabled: boolean;
supportingTextId: string;
counterId: string;
};

export type TextInputVariant = 'filled' | 'outlined';
Expand Down Expand Up @@ -107,6 +117,7 @@ export type FilledTextInputHookData = SharedTextInputStyleData & {
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: StyleProp<ViewStyle> | undefined;
outlineStyles: StyleProp<ViewStyle>;
hoveredOutlineStyles: StyleProp<ViewStyle> | undefined;
animatedActiveOutlineStyles: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
inputStyles: StyleProp<TextStyle>;
};
Expand All @@ -121,6 +132,7 @@ export type OutlinedTextInputHookData = SharedTextInputStyleData & {
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: undefined;
outlineStyles: StyleProp<ViewStyle>;
hoveredOutlineStyles: undefined;
inputStyles: StyleProp<TextStyle>;
};

Expand Down Expand Up @@ -153,6 +165,7 @@ export type TextInputHookReturn = SharedTextInputStyleData & {
fieldStyles: StyleProp<ViewStyle>;
disabledBackgroundStyles: StyleProp<ViewStyle> | undefined;
outlineStyles: StyleProp<ViewStyle>;
hoveredOutlineStyles: StyleProp<ViewStyle> | undefined;
inputStyles: StyleProp<TextStyle>;
placeholder: string | undefined;
counterText: string;
Expand Down Expand Up @@ -321,6 +334,7 @@ function TextInput({
fieldStyles,
disabledBackgroundStyles,
outlineStyles,
hoveredOutlineStyles,
animatedActiveOutlineStyles,
animatedLabelWrapperStyles,
animatedLabelTextStyles,
Expand Down Expand Up @@ -361,92 +375,110 @@ function TextInput({

return (
<Pressable onPress={focusInput} accessible={false} role="none">
<View style={fieldStyles}>
{/* Disabled tint overlay — filled variant only. A childless
{({ hovered }: PressableState) => (
<>
<View style={fieldStyles}>
{/* Disabled tint overlay — filled variant only. A childless
absolutely-positioned View whose translucent fill is applied via the
`opacity` style, so it never affects label/input rendering and works
with PlatformColor on Android. */}
{!!disabledBackgroundStyles && (
<View style={disabledBackgroundStyles} />
)}
{!!disabledBackgroundStyles && (
<View style={disabledBackgroundStyles} />
)}

{/* Inactive indicator — always-visible 1px bottom border (filled) or
{/* Inactive indicator — always-visible 1px bottom border (filled) or
full border (outlined); height and color reflect error/disabled state
but do not change on focus */}
<View style={outlineStyles} />

{/* Active indicator — filled variant only; 2px bar that expands from
<View
style={
hovered && hoveredOutlineStyles
? hoveredOutlineStyles
: outlineStyles
}
/>

{/* Active indicator — filled variant only; 2px bar that expands from
the center outward via scaleX (0 → 1) on focus and collapses on blur */}
{!!animatedActiveOutlineStyles && (
<Animated.View style={animatedActiveOutlineStyles} />
)}

{!!label && (
<Animated.View aria-hidden style={animatedLabelWrapperStyles}>
<Animated.Text style={animatedLabelTextStyles}>
{label}
</Animated.Text>
</Animated.View>
)}

{renderLeadingAccessory
? renderLeadingAccessory({
style: leadingAccessoryStyles,
error: hasError,
disabled: isDisabled,
multiline: !!rest.multiline,
})
: null}

<Animated.View style={[containerStyles, animatedContainerStyle]}>
{hasPrefix && <Text style={prefixStyles}>{prefix}</Text>}

{render({
ref: input,
selectionColor,
cursorColor,
placeholderTextColor,
...accessibilityProps.input,
...rest,
editable: isEditable,
placeholder,
style: inputStyles,
onChangeText,
onFocus,
onBlur,
})}

{hasSuffix && <Text style={suffixStyles}>{suffix}</Text>}
</Animated.View>

{renderTrailingAccessory ? (
renderTrailingAccessory({
style: trailingAccessoryStyles,
error: hasError,
disabled: isDisabled,
multiline: !!rest.multiline,
})
) : hasError ? (
<TextInputErrorIcon style={trailingAccessoryStyles} theme={theme} />
) : null}
</View>

<View style={styles.addendum}>
{!!supportingText && (
<Text
{...accessibilityProps.supportingText}
style={supportingTextStyles}
>
{supportingText}
</Text>
)}

{hasCounter && (
<Text {...accessibilityProps.counter} style={counterStyles}>
{counterText}
</Text>
)}
</View>
{!!animatedActiveOutlineStyles && (
<Animated.View style={animatedActiveOutlineStyles} />
)}

{!!label && (
<Animated.View aria-hidden style={animatedLabelWrapperStyles}>
<Animated.Text style={animatedLabelTextStyles}>
{label}
</Animated.Text>
</Animated.View>
)}

{renderLeadingAccessory
? renderLeadingAccessory({
style: leadingAccessoryStyles,
error: hasError,
disabled: isDisabled,
multiline: !!rest.multiline,
})
: null}

<Animated.View style={[containerStyles, animatedContainerStyle]}>
{hasPrefix && <Text style={prefixStyles}>{prefix}</Text>}

{render({
ref: input,
selectionColor,
cursorColor,
placeholderTextColor,
...accessibilityProps.input,
...rest,
accessibilityHint: mergeAccessibilityText(
', ',
rest.accessibilityHint,
accessibilityProps.input.accessibilityHint
),
editable: isEditable,
placeholder,
style: inputStyles,
onChangeText,
onFocus,
onBlur,
})}

{hasSuffix && <Text style={suffixStyles}>{suffix}</Text>}
</Animated.View>

{renderTrailingAccessory ? (
renderTrailingAccessory({
style: trailingAccessoryStyles,
error: hasError,
disabled: isDisabled,
multiline: !!rest.multiline,
})
) : hasError ? (
<TextInputErrorIcon
style={trailingAccessoryStyles}
theme={theme}
/>
) : null}
</View>

<View style={styles.addendum}>
{!!supportingText && (
<Text
{...accessibilityProps.supportingText}
style={supportingTextStyles}
>
{supportingText}
</Text>
)}

{hasCounter && (
<Text {...accessibilityProps.counter} style={counterStyles}>
{counterText}
</Text>
)}
</View>
</>
)}
</Pressable>
);
}
Expand Down
36 changes: 28 additions & 8 deletions src/components/TextInput/TextInputIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getIconColor } from './utils';
import { useInternalTheme } from '../../core/theming';
import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop';
import hasTouchHandler from '../../utils/hasTouchHandler';
import Icon from '../Icon';
import IconButton from '../IconButton/IconButton';
import type { Props as IconButtonProps } from '../IconButton/IconButton';

Expand All @@ -33,8 +34,10 @@ export type TextInputIconProps = TextInputAccessoryProps &
* `TextInputAccessoryProps`, which TextInput passes into those render props.
*
* Without `onPress`, `onLongPress`, `onPressIn` or `onPressOut`, the icon is
* decorative: a plain element that keeps its label but isn't a control, and a
* tap on it goes to the field.
* decorative: a plain element, not an `IconButton`, so it has no button role
* and isn't a focus stop. It is hidden from assistive tech unless you pass
* `aria-label`, and a tap on it goes to the field. An icon with a handler is a
* control and needs `aria-label` (or `accessibilityLabel`).
*
* ## Usage
* ```js
Expand Down Expand Up @@ -91,9 +94,9 @@ const TextInputIcon = ({
isDisabled: disabled,
});

// A decorative icon is not a control, so `disabled` would only announce it as
// a disabled button. Must match TouchableRipple's predicate, or an icon with
// only `onLongPress` stays pressable on a disabled field.
// Decorative icons render no button at all. Must match TouchableRipple's
// predicate, or an icon with only `onLongPress` would count as decorative
// and stay pressable on a disabled field.
const { onLongPress, onPressIn, onPressOut } = rest;
const isInteractive = hasTouchHandler({
onPress,
Expand All @@ -102,6 +105,23 @@ const TextInputIcon = ({
onPressOut,
});

const accessibleName = rest['aria-label'] ?? rest.accessibilityLabel;

if (!isInteractive) {
return (
<View
ref={rest.ref}
testID={rest.testID}
style={[styles.icon, style]}
{...(accessibleName
? { accessible: true, role: 'img', 'aria-label': accessibleName }
: { 'aria-hidden': true })}
>
<Icon source={icon} color={color} size={iconSize} theme={theme} />
</View>
);
}

return (
<View style={styles.iconWrapper}>
<IconButton
Expand All @@ -117,10 +137,10 @@ const TextInputIcon = ({
// TextInput already dims the whole accessory when the field is
// disabled. Forwarding `disabled` makes IconButton dim the icon too,
// and the two multiply, so let IconButton own it and cancel the outer
// one. A decorative icon keeps it, nothing else dims it.
isInteractive && disabled ? styles.notDimmed : null,
// one.
disabled ? styles.notDimmed : null,
]}
disabled={isInteractive ? disabled : undefined}
disabled={disabled}
onPress={onPress}
/>
</View>
Expand Down
4 changes: 4 additions & 0 deletions src/components/TextInput/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
useEffect,
useId,
useImperativeHandle,
useRef,
useState,
Expand Down Expand Up @@ -323,6 +324,7 @@ const useTextInputLayout = ({
export const useTextInput = (props: TextInputProps): TextInputHookReturn => {
const { ref, variant = 'filled', theme: themeOverride } = props;

const id = useId();
const input = useRef<ComponentRef<typeof NativeTextInput>>(null);
const init = useRef(false);

Expand Down Expand Up @@ -421,6 +423,8 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {
});

const accessibilityProps = getAccessibilityData({
supportingTextId: `${id}-supporting-text`,
counterId: `${id}-counter`,
hasError: flags.hasError,
hasCounter: flags.hasCounter,
isDisabled: flags.isDisabled,
Expand Down
Loading
Loading