1+ import { createResource , For , Show , Suspense , type JSX } from 'solid-js' ;
2+
3+ import { SerovalValue } from './SerovalValue.tsx' ;
4+ import { PropertySeparator } from '../ui/Properties.tsx' ;
5+ import { Section } from '../ui/Section' ;
6+ import Button from '../ui/Button' ;
7+ import { Badge } from '../ui/Badge.tsx' ;
8+
9+
10+ import './FormDataViewer.css' ;
11+
12+ function DocumentIcon (
13+ props : JSX . IntrinsicElements [ "svg" ] & { title : string } ,
14+ ) : JSX . Element {
15+ return (
16+ < svg
17+ xmlns = "http://www.w3.org/2000/svg"
18+ fill = "none"
19+ viewBox = "0 0 24 24"
20+ stroke = "currentColor"
21+ { ...props }
22+ >
23+ < title > { props . title } </ title >
24+ < path stroke-linecap = "round" stroke-linejoin = "round" d = "M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m2.25 0H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
25+ </ svg >
26+ ) ;
27+ }
28+
29+ interface FormDataViewerInnerProps {
30+ source : FormData ;
31+ }
32+
33+ function FormDataViewerInner ( props : FormDataViewerInnerProps ) : JSX . Element {
34+ function openFileInNewTab ( file : File ) {
35+ const fileURL = URL . createObjectURL ( file ) ;
36+ const link = document . createElement ( "a" ) ;
37+ link . href = fileURL ;
38+ link . target = "_blank" ; // Open in a new tab
39+ link . style . display = "none" ; // Hide the link
40+ document . body . appendChild ( link ) ;
41+ link . click ( ) ;
42+ document . body . removeChild ( link ) ;
43+ }
44+
45+ return (
46+ < Section title = "FormData" options = { { size : 'sm' } } >
47+ < div data-start-form-data-viewer data-start-seroval-properties >
48+ < For each = { Array . from ( props . source . entries ( ) ) } >
49+ { ( [ key , value ] ) => (
50+ < div data-start-seroval-property >
51+ < SerovalValue value = { `"${ key } "` } />
52+ < PropertySeparator />
53+ { typeof value === 'string'
54+ ? < SerovalValue value = { JSON . stringify ( value ) } />
55+ : (
56+ < Button onClick = { ( ) => openFileInNewTab ( value ) } >
57+ < Badge type = "info" >
58+ < DocumentIcon title = { value . name } />
59+ { value . name }
60+ </ Badge >
61+ </ Button >
62+ ) }
63+ </ div >
64+ ) }
65+ </ For >
66+ </ div >
67+ </ Section >
68+ ) ;
69+ }
70+
71+ export interface FormDataViewerProps {
72+ source : FormData | Promise < FormData > ;
73+ }
74+
75+ export function FormDataViewer ( props : FormDataViewerProps ) {
76+ const [ data ] = createResource ( ( ) => props . source ) ;
77+
78+ return (
79+ < Suspense >
80+ < Show when = { data ( ) } >
81+ { ( current ) => < FormDataViewerInner source = { current ( ) } /> }
82+ </ Show >
83+ </ Suspense >
84+ ) ;
85+ }
0 commit comments