Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a simpler way to bind reactives when dynamically mounting new widgets #4796

Closed
AliJavidiCS opened this issue Jul 24, 2024 · 0 comments

Comments

@AliJavidiCS
Copy link

Hi. This is a simple request to add simpler way to use data_bind on dynamically mounted widgets. Take a look at the sample code below please.

from textual.app import App, ComposeResult
from textual.widgets import Label
from textual.reactive import reactive
from textual.widget import Widget


class L3(Widget):
    animal_sound = reactive("Meow")

    def compose(self) -> ComposeResult:
        yield Label(self.animal_sound*3)


class L2(Widget):
    animal_sound = reactive("Woof")

    def compose(self) -> ComposeResult:
        yield Label(self.animal_sound*2)
        yield L3()

    def on_focus(self, event: Focus):
        new_L3 = L3()
        new_L3.animal_sound = "Hmmm"
        
        # How Can I bind L3's animal_sound to MainApp's animal sound directly?
        new_L3.data_bind(self.parent.animal_sound) # Would be so great if we could use objects to bind reactives instead of classes
        self.mount(new_L3)


class MainApp(App):
    animal_sound = reactive("Woof")

    def compose(self) -> ComposeResult:
        yield Label("Sorry if this question is dumb")
        yield L2()


MainApp().run()

Thanks in advance!

@Textualize Textualize deleted a comment from github-actions bot Jul 26, 2024
@Textualize Textualize locked and limited conversation to collaborators Jul 27, 2024
@willmcgugan willmcgugan converted this issue into discussion #4811 Jul 27, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant