From 13a1d462a5bbfe9d5b76997a61cb166fd39d38fc Mon Sep 17 00:00:00 2001 From: cswamy <101974014+cswamy@users.noreply.github.com> Date: Wed, 7 Feb 2024 13:04:53 +0000 Subject: [PATCH] Fix for #148: open links in new tab (#157) --- demo/components_list.py | 4 ++++ src/npm-fastui/src/events.ts | 6 +++++- src/npm-fastui/src/models.d.ts | 1 + src/python-fastui/fastui/events.py | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/demo/components_list.py b/demo/components_list.py index 6e3622c1..b329c1b9 100644 --- a/demo/components_list.py +++ b/demo/components_list.py @@ -78,6 +78,10 @@ class Delivery(BaseModel): components=[c.Text(text='Pydantic (External link)')], on_click=GoToEvent(url='https://pydantic.dev'), ), + c.Link( + components=[c.Text(text='FastUI repo (New tab)')], + on_click=GoToEvent(url='https://github.com/pydantic/FastUI', target='_blank'), + ), ], ), ], diff --git a/src/npm-fastui/src/events.ts b/src/npm-fastui/src/events.ts index 45e3de90..45300aac 100644 --- a/src/npm-fastui/src/events.ts +++ b/src/npm-fastui/src/events.ts @@ -35,7 +35,11 @@ export function useFireEvent(): { fireEvent: (event?: AnyEvent) => void } { } case 'go-to': if (event.url) { - location.goto(event.url) + if (event.target) { + window.open(event.url, event.target) + } else { + location.goto(event.url) + } } if (event.query) { location.setQuery(event.query) diff --git a/src/npm-fastui/src/models.d.ts b/src/npm-fastui/src/models.d.ts index a083f8b7..ff5ee388 100644 --- a/src/npm-fastui/src/models.d.ts +++ b/src/npm-fastui/src/models.d.ts @@ -141,6 +141,7 @@ export interface GoToEvent { query?: { [k: string]: string | number } + target?: '_blank' type: 'go-to' } export interface BackEvent { diff --git a/src/python-fastui/fastui/events.py b/src/python-fastui/fastui/events.py index f4a91c33..c572ef87 100644 --- a/src/python-fastui/fastui/events.py +++ b/src/python-fastui/fastui/events.py @@ -18,6 +18,7 @@ class GoToEvent(BaseModel): # can be a path or a full URL url: Union[str, None] = None query: Union[Dict[str, Union[str, float, None]], None] = None + target: Union[Literal['_blank'], None] = None type: Literal['go-to'] = 'go-to'