WolfgangFahl / nicegui_widgets

nicegui widgets
http://ngdemo.bitplan.com
Apache License 2.0
25 stars 2 forks source link

add HideShow widget #44

Closed WolfgangFahl closed 10 months ago

WolfgangFahl commented 10 months ago

migrate justpy jpwidgets code to nicegui:

class HideShow(jp.Div):
    """
    Create a Div with visibility (hid/show) toogle 
    """
    TRIANGLE_LEFT = "◀"
    TRIANGLE_DOWN = "▼"

    def __init__(
        self,
        hide_show_label: tuple,
        show_content: bool = True,
        **kwargs):
        """
        constructor

        Args:
            content: justpy component with the content to hide/show
            hide_show_label: labels to be shown if the content is hidden/shown.       
            show_content: If True show the content at page load otherwise the content is hidden.
            **kwargs: additional justpy arguments
        """
        # first add the hide/show button to my parent
        a=kwargs.get("a")
        self.label_if_shown, self.label_if_hidden = hide_show_label
        self.btn = jp.Button(a=a, text=self._getStatusLabel(show_content), on_click=self.toggleHideShow)
        # then create a div component
        jp.Div.__init__(self,**kwargs)
        self._setShowContent(show_content)

    def _setShowContent(self,show_content:bool):
        """
        set my show_content state via my data container
        """
        self.data["show_content"]=show_content
        self.hidden(not show_content)

    def _getStatusLabel(self,show_content:bool) -> str:
        """
        Returns the Icon of the current status
        """
        if show_content:
            icon = self.TRIANGLE_DOWN
            label = self.label_if_shown
        else:
            icon = self.TRIANGLE_LEFT
            label = self.label_if_hidden if self.label_if_hidden is not None else self.label_if_shown
        return f"{label} {icon}"      

    def toggleHideShow(self, _msg:dict):
        """
        Toggle the visibility status of the content
        """
        show_content = not self.data["show_content"]
        self._setShowContent(show_content)
        self.btn.text = self._getStatusLabel(show_content)
WolfgangFahl commented 9 months ago

2023-12-26___122814