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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import android.content.res.Configuration
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.text.Editable
Expand Down Expand Up @@ -66,6 +68,7 @@ import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel
import com.facebook.react.uimanager.ReactAccessibilityDelegate
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.uimanager.style.BorderRadiusProp
import com.facebook.react.uimanager.style.BorderStyle
Expand Down Expand Up @@ -138,6 +141,8 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
private var fontFamily: String? = null
private var fontWeight = ReactConstants.UNSET
private var fontStyle = ReactConstants.UNSET
private var hasUnderlineColor = false
private var underlineColor: Int? = null
internal var parsedFontVariationSettings: String? = null
private set

Expand Down Expand Up @@ -1058,6 +1063,39 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
setBackgroundColor(this, color)
}

internal fun setUnderlineColor(color: Int?) {
hasUnderlineColor = true
underlineColor = color
applyUnderlineColor()
}

override fun setBackgroundDrawable(background: Drawable?) {
super.setBackgroundDrawable(background)
if (hasUnderlineColor) {
applyUnderlineColor()
}
}

private fun applyUnderlineColor() {
var drawableToMutate =
(background as? CompositeBackgroundDrawable)?.originalBackground ?: background ?: return

if (drawableToMutate.constantState != null) {
try {
drawableToMutate = checkNotNull(drawableToMutate.mutate())
} catch (e: NullPointerException) {
FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e)
}
}

if (underlineColor == null) {
drawableToMutate.clearColorFilter()
} else {
@Suppress("DEPRECATION")
drawableToMutate.setColorFilter(checkNotNull(underlineColor), PorterDuff.Mode.SRC_IN)
}
}

public fun setBorderWidth(position: Int, width: Float) {
setBorderWidth(this, LogicalEdge.entries[position], toDIPFromPixel(width))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,29 +524,7 @@ public open class ReactTextInputManager public constructor() :

@ReactProp(name = "underlineColorAndroid", customType = "Color")
public fun setUnderlineColor(view: ReactEditText, underlineColor: Int?) {
// Drawable.mutate() can sometimes crash due to an AOSP bug:
// See https://code.google.com/p/android/issues/detail?id=191754 for more info
val background = view.background
var drawableToMutate = background

if (background == null) {
return
}

if (background.constantState != null) {
try {
drawableToMutate = checkNotNull(background.mutate())
} catch (e: NullPointerException) {
FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e)
}
}

if (underlineColor == null) {
drawableToMutate.clearColorFilter()
} else {
@Suppress("DEPRECATION")
drawableToMutate.setColorFilter(underlineColor, PorterDuff.Mode.SRC_IN)
}
view.setUnderlineColor(underlineColor)
}

@SuppressLint("WrongConstant")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package com.facebook.react.views.textinput

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.text.InputFilter
import android.text.InputFilter.AllCaps
Expand Down Expand Up @@ -96,6 +97,20 @@ class ReactTextInputPropertyTest {
assertThat(view.inputType and InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS).isZero
}

@Test
fun testUnderlineColorAppliedWhenBackgroundBecomesAvailable() {
view.background = null

manager.setUnderlineColor(view, Color.RED)

val background = ColorDrawable(Color.WHITE)
view.background = background
assertThat(background.colorFilter).isNotNull()

manager.setUnderlineColor(view, null)
assertThat(background.colorFilter).isNull()
}

@Test
fun testAutoCapitalize() {
manager.updateProperties(view, buildStyles())
Expand Down
Loading