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

Use component as field type in table #301

Closed
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
19 changes: 18 additions & 1 deletion demo/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,25 @@ class User(BaseModel):
name: str = Field(title='Name')
dob: date = Field(title='Date of Birth')
enabled: bool | None = None
avatar: c.Image | None = None


users: list[User] = [
User(id=1, name='John', dob=date(1990, 1, 1), enabled=True),
User(
id=1,
name='John',
dob=date(1990, 1, 1),
enabled=True,
avatar=c.Image(
src='https://avatars.githubusercontent.com/u/110818415',
alt='Pydantic Logo',
width=50,
height=50,
loading='lazy',
referrer_policy='no-referrer',
class_name='border rounded',
),
),
User(id=2, name='Jane', dob=date(1991, 1, 1), enabled=False),
User(id=3, name='Jack', dob=date(1992, 1, 1)),
]
Expand All @@ -115,6 +130,7 @@ def users_view() -> list[AnyComponent]:
DisplayLookup(field='name', on_click=GoToEvent(url='/table/users/{id}/')),
DisplayLookup(field='dob', mode=DisplayMode.date),
DisplayLookup(field='enabled'),
DisplayLookup(field='avatar'),
],
),
title='Users',
Expand Down Expand Up @@ -154,6 +170,7 @@ def user_profile(id: int) -> list[AnyComponent]:
DisplayLookup(field='name'),
DisplayLookup(field='dob', mode=DisplayMode.date),
DisplayLookup(field='enabled'),
DisplayLookup(field='avatar'),
],
),
title=user.name,
Expand Down
11 changes: 10 additions & 1 deletion src/npm-fastui/src/components/display.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { FC } from 'react'

import type { AnyEvent, DisplayMode, Display, JsonData } from '../models'
import type { AnyEvent, DisplayMode, Display, JsonData, FastProps } from '../models'

import { useCustomRender } from '../hooks/config'
import { unreachable, asTitle } from '../tools'

import { AnyComp } from '.'

import { JsonComp } from './Json'
import { LinkRender } from './link'

Expand All @@ -26,6 +28,10 @@ export const DisplayComp: FC<Display> = (props) => {
}
}

const isCompProps = (obj: any): obj is FastProps => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs some work... currently very limited checks done to ensure the prop contains a valid component definition.

return 'type' in obj
}

const DisplayRender: FC<Display> = (props) => {
const mode = props.mode ?? 'auto'
const value = props.value ?? null
Expand All @@ -34,6 +40,9 @@ const DisplayRender: FC<Display> = (props) => {
} else if (Array.isArray(value)) {
return <DisplayArray mode={mode} value={value} />
} else if (typeof value === 'object' && value !== null) {
if (isCompProps(value)) {
return <AnyComp {...value} />
}
return <DisplayObject mode={mode} value={value} />
} else {
return <DisplayPrimitive mode={mode} value={value} />
Expand Down
Loading