seewindcn / reflex-antd

A Reflex wrapper for Antd
Apache License 2.0
19 stars 4 forks source link

Menu events for Dropdown Button #7

Open TheRideout opened 1 month ago

TheRideout commented 1 month ago

I am trying to get the menu of the dropdown button to call an event function but am running into argument errors.

class MyState(rx.State):
    def menu_click(self, e):
        return rx.console_log(e)

    def dropdown_click(self):
        return rx.console_log('Button clicked')

def dropdown_test() -> rx.Component:
    return navigation.dropdown_button(
        "Test",
        menu=helper.contain(
            items=[{'label': 'item1', 'key': 1}, {'label': 'item2', 'key': 2}],
            on_click=MyState.menu_click
        ),
        on_click=MyState.dropdown_click
    )

However regardless of the function signature of MyState.menu_click (unless there are no args), I get the following:

ValueError: number of arguments in MyState.menu_click doesn't match the definition of the event trigger 'rs = triggers.get(self._get_event_trigger_key(), lambda: [])' defined in the 'DropdownButton' component

I would ultimately like to pass info of the clicked menu item into the MyState.menu_click function. The demo on the official antd docs simply pass the event along, though it seems you may have access to key and keyPath.

Since the dropdown button menu seems to mirror the antd menu component, I took a look at how that was wrapped as well. Mirroring that a bit to modify the dropdown button wrapper class, I seemed to get it to work, and updated MyState.menu_click to accept key, key_path args.

class OnSelectEvent(Base):
    item: Any
    key: Any
    keyPath: Any
    selectedKeys: List[str]

class DropdownButton(Dropdown):
    ...

    def get_event_triggers(self) -> Dict[str, Any]:
        _triggers = super().get_event_triggers()

        def _menu_on_click(ev: OnSelectEvent):
            return [ev.key, ev.keyPath]

        _triggers.update({
            EventTriggers.ON_CLICK: lambda: [],
            'menu.on_click': _menu_on_click,
        })
        return _triggers

Is this the right way to solve this and it simply hasn't been implemented yet, or am I missing something?

seewindcn commented 1 month ago

yes, you are right, thank you, I will fix it.