# Llamar a una acción de widget desde otro widget

**URL:** https://meta.discourse.org/t/call-a-widget-action-from-another-widget/234292
**Category:** Support
**Created:** [28 Julio, 2022 16:54 UTC](https://meta.discourse.org/t/call-a-widget-action-from-another-widget/234292 "2022-07-28T16:54:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lcor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lcor/32/249044_2.png) [@lcor](https://meta.discourse.org/u/lcor)
#### Post date: [28 Julio, 2022 16:54 UTC](https://meta.discourse.org/t/call-a-widget-action-from-another-widget/234292/1 "2022-07-28T16:54:55Z")

</div>

Hola,

Me gustaría llamar a la acción `toggleSearchMenu` desde un widget de botón personalizado que abra el menú de búsqueda al hacer clic en él.

Intenté:

```plaintext
  click() {
    this.sendWidgetAction('toggleSearchMenu').then(() => console.log("SENDWIDGETACTION"));
  }

```

Pero al hacer clic en mi botón, dice `toggleSearchMenu not found` en la consola.

Intenté otras formas que `no funcionan` como puedes ver aquí:

```plaintext
import { createWidget } from "discourse/widgets/widget";
import { iconNode } from "discourse-common/lib/icon-library";
import { h } from 'virtual-dom';

export default createWidget("search-btn-widget", {
  tagName: "div.search-btn-widget",
  buildKey: () => "search-btn-widget",

  html(args) {
    let search_button_helper = "button#search-btn";
    let search_button_class;
    let search_title = "Chercher un sujet";
    let search_text = "Chercher un sujet";
    let search_icon = "ri-small-recherche";
    let search_label_helper = "span.d-button-label";

    if (args == "default") {
      search_button_class = "btn btn-default btn btn-icon-text";
    } else if (args == "primary") {
      search_button_class = "btn btn-primary btn btn-icon-text";
    } else {
      return;
    }

    const toggleSearch = function() {
      this.sendWidgetAction("toggleSearchMenu");
    };

    return h(
      search_button_helper,
      {
        className: search_button_class,
        title: search_title,
        // action: "toggleSearchMenu" => No funciona
        // onclick: "toggleSearchMenu" => No funciona
        onclick: toggleSearch
      },
      [iconNode(search_icon), h(search_label_helper, search_text)]
    )
  },
  // click() {
  // this.sendWidgetAction('toggleSearchMenu').then(() => console.log("SENDWIDGETACTION"));
  // } => No funciona
});

```

¿Alguna idea sobre cómo podría llamar a esta acción de widget dentro de mi widget si es que es posible?

¡Muchas gracias! 🙏

---

<div class="post-metadata">

### Author: ![lcor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lcor/32/249044_2.png) [@lcor](https://meta.discourse.org/u/lcor)
#### Post date: [2 Agosto, 2022 15:30 UTC](https://meta.discourse.org/t/call-a-widget-action-from-another-widget/234292/2 "2022-08-02T15:30:43Z")

</div>

Encontré una solución, usando `createWidgetFrom` e importando el widget de encabezado:

```plaintext
import {
  default as header
} from "discourse/widgets/header";

```

Eso me permitió acceder al ámbito del método de acción `toggleSearchMenu`.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [1 Septiembre, 2022 15:31 UTC](https://meta.discourse.org/t/call-a-widget-action-from-another-widget/234292/3 "2022-09-01T15:31:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
