Skip to content

Commit

Permalink
Allow nesting subcomponents like Images inside of Tables, Details, et…
Browse files Browse the repository at this point in the history
…c. (#308)
  • Loading branch information
jimkring authored May 30, 2024
1 parent 57815a7 commit 4b7c103
Showing 1 changed file with 30 additions and 2 deletions.
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 = [
'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

0 comments on commit 4b7c103

Please sign in to comment.