Skip to content

Commit 234c674

Browse files
aashay-gaikwadfacebook-github-bot
authored andcommitted
Spec to KComponent -> ParentComponentMediator and ChildComponentSiblingCommunication
Summary: Updating LithoDocs for Communicating between Components to have KComponent samples: https://fblitho.com/docs/mainconcepts/coordinate-state-actions/communicating-between-components/ - Will be updating the guidelines towards the end of the diff stack - Not deleting the Spec files as they're used for Java Examples Reviewed By: astreet Differential Revision: D73106322 fbshipit-source-id: 2f0509641559faffa0cf085aa9bad98f75d17528
1 parent a05d4d8 commit 234c674

3 files changed

Lines changed: 113 additions & 2 deletions

File tree

docs/mainconcepts/coordinate-state-actions/communicating-between-components.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ Two sibling components (two child components of the same parent) cannot communic
6161

6262
A child component that needs to send a signal to a sibling component will dispatch an event to the common parent component:
6363

64-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ChildComponentSiblingCommunicationSpec.java start=start_dispatch_to_parent end=end_dispatch_to_parent
64+
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ChildComponentSiblingCommunicationKComponent.kt start=start_dispatch_to_parent end=end_dispatch_to_parent
6565
```
6666

6767
As shown in the following code, the parent component can:
6868

6969
* Perform a state update to recreate the sibling with new data (@OnUpdateState)
7070
* Trigger an event on the sibling using a reference (@OnEvent).
7171

72-
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentMediatorSpec.java start=start_parent_mediator end=end_parent_mediator
72+
```java file=sample/src/main/java/com/facebook/samples/litho/java/communicating/ParentComponentMediatorKComponent.kt start=start_parent_mediator end=end_parent_mediator
7373
```
7474

7575
### Communicating externally to a component
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.samples.litho.java.communicating
18+
19+
import android.graphics.Color
20+
import com.facebook.litho.Component
21+
import com.facebook.litho.ComponentScope
22+
import com.facebook.litho.KComponent
23+
import com.facebook.litho.Row
24+
import com.facebook.litho.Style
25+
import com.facebook.litho.core.height
26+
import com.facebook.litho.core.margin
27+
import com.facebook.litho.core.width
28+
import com.facebook.litho.flexbox.border
29+
import com.facebook.litho.kotlin.widget.Border
30+
import com.facebook.litho.kotlin.widget.BorderEdge
31+
import com.facebook.litho.kotlin.widget.BorderRadius
32+
import com.facebook.litho.kotlin.widget.SolidColor
33+
import com.facebook.litho.kotlin.widget.Text
34+
import com.facebook.litho.view.onClick
35+
import com.facebook.rendercore.dp
36+
37+
// start_dispatch_to_parent
38+
class ChildComponentSiblingCommunicationKComponent(
39+
private val id: Int,
40+
private val isSelected: Boolean,
41+
private val onSelected: (Int) -> Unit,
42+
) : KComponent() {
43+
44+
override fun ComponentScope.render(): Component? {
45+
return Row(style = Style.onClick { onSelected(id) }.margin(all = 30.dp)) {
46+
child(
47+
SolidColor(
48+
color = if (isSelected) Color.BLUE else Color.WHITE,
49+
style =
50+
Style.width(20.dp)
51+
.height(20.dp)
52+
.margin(top = 10.dp, end = 30.dp)
53+
.border(
54+
Border(
55+
edgeAll = BorderEdge(color = Color.BLUE, width = 1.dp),
56+
radius = BorderRadius(all = 2.dp)))))
57+
child(Text(text = "ChildComponent $id", textSize = 20.dp))
58+
}
59+
}
60+
}
61+
// end_dispatch_to_parent
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.samples.litho.java.communicating
18+
19+
import com.facebook.litho.Column
20+
import com.facebook.litho.Component
21+
import com.facebook.litho.ComponentScope
22+
import com.facebook.litho.KComponent
23+
import com.facebook.litho.Style
24+
import com.facebook.litho.core.padding
25+
import com.facebook.litho.kotlin.widget.Text
26+
import com.facebook.litho.useState
27+
import com.facebook.rendercore.dp
28+
29+
// start_parent_mediator
30+
class ParentComponentMediatorKComponent : KComponent() {
31+
override fun ComponentScope.render(): Component? {
32+
33+
val selectedPosition = useState { 0 }
34+
35+
return Column(style = Style.padding(all = 30.dp)) {
36+
child(Text(text = "ChildComponent", textSize = 30.dp))
37+
child(
38+
ChildComponentSiblingCommunicationKComponent(
39+
id = 0,
40+
isSelected = selectedPosition.value == 0,
41+
onSelected = { selectedPosition.update(0) }))
42+
child(
43+
ChildComponentSiblingCommunicationKComponent(
44+
id = 1,
45+
isSelected = selectedPosition.value == 1,
46+
onSelected = { selectedPosition.update(1) }))
47+
}
48+
}
49+
}
50+
// end_parent_mediator

0 commit comments

Comments
 (0)