Skip to content
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

Event from parent to child (v1.6.0) #309

Closed
iamgpe opened this issue Mar 10, 2023 · 2 comments
Closed

Event from parent to child (v1.6.0) #309

iamgpe opened this issue Mar 10, 2023 · 2 comments

Comments

@iamgpe
Copy link

iamgpe commented Mar 10, 2023

When I try to retrieve an event from a component declared as parent (or parent of parent, etc..), the child component doesn't seem to want to retrieve it.

It would be nice to have a feature that would allow to trigger an event launched in a parent component in a child.

  • expected idea (from parent to child)
<parent-component>
    <child-component data-action="parent-component.custom-event:child-component#childComponentMethod">
        <!-- some code here -->
    </child-component>
</parent-component>
  • code that works (from child to parent)
<parent-component>
    <child-component data-action="child-event:parent-component#childComponentMethod">
        <!-- some code here -->
    </child-component>
</parent-component>

I haven't found an equivalent way to do this kind of thing on catalyst, but it would be nice to have this kind of feature.

@iamgpe iamgpe changed the title Event from parent to child (v 1.6.0) Event from parent to child (v1.6.0) Mar 10, 2023
@iamgpe
Copy link
Author

iamgpe commented Mar 10, 2023

This workaround is ugly but it work:

In your dom

<parent-component>
    <child-component data-targets="parent-component.bubbleables">
        <!-- some code here -->
    </child-component>
</parent-component>

In your parent component

import { controller, targets } from "@github/catalyst";

@controller
class ParentComponentElement extends HTMLElement {
    @targets bubbleables!: HTMLElement[];

    // call when you need to dispatch event to child
    private dispatchEventToBubbleables(eventName: BusEvents): void {
        const event = new CustomEvent(eventName);

        this.bubbleables.forEach((element) => element.dispatchEvent(event));
    }
}

In your child components

import { controller } from "@github/catalyst";

@controller
class ChildrenComponentElement extends HTMLElement {
    connectedCallback(): void {
        this.addEventListener('eventName', this.doSomething);
    }

    private doSomething(): void {
        console.log('event received 🤩🤩🤩');
    }
}

@keithamus
Copy link
Member

Thanks for the issue!

Events should only go upwards, from children to parents. Parents can use direct communication via the child’s API - directly calling methods and accessing properties. You can use targets to help easily query child components and use their class methods.

@iamgpe iamgpe closed this as completed Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants