Skip to content
Open
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
15 changes: 10 additions & 5 deletions src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import type { AnimatedStyle } from 'react-native-reanimated';
import { useTextInput } from './hooks';
import { styles } from './styles';
import TextInputErrorIcon from './TextInputErrorIcon';
import type { TextInputAccessoryProps } from './TextInputIcon';
import {
TextInputAccessoryPlacementContext,
type TextInputAccessoryProps,
} from './TextInputIcon';
import type { InternalTheme, ThemeProp } from '../../theme/types';

export type TextInputAnimationState = {
Expand Down Expand Up @@ -464,14 +467,16 @@ function TextInput({
</Text>
)}

{renderLeadingAccessory
? renderLeadingAccessory({
{renderLeadingAccessory ? (
<TextInputAccessoryPlacementContext.Provider value="start">
{renderLeadingAccessory({
style: leadingAccessoryStyles,
error: hasError,
disabled: isDisabled,
multiline: !!rest.multiline,
})
: null}
})}
</TextInputAccessoryPlacementContext.Provider>
) : null}

{/* Keep the field visible to native accessibility even before focus. */}
<View style={containerStyles}>
Expand Down
7 changes: 6 additions & 1 deletion src/components/TextInput/TextInputIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export type TextInputAccessoryProps = {
export type TextInputIconProps = TextInputAccessoryProps &
Omit<IconButtonProps, keyof TextInputAccessoryProps | 'size'>;

export const TextInputAccessoryPlacementContext = React.createContext<
'start' | 'end'
>('end');

/**
* A component to render a leading / trailing icon in the TextInput
* (return it from `startAccessory` or `endAccessory`). Accepts icon-specific props as well as
Expand Down Expand Up @@ -85,11 +89,12 @@ const TextInputIcon = ({
...rest
}: TextInputIconProps) => {
const theme = useInternalTheme(themeOverride);
const placement = React.useContext(TextInputAccessoryPlacementContext);

const color = getIconColor({
theme,
iconColor,
hasError: error,
hasError: placement === 'start' ? false : error,
isDisabled: disabled,
});

Expand Down
18 changes: 18 additions & 0 deletions src/components/__tests__/TextInputStates.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ it('uses the disabled colors for a disabled invalid field and keeps it invalid o
}
expect(screen.getByLabelText('Email')).toHaveProp('aria-invalid', true);
});

it('keeps the start icon onSurfaceVariant while the end icon uses the error color', async () => {
await render(
<TextInput
label="Email"
error
startAccessory={(props) => <TextInput.Icon {...props} icon="email" />}
endAccessory={(props) => <TextInput.Icon {...props} icon="close" />}
/>
);

expect(
screen.getByText('email', { includeHiddenElements: true })
).toHaveStyle({ color: theme.colors.onSurfaceVariant });
expect(
screen.getByText('close', { includeHiddenElements: true })
).toHaveStyle({ color: theme.colors.error });
});
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
style={
[
{
"color": "rgba(179, 38, 30, 1)",
"color": "rgba(73, 69, 79, 1)",
"fontSize": 24,
},
[
Expand Down Expand Up @@ -2305,7 +2305,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
style={
[
{
"color": "rgba(179, 38, 30, 1)",
"color": "rgba(73, 69, 79, 1)",
"fontSize": 24,
},
[
Expand Down
Loading