-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/toolpad-app/src/toolpad/propertyControls/file.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Typography } from '@mui/material'; | ||
import * as React from 'react'; | ||
import type { EditorProps } from '../../types'; | ||
|
||
function FilePropEditor({ value = [] }: EditorProps<any[]>) { | ||
const files = Array.from(value); | ||
const hasSelectedFiles = files?.length > 0; | ||
|
||
if (!hasSelectedFiles) { | ||
return ( | ||
<Typography variant="body2" noWrap> | ||
No files chosen | ||
</Typography> | ||
); | ||
} | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Typography variant="body2" paragraph sx={{ mb: 1, fontWeight: 700 }}> | ||
Files: | ||
</Typography> | ||
{files.map(({ name }) => ( | ||
<Typography variant="body2" noWrap key={name} sx={{ mb: 0.5 }}> | ||
{name} | ||
</Typography> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
export default FilePropEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as React from 'react'; | ||
import { TextField as MuiTextField, TextFieldProps as MuiTextFieldProps } from '@mui/material'; | ||
import { createComponent } from '@mui/toolpad-core'; | ||
|
||
interface FullFile { | ||
name: string; | ||
type: string; | ||
size: number; | ||
base64: null | string; | ||
} | ||
|
||
export type Props = MuiTextFieldProps & { | ||
multiple: boolean; | ||
onChange: (files: FullFile[]) => void; | ||
}; | ||
|
||
const readFile = async (file: Blob): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
const readerBase64 = new FileReader(); | ||
|
||
readerBase64.onload = (event) => { | ||
if (!event.target) { | ||
reject(); | ||
return; | ||
} | ||
|
||
resolve(event.target.result as string); | ||
}; | ||
|
||
readerBase64.readAsDataURL(file); | ||
}); | ||
}; | ||
|
||
function FilePicker({ multiple, onChange, ...props }: Props) { | ||
const handleChange = async (changeEvent: React.ChangeEvent<HTMLInputElement>) => { | ||
const filesPromises = Array.from(changeEvent.target.files || []).map(async (file) => { | ||
const fullFile: FullFile = { | ||
name: file.name, | ||
type: file.type, | ||
size: file.size, | ||
base64: await readFile(file), | ||
}; | ||
|
||
return fullFile; | ||
}); | ||
|
||
const files = await Promise.all(filesPromises); | ||
|
||
onChange(files); | ||
}; | ||
|
||
return ( | ||
<MuiTextField | ||
{...props} | ||
type="file" | ||
value={undefined} | ||
inputProps={{ multiple }} | ||
onChange={handleChange} | ||
/> | ||
); | ||
} | ||
|
||
export default createComponent(FilePicker, { | ||
argTypes: { | ||
value: { | ||
typeDef: { type: 'file' }, | ||
onChangeProp: 'onChange', | ||
}, | ||
label: { | ||
typeDef: { type: 'string' }, | ||
}, | ||
variant: { | ||
typeDef: { type: 'string', enum: ['outlined', 'filled', 'standard'] }, | ||
defaultValue: 'outlined', | ||
}, | ||
size: { | ||
typeDef: { type: 'string', enum: ['small', 'normal'] }, | ||
defaultValue: 'small', | ||
}, | ||
multiple: { | ||
typeDef: { type: 'boolean' }, | ||
defaultValue: true, | ||
}, | ||
fullWidth: { | ||
typeDef: { type: 'boolean' }, | ||
}, | ||
disabled: { | ||
typeDef: { type: 'boolean' }, | ||
}, | ||
sx: { | ||
typeDef: { type: 'object' }, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters