Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file input dialog #90

Merged
merged 13 commits into from
Oct 3, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Made text input box use a monospace font
- Disabled download buttons when there is no input/output.
- Added a modal to the file input section.

### πŸ”§ Internal changes

Expand Down
57 changes: 37 additions & 20 deletions demo/src/MemoryModelsUserInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Tooltip,
MenuItem,
Stack,
Modal,
} from "@mui/material";
import DownloadJSONButton from "./DownloadJSONButton";
import MemoryModelsMenu from "./MemoryModelsMenu";
Expand Down Expand Up @@ -45,6 +46,10 @@ type MemoryModelsUserInputPropTypes = MemoryModelsFileInputPropTypes &

function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) {
const [uploadedFileString, setUploadedFileString] = useState("");
const [open, setOpen] = useState(false);

const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);

const onChange = (event) => {
try {
Expand All @@ -54,7 +59,6 @@ function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) {
fileReader.onload = (event) => {
const fileString = event.target.result as string;
setUploadedFileString(fileString);
props.setTextData(fileString);
};
} catch (error) {
const errorMessage = `Error reading uploaded file as text. Please ensure it's in UTF-8 encoding: ${error.message}`;
Expand All @@ -66,29 +70,42 @@ function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) {

const onLoadButtonClick = () => {
props.setTextData(uploadedFileString);
setOpen(false);
};

return (
<Stack direction={"row"} spacing={2}>
<Input
type="file"
onChange={onChange}
inputProps={{
accept: "application/JSON",
"data-testid": "file-input",
}}
disableUnderline={true}
/>
<Button
data-testid="file-input-reapply-button"
variant="contained"
disabled={!uploadedFileString}
onClick={onLoadButtonClick}
sx={{ textTransform: "none" }}
>
Load file data
<div>
<Button onClick={handleOpen} sx={{ textTransform: "none" }}>
File Input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to "Upload JSON File"

</Button>
</Stack>
<Modal open={open} onClose={handleClose}>
<Box className="fileInputBox">
<div>
<Input
type="file"
onChange={onChange}
inputProps={{
accept: "application/JSON",
"data-testid": "file-input",
}}
disableUnderline={true}
/>
</div>
<div>
<Button
data-testid="file-input-reapply-button"
variant="contained"
color="primary"
disabled={!uploadedFileString}
onClick={onLoadButtonClick}
sx={{ textTransform: "none" }}
>
Load file data
</Button>
</div>
</Box>
</Modal>
</div>
);
}

Expand Down
13 changes: 13 additions & 0 deletions demo/src/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ input[type="number"]::-webkit-outer-spin-button {
transition: transform 0.2s ease-in-out;
transform: rotate(180deg);
}

.fileInputBox {
display: flex;
position: absolute;
top: 40%;
left: 20%;
width: 50%;
background-color: white;
border-radius: 5px;
justify-content: space-between;
padding: 10px;
align-items: center;
}
Loading