浏览代码

update ui.link and ui.link_target

Falko Schindler 2 年之前
父节点
当前提交
2b36bcd104
共有 3 个文件被更改,包括 14 次插入13 次删除
  1. 9 13
      nicegui/elements/link.py
  2. 2 0
      nicegui/ui.py
  3. 3 0
      test_v1.py

+ 9 - 13
nicegui/elements/old/link.py → nicegui/elements/link.py

@@ -1,15 +1,12 @@
 from typing import Callable, Union
 
-import justpy as jp
+from ..element import Element
+from .mixins.text_element import TextElement
 
-from .. import globals
-from .element import Element
-from .group import Group
 
+class Link(TextElement):
 
-class Link(Group):
-
-    def __init__(self, text: str = '', target: Union[Callable, str] = '#'):
+    def __init__(self, text: str = '', target: Union[Callable, str] = '#') -> None:
         """Link
 
         Create a hyperlink.
@@ -20,10 +17,9 @@ class Link(Group):
         :param text: display text
         :param target: page function or string that is a an absolute URL or relative path from base URL
         """
-        href = target if isinstance(target, str) else globals.find_route(target)[1:]
-        view = jp.A(text=text, href=href, classes='underline text-blue', temp=False)
-
-        super().__init__(view)
+        super().__init__(tag='a', text=text)
+        self._props['href'] = target if isinstance(target, str) else None  # TODO: globals.find_route(target)[1:]
+        self._classes.extend(['underline, text-blue'])
 
 
 class LinkTarget(Element):
@@ -35,5 +31,5 @@ class LinkTarget(Element):
 
         :param name: target name
         """
-        view = jp.A(name=name, temp=False)
-        super().__init__(view)
+        super().__init__('a')
+        self._props['name'] = name

+ 2 - 0
nicegui/ui.py

@@ -11,6 +11,8 @@ from .elements.icon import Icon as icon
 from .elements.image import Image as image
 from .elements.joystick import Joystick as joystick
 from .elements.label import Label as label
+from .elements.link import Link as link
+from .elements.link import LinkTarget as link_target
 from .elements.row import Row as row
 from .elements.separator import Separator as separator
 from .elements.tooltip import Tooltip as tooltip

+ 3 - 0
test_v1.py

@@ -50,6 +50,9 @@ with ui.card():
                                                          f"{msg['args']['data']['vector']['y']:.3f}"),
                 on_end=lambda msg: coordinates.set_text('0, 0')).style('width: 15em')
     coordinates = ui.label('0, 0')
+    ui.link('Google', 'https://www.google.com/')
+    ui.link('Target', '#target')
+    ui.link_target('target')
 
 
 ui.run(port=1234)