Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iframe Component support #47

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/fastui/src/components/Iframe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FC } from 'react'

import { ClassName } from '../hooks/className'

export interface IframeProps {
type: 'Iframe'
src: string
width?: string | number
height?: string | number
title?: string
className?: ClassName
}

export const IframeComp: FC<IframeProps> = (props) => {
const { src, width, height, title } = props
return <iframe src={src} width={width} height={height} title={title} />
}
5 changes: 5 additions & 0 deletions packages/fastui/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from './display'
import { JsonComp, JsonProps } from './Json'
import { ServerLoadComp, ServerLoadProps } from './ServerLoad'
import { IframeComp, IframeProps } from './Iframe'

export type {
TextProps,
Expand All @@ -63,6 +64,7 @@ export type {
DisplayPrimitiveProps,
JsonProps,
ServerLoadProps,
IframeProps,
}

// TODO some better way to export components
Expand Down Expand Up @@ -91,6 +93,7 @@ export type FastProps =
| AllDisplayProps
| JsonProps
| ServerLoadProps
| IframeProps

export type FastClassNameProps = Exclude<FastProps, TextProps | AllDisplayProps | ServerLoadProps | PageTitleProps>

Expand Down Expand Up @@ -169,6 +172,8 @@ export const AnyComp: FC<FastProps> = (props) => {
return <JsonComp {...props} />
case 'ServerLoad':
return <ServerLoadComp {...props} />
case 'Iframe':
return <IframeComp {...props} />
default:
unreachable('Unexpected component type', type, props)
return <DisplayError title="Invalid Server Response" description={`Unknown component type: "${type}"`} />
Expand Down
8 changes: 8 additions & 0 deletions python/demo/components_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ class Delivery(BaseModel):
],
class_name='border-top mt-3 pt-1',
),
c.Div(
components=[
c.Heading(text='Iframe', level=2),
c.Markdown(text='`Iframe` can be used to embed external content.'),
c.Iframe(src='https://pydantic.dev', width='100%', height=400),
],
class_name='border-top mt-3 pt-1',
),
title='Components',
)

Expand Down
1 change: 1 addition & 0 deletions python/demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def api_index() -> list[AnyComponent]:
* `Navbar` — see the top of this page
* `Modal` — static example [here](/components#button-and-modal), dynamic content example [here](/components#dynamic-modal)
* `ServerLoad` — see [dynamic modal example](/components#dynamic-modal) and [SSE example](/components#server-load-sse)
* `Iframe` - example [here](/components#iframe)
* `Table` — See [cities table](/table/cities) and [users table](/table/users)
* `Pagination` — See the bottom of the [cities table](/table/cities)
* `ModelForm` — See [forms](/forms/login)
Expand Down
9 changes: 9 additions & 0 deletions python/fastui/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ class ServerLoad(pydantic.BaseModel, extra='forbid'):
type: typing.Literal['ServerLoad'] = 'ServerLoad'


class Iframe(pydantic.BaseModel, extra='forbid'):
src: pydantic.HttpUrl
title: str | None = None
width: str | int | None = None
height: str | int | None = None
type: typing.Literal['Iframe'] = 'Iframe'


AnyComponent = typing.Annotated[
Text
| Paragraph
Expand All @@ -191,6 +199,7 @@ class ServerLoad(pydantic.BaseModel, extra='forbid'):
| Details
| Form
| ModelForm
| Iframe
| FormField,
pydantic.Field(discriminator='type'),
]