11import * as Common from "@frontend/common" ;
22import { PermMedia } from "@mui/icons-material" ;
3- import { Box , Button , CircularProgress , FormControl , InputLabel , MenuItem , Select , SelectProps , Stack , useMediaQuery } from "@mui/material" ;
3+ import { Box , Button , CircularProgress , FormControl , InputLabel , MenuItem , Select , SelectProps , Stack , styled , useMediaQuery } from "@mui/material" ;
44import { ErrorBoundary , Suspense } from "@suspensive/react" ;
55import * as React from "react" ;
66
7- import { Fieldset } from "./fieldset" ;
87import { useAppContext } from "../../contexts/app_context" ;
98import { PublicFileUploadDialog } from "../dialogs/public_file_upload" ;
9+ import { Fieldset } from "./fieldset" ;
1010
1111type PublicFileSelectorProps = SelectProps < string | null | undefined > & {
1212 setFileIdAsValue ?: ( fileId ?: string | null ) => void ;
@@ -17,18 +17,29 @@ const ImageFallback: React.FC<{ language: "ko" | "en" }> = ({ language }) => (
1717) ;
1818
1919type PublicFileSelectorState = {
20+ fileId ?: string | null ;
2021 openUploadDialog ?: boolean ;
2122} ;
2223
24+ const ScaledFallbackImage = styled ( Common . Components . FallbackImage ) ( {
25+ width : "100%" ,
26+ maxWidth : "20rem" ,
27+ objectFit : "contain" ,
28+ } ) ;
29+
2330export const PublicFileSelector : React . FC < PublicFileSelectorProps > = ErrorBoundary . with (
2431 { fallback : Common . Components . ErrorFallback } ,
25- Suspense . with ( { fallback : < CircularProgress /> } , ( { setFileIdAsValue, ...props } ) => {
32+ Suspense . with ( { fallback : < CircularProgress /> } , ( { onChange , setFileIdAsValue, ...props } ) => {
2633 const [ selectorState , setSelectorState ] = React . useState < PublicFileSelectorState > ( { } ) ;
2734 const { language } = useAppContext ( ) ;
2835 const participantPortalClient = Common . Hooks . BackendParticipantPortalAPI . useParticipantPortalClient ( ) ;
2936 const { data } = Common . Hooks . BackendParticipantPortalAPI . usePublicFilesQuery ( participantPortalClient ) ;
3037 const isMobile = useMediaQuery ( ( theme ) => theme . breakpoints . down ( "md" ) ) ;
3138
39+ const setSelectedFile : SelectProps < string | null | undefined > [ "onChange" ] = ( event , child ) => {
40+ setSelectorState ( ( ps ) => ( { ...ps , fileId : event . target . value } ) ) ;
41+ onChange ?.( event , child ) ;
42+ } ;
3243 const openUploadDialog = ( ) => setSelectorState ( ( ps ) => ( { ...ps , openUploadDialog : true } ) ) ;
3344 const closeUploadDialog = ( ) => setSelectorState ( ( ps ) => ( { ...ps , openUploadDialog : false } ) ) ;
3445
@@ -42,11 +53,11 @@ export const PublicFileSelector: React.FC<PublicFileSelectorProps> = ErrorBounda
4253 < PublicFileUploadDialog open = { ! ! selectorState . openUploadDialog } onClose = { closeUploadDialog } setFileIdAsValue = { setFileIdAsValue } />
4354 < Fieldset legend = { props . label ?. toString ( ) || "" } >
4455 < Stack direction = "column" spacing = { 2 } alignItems = "center" justifyContent = "center" >
45- < Common . Components . FallbackImage src = { selectedFile ?. file } alt = "Selected File" errorFallback = { < ImageFallback language = { language } /> } />
56+ < ScaledFallbackImage src = { selectedFile ?. file } alt = "Selected File" loading = "lazy " errorFallback = { < ImageFallback language = { language } /> } />
4657 < Stack direction = { isMobile ? "column" : "row" } spacing = { 2 } sx = { { width : "100%" } } alignItems = "center" >
4758 < FormControl fullWidth >
4859 < InputLabel id = "public-file-label" > { props . label } </ InputLabel >
49- < Select labelId = "public-file-label" { ...props } >
60+ < Select labelId = "public-file-label" onChange = { setSelectedFile } { ...props } >
5061 { files . map ( ( file ) => (
5162 < MenuItem key = { file . id } value = { file . id } children = { file . name } />
5263 ) ) }
0 commit comments