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

Allow nesting subcomponents like Images inside of Tables, Details, etc. #308

Merged
merged 1 commit into from
May 30, 2024
Merged
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
32 changes: 30 additions & 2 deletions 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,28 @@ export const DisplayComp: FC<Display> = (props) => {
}
}

// todo: this list should probably be defined in the models file
const nestableSubcomponents = [
Copy link
Member

Choose a reason for hiding this comment

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

I think there are two clean options here in terms of ways forward:

  1. Something like add FormattedText component #29 where we define a subset of components (or maybe they're not even included in regular components) that can be used in buttons and table cells and paragraphs (although I'm already seeing a problem that stuff like images belong in table cells but I think are illegal in paragraphs or buttons)
  2. We allow anything in a table cell, and DisplayObject just becomes a component, which I've wanted otherwise anyway. Perhaps we have some logic for the default components for table cells is a DisplayObject

Or maybe route 3 is we merge this to help @jimkring and worry about the ultimately correct solution later?

'Text',
'Paragraph',
'Div',
'Heading',
'Markdown',
'Code',
'Json',
'Button',
'Link',
'LinkList',
'ServerLoad',
'Image',
'Iframe',
'Video',
'Spinner',
'Custom',
'Table',
'Details',
]

const DisplayRender: FC<Display> = (props) => {
const mode = props.mode ?? 'auto'
const value = props.value ?? null
Expand All @@ -34,7 +58,11 @@ const DisplayRender: FC<Display> = (props) => {
} else if (Array.isArray(value)) {
return <DisplayArray mode={mode} value={value} />
} else if (typeof value === 'object' && value !== null) {
return <DisplayObject mode={mode} value={value} />
if (value.type !== null && typeof value.type === 'string' && nestableSubcomponents.includes(value.type)) {
return <AnyComp {...(value as unknown as FastProps)} />
} else {
return <DisplayObject mode={mode} value={value} />
}
} else {
return <DisplayPrimitive mode={mode} value={value} />
}
Expand Down
Loading