Skip to content

Commit 780939f

Browse files
srdjan-harnessHarness
authored andcommitted
update ViewOnly (#10304)
* 62c3e8 improve * 5957d7 update ViewOnly
1 parent 48ecb82 commit 780939f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

packages/ui/src/components/view-only.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { ReactNode, useLayoutEffect, useRef, useState } from 'react'
1+
import { isValidElement, ReactNode, useCallback, useLayoutEffect, useRef, useState } from 'react'
22

33
import { Layout, Separator, Spacer, Text } from '@/components'
44
import { useResizeObserver } from '@/hooks'
55
import { cn, wrapConditionalObjectElement } from '@/utils'
66

7+
export type ViewOnlyItemData = { label: string; value: ReactNode } | ReactNode
8+
79
function splitArray<T>(array: T[]): [T[], T[]] {
810
if (array.length <= 2) {
911
return [array, []]
@@ -16,7 +18,7 @@ function splitArray<T>(array: T[]): [T[], T[]] {
1618
return [firstHalf, secondHalf]
1719
}
1820

19-
const ViewOnlyItem = ({ label, value }: { label: string; value: ReactNode }) => (
21+
export const ViewOnlyItem = ({ label, value }: { label: string; value: ReactNode }) => (
2022
<Layout.Grid key={label} flow="row" gapX="2xl" columns="minmax(0, 200px) minmax(0, 1fr)" align="start">
2123
<Text color="foreground-3" as="dt">
2224
{label}
@@ -31,7 +33,7 @@ const isWideEnoughForColumns = (width: number) => width >= 800
3133

3234
export interface ViewOnlyProps {
3335
title?: string
34-
data: { label: string; value: ReactNode }[]
36+
data: ViewOnlyItemData[]
3537
layout?: 'singleColumn' | 'columns'
3638
className?: string
3739
}
@@ -59,6 +61,16 @@ export const ViewOnly = ({ className, title, data, layout = 'columns' }: ViewOnl
5961
200
6062
)
6163

64+
const renderItem = useCallback((item: ViewOnlyItemData) => {
65+
if (item instanceof Object && 'label' in item && 'value' in item && typeof item.label === 'string') {
66+
return <ViewOnlyItem key={item.label} label={item.label} value={item.value} />
67+
} else if (isValidElement(item)) {
68+
return item
69+
}
70+
71+
return null
72+
}, [])
73+
6274
if (!data || data.length === 0) return null
6375

6476
const isSeparatorVisible = isLayoutColumns && data.length > 2
@@ -77,22 +89,14 @@ export const ViewOnly = ({ className, title, data, layout = 'columns' }: ViewOnl
7789
align="start"
7890
{...wrapConditionalObjectElement({ columns: '1fr auto 1fr' }, isLayoutColumns)}
7991
>
80-
<Layout.Grid className="gap-y-3.5">
81-
{leftColumnData.map(({ label, value }) => (
82-
<ViewOnlyItem key={label} label={label} value={value} />
83-
))}
84-
</Layout.Grid>
92+
<Layout.Grid className="gap-y-3.5">{leftColumnData.map(item => renderItem(item))}</Layout.Grid>
8593

8694
{isLayoutColumns && (
8795
<Separator orientation="vertical" className={cn('ml-4 mr-5', { invisible: !isSeparatorVisible })} />
8896
)}
8997

9098
{!!rightColumnData && (
91-
<Layout.Grid className="gap-y-3.5">
92-
{rightColumnData.map(({ label, value }) => (
93-
<ViewOnlyItem key={label} label={label} value={value} />
94-
))}
95-
</Layout.Grid>
99+
<Layout.Grid className="gap-y-3.5">{rightColumnData.map(item => renderItem(item))}</Layout.Grid>
96100
)}
97101
</Layout.Grid>
98102

0 commit comments

Comments
 (0)