Skip to content

Commit 0b292e3

Browse files
committed
Improve tag view to support more data types
1 parent 6510a01 commit 0b292e3

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

src/components/result/GenericResultView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function GenericResultView({
365365
<span className="rfs-text-sm rfs-text-muted-foreground">{pid}</span>
366366
</a>
367367
<div className="rfs-flex rfs-flex-wrap rfs-gap-2 rfs-truncate">
368-
{tags && tags.map((tag) => <GenericResultViewTag key={tag.field} result={result} {...tag} />)}
368+
{tags && tags.map((tag, i) => <GenericResultViewTag key={i} result={result} {...tag} />)}
369369
</div>
370370
<div className="rfs-grow">{description}</div>
371371
<div className="rfs-mt-8 rfs-flex rfs-flex-col rfs-flex-wrap rfs-justify-end rfs-gap-2 md:rfs-flex-row md:rfs-items-center md:rfs-gap-4">

src/components/result/GenericResultViewTag.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ export function GenericResultViewTag({
8080
)
8181

8282
const base = useCallback(
83-
(fieldValue: string | string[], value: ReactNode) => {
83+
(fieldValue: string | string[], value: ReactNode, key?: string) => {
8484
return (
85-
<Badge variant="secondary" className="rfs-truncate" onClick={(e) => handleClick(fieldValue, value, e)}>
85+
<Badge key={key} variant="secondary" className="rfs-truncate" onClick={(e) => handleClick(fieldValue, value, e)}>
8686
<span className="rfs-flex rfs-truncate">
8787
{icon} {value}
8888
</span>
@@ -92,12 +92,12 @@ export function GenericResultViewTag({
9292
[handleClick, icon]
9393
)
9494

95-
if (!label) return Array.isArray(value) ? value.map((v) => base(fieldValue[value.indexOf(v)], v)) : base(fieldValue, value)
95+
if (!label) return Array.isArray(value) ? value.map((v, i) => base(fieldValue[value.indexOf(v)], v, field + i)) : base(fieldValue, value)
9696
if (!value) return null
9797

9898
if (Array.isArray(value)) {
9999
return value.map((entry, i) => (
100-
<Tooltip delayDuration={500} key={i}>
100+
<Tooltip delayDuration={500} key={field + i}>
101101
<TooltipTrigger>{base(fieldValue[value.indexOf(entry)], entry)}</TooltipTrigger>
102102
<TooltipContent>{label}</TooltipContent>
103103
</Tooltip>

src/components/result/utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
export function autoUnwrap<E>(item?: E | { raw?: E }) {
22
if (!item) {
33
return undefined
4-
} else if (typeof item === "string") {
5-
return item
64
} else if (typeof item === "object" && "raw" in item) {
75
return item.raw
8-
} else {
9-
return JSON.stringify(item)
10-
}
6+
} else return item
117
}
128

139
export function autoUnwrapArray<E>(item?: E[] | { raw?: E[] }) {

src/stories/GenericResultView.stories.tsx

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ export const Full: Story = {
101101
landingPageLocation: "https://www.chemotion-repository.net/inchikey/UMEUUBQHJXGOAF-UHFFFAOYSA-N.1",
102102
identifier: "CRS-39445",
103103
isMetadataFor: ["example1", "example2", "example3"],
104-
timestamp: "2024-05-21"
104+
timestamp: "2024-05-21",
105+
numberTest: 2025,
106+
numberArrayTest: [1, 2, 3],
107+
booleanTest: true,
108+
booleanArrayTest: [true, false],
109+
stringTest: "A single entry",
110+
stringArrayTest: ["Works", "with", "arrays"]
105111
},
106112
imageField: "locationPreview/Sample",
107113
invertImageInDarkMode: true,
@@ -114,19 +120,45 @@ export const Full: Story = {
114120
{
115121
icon: <GlobeIcon className="rfs-shrink-0 rfs-size-4 rfs-mr-2" />,
116122
field: "hadPrimarySource",
117-
valueMapper: tryURLPrettyPrint,
118-
label: "Primary Source"
123+
singleValueMapper: tryURLPrettyPrint,
124+
label: "Primary Source",
125+
clickBehavior: "follow-url"
119126
},
120127
{
121128
icon: <ScaleIcon className="rfs-shrink-0 rfs-size-4 rfs-mr-2" />,
122129
field: "licenseURL",
123-
valueMapper: tryURLPrettyPrint,
124-
label: "License URL"
130+
singleValueMapper: tryURLPrettyPrint,
131+
label: "License URL",
132+
clickBehavior: "follow-url"
125133
},
126134
{
127135
icon: <AtomIcon className="rfs-shrink-0 rfs-size-4 rfs-mr-2" />,
128136
field: "Compound",
129137
label: "Compound"
138+
},
139+
{
140+
field: "numberTest"
141+
},
142+
{
143+
field: "numberArrayTest"
144+
},
145+
{
146+
field: "booleanTest",
147+
singleValueMapper: (b) => (b ? "Yes" : "No")
148+
},
149+
{
150+
field: "booleanArrayTest",
151+
singleValueMapper: (b) => (b ? "Yes" : "No")
152+
},
153+
{
154+
field: "stringTest"
155+
},
156+
{
157+
field: "stringArrayTest"
158+
},
159+
{
160+
field: "stringArrayTest",
161+
singleValueMapper: (v) => v.toUpperCase()
130162
}
131163
]
132164
}

0 commit comments

Comments
 (0)