Skip to content

Commit 4b7c103

Browse files
authored
Allow nesting subcomponents like Images inside of Tables, Details, etc. (#308)
1 parent 57815a7 commit 4b7c103

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/npm-fastui/src/components/display.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { FC } from 'react'
22

3-
import type { AnyEvent, DisplayMode, Display, JsonData } from '../models'
3+
import type { AnyEvent, DisplayMode, Display, JsonData, FastProps } from '../models'
44

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

8+
import { AnyComp } from '.'
9+
810
import { JsonComp } from './Json'
911
import { LinkRender } from './link'
1012

@@ -26,6 +28,28 @@ export const DisplayComp: FC<Display> = (props) => {
2628
}
2729
}
2830

31+
// todo: this list should probably be defined in the models file
32+
const nestableSubcomponents = [
33+
'Text',
34+
'Paragraph',
35+
'Div',
36+
'Heading',
37+
'Markdown',
38+
'Code',
39+
'Json',
40+
'Button',
41+
'Link',
42+
'LinkList',
43+
'ServerLoad',
44+
'Image',
45+
'Iframe',
46+
'Video',
47+
'Spinner',
48+
'Custom',
49+
'Table',
50+
'Details',
51+
]
52+
2953
const DisplayRender: FC<Display> = (props) => {
3054
const mode = props.mode ?? 'auto'
3155
const value = props.value ?? null
@@ -34,7 +58,11 @@ const DisplayRender: FC<Display> = (props) => {
3458
} else if (Array.isArray(value)) {
3559
return <DisplayArray mode={mode} value={value} />
3660
} else if (typeof value === 'object' && value !== null) {
37-
return <DisplayObject mode={mode} value={value} />
61+
if (value.type !== null && typeof value.type === 'string' && nestableSubcomponents.includes(value.type)) {
62+
return <AnyComp {...(value as unknown as FastProps)} />
63+
} else {
64+
return <DisplayObject mode={mode} value={value} />
65+
}
3866
} else {
3967
return <DisplayPrimitive mode={mode} value={value} />
4068
}

0 commit comments

Comments
 (0)