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
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ internal object ViewManagersPropertyCache {
}
}

private class BoxedFloatPropSetter(prop: ReactProp, setter: Method) :
PropSetter(prop, "number", setter) {

override fun getValueOrDefault(value: Any?, context: Context): Any? {
if (value != null) {
// All numbers from JS are Doubles which can't be simply cast to Float
return if (value is Double) value.toFloat() else value as Float
}
return null
}
}

private class BoxedIntPropSetter : PropSetter {

constructor(prop: ReactProp, setter: Method) : super(prop, "number", setter)
Expand Down Expand Up @@ -391,6 +403,7 @@ internal object ViewManagersPropertyCache {
DoublePropSetter(annotation, method, annotation.defaultDouble)
String::class.java -> StringPropSetter(annotation, method)
java.lang.Boolean::class.java -> BoxedBooleanPropSetter(annotation, method)
java.lang.Float::class.java -> BoxedFloatPropSetter(annotation, method)
java.lang.Integer::class.java ->
if ("Color" == annotation.customType) {
BoxedColorPropSetter(annotation, method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ReactPropAnnotationSetterTest {

fun onBoxedBooleanSetterCalled(value: Boolean?)

fun onBoxedFloatSetterCalled(value: Float?)

fun onBoxedIntSetterCalled(value: Int?)

fun onArraySetterCalled(value: ReadableArray?)
Expand Down Expand Up @@ -128,6 +130,11 @@ class ReactPropAnnotationSetterTest {
viewManagerUpdatesReceiver.onBoxedIntSetterCalled(value)
}

@ReactProp(name = "boxedFloatProp")
fun setBoxedFloatProp(v: View?, value: Float?) {
viewManagerUpdatesReceiver.onBoxedFloatSetterCalled(value)
}

@ReactProp(name = "arrayProp")
fun setArrayProp(v: View?, value: ReadableArray?) {
viewManagerUpdatesReceiver.onArraySetterCalled(value)
Expand Down Expand Up @@ -314,6 +321,22 @@ class ReactPropAnnotationSetterTest {
Mockito.reset(updatesReceiverMock)
}

@Test
fun testBoxedFloatSetter() {
viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", 3.5))
Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(3.5f)
Mockito.verifyNoMoreInteractions(updatesReceiverMock)
Mockito.reset(updatesReceiverMock)
viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", -7.0))
Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(-7.0f)
Mockito.verifyNoMoreInteractions(updatesReceiverMock)
Mockito.reset(updatesReceiverMock)
viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", null))
Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(null)
Mockito.verifyNoMoreInteractions(updatesReceiverMock)
Mockito.reset(updatesReceiverMock)
}

@Test
fun testArraySetter() {
val array: ReadableArray = JavaOnlyArray()
Expand Down
Loading