I am using Nativewind (AKA Tailwind) for styling and I have a styled component:
export const StyledTextInput = styled(
TextInput,
'border-solid border-2 border-gray-200 bg-gray-100 p-1 rounded-md'
)
I want to override the style, for example, if the input is wrong, like this:
<StyledTextInput
textContentType="password"
placeholder="Password"
placeholderTextColor="grey"
secureTextEntry={true}
onBlur={onBlur}
onChangeText={onChange}
value={value}
className={errors.passwordField ? 'border-red-500' : ''}
/>
However, it won't always work. But sometimes it will.
Let me show you:
If in the second code snipped (where I do the conditional styling) I add "p-6" to the className, it looks as expected, with an increased padding:
But if I add "p-5", the original className is used and the value is not overriden. And it looks like this:
This is not even consystent. For "p-2", "p-4" and >=6 it will work, but not for "p-5" and "p-3". Something symilar happens with the color. The original className does not get overridden with 'border-red-500' but it does with 'border-red-700' or 'border-red-800'...
Edit: Thanks to the comments, I now understand that the application of the styles is not based on "className" hierarchy, but rather following the “stylesheet”. Can I change or predict how the stylesheet is built by Tailwind?
Additionally, Is there any way I could include the conditional formatting in the "original" styled component (StyledTextInput), so it is consistent across screens and I don't have to be overriding?